From 9589bc959395a04150c70d4aa3f883159b110752 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:12:29 +0000 Subject: [PATCH 1/6] chore(deps): update dependency @octokit/tsconfig to v4 --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9aadd17c6..9f7db924b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "universal-user-agent": "^7.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^3.0.0", + "@octokit/tsconfig": "^4.0.0", "@types/fetch-mock": "^7.3.1", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", @@ -1977,9 +1977,9 @@ } }, "node_modules/@octokit/tsconfig": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-3.1.0.tgz", - "integrity": "sha512-3jGTGqDnnh/MZlg/sf21J/0cghsmaSnG+ZPK+o++sQwUwgrLVtfbUi/BANHgf22SRnxhdYtOoRX90I9/cP+9BA==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-4.0.0.tgz", + "integrity": "sha512-hRd6UhX19m+8WhfrEpNLtm9TjuizYSG/dE0a+ivU71ylSxABVe4mEK+JMAGdjj6/gIQ+5DPegTPofi4P8VC5IA==", "dev": true, "license": "MIT" }, diff --git a/package.json b/package.json index 1d9b5d6fb..4f20d20b3 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "universal-user-agent": "^7.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^3.0.0", + "@octokit/tsconfig": "^4.0.0", "@types/fetch-mock": "^7.3.1", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", From 7d4e8a94df00eb7d3cf367ef191beaf8487ecf58 Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Tue, 24 Sep 2024 21:34:39 +0200 Subject: [PATCH 2/6] refactor(types): adjust cache method signatures --- src/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.ts b/src/types.ts index 4fbb6f8c7..67d54d135 100644 --- a/src/types.ts +++ b/src/types.ts @@ -162,8 +162,8 @@ export type InstallationAccessTokenData = { expiresAt: UTC_TIMESTAMP; permissions: Permissions; repositorySelection: REPOSITORY_SELECTION; - repositoryIds?: number[]; - repositoryNames?: string[]; + repositoryIds: number[] | undefined; + repositoryNames: string[] | undefined; singleFileName?: string; }; From 81847ef4bdab1b02381702f06dc905bf8e4b8e58 Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Tue, 24 Sep 2024 21:41:28 +0200 Subject: [PATCH 3/6] refactor(get-app-authentication): make TS happy when passing an object property as undefined --- src/get-app-authentication.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/get-app-authentication.ts b/src/get-app-authentication.ts index 49075160b..519ae3185 100644 --- a/src/get-app-authentication.ts +++ b/src/get-app-authentication.ts @@ -10,11 +10,16 @@ export async function getAppAuthentication({ timeDifference?: number; }): Promise { try { - const appAuthentication = await githubAppJwt({ - id: appId, - privateKey, - now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference, - }); + const appAuthentication = timeDifference + ? await githubAppJwt({ + id: appId, + privateKey, + now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference, + }) + : await githubAppJwt({ + id: appId, + privateKey, + }); return { type: "app", From fd67b16efb1371f91fbe3c9d8499fe22eb4bb0d7 Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Tue, 24 Sep 2024 21:59:51 +0200 Subject: [PATCH 4/6] refactor(get-installation-authentication): make TS happy by not passing undefined properties of payloads --- src/get-installation-authentication.ts | 75 ++++++++++++++++++-------- 1 file changed, 54 insertions(+), 21 deletions(-) diff --git a/src/get-installation-authentication.ts b/src/get-installation-authentication.ts index 7d02fab8e..686bcdda7 100644 --- a/src/get-installation-authentication.ts +++ b/src/get-installation-authentication.ts @@ -52,7 +52,7 @@ export async function getInstallationAuthentication( repositorySelection, } = result; - return toTokenAuthentication({ + const authParams = { installationId, token, createdAt, @@ -61,14 +61,45 @@ export async function getInstallationAuthentication( repositorySelection, repositoryIds, repositoryNames, - singleFileName, - }); + }; + + if (singleFileName) { + Object.assign(authParams, { + singleFileName, + }); + } + + return toTokenAuthentication(authParams); } } const appAuthentication = await getAppAuthentication(state); const request = customRequest || state.request; + const payload = { + installation_id: installationId, + mediaType: { + previews: ["machine-man"], + }, + headers: { + authorization: `bearer ${appAuthentication.token}`, + }, + }; + + if (options.repositoryIds) { + Object.assign(payload, { repository_ids: options.repositoryIds }); + } + + if (options.repositoryNames) { + Object.assign(payload, { + repositories: options.repositoryNames, + }); + } + + if (options.permissions) { + Object.assign(payload, { permissions: options.permissions }); + } + const { data: { token, @@ -78,18 +109,10 @@ export async function getInstallationAuthentication( repository_selection: repositorySelectionOptional, single_file: singleFileName, }, - } = await request("POST /app/installations/{installation_id}/access_tokens", { - installation_id: installationId, - repository_ids: options.repositoryIds, - repositories: options.repositoryNames, - permissions: options.permissions, - mediaType: { - previews: ["machine-man"], - }, - headers: { - authorization: `bearer ${appAuthentication.token}`, - }, - }); + } = await request( + "POST /app/installations/{installation_id}/access_tokens", + payload, + ); /* istanbul ignore next - permissions are optional per OpenAPI spec, but we think that is incorrect */ const permissions = permissionsOptional || {}; @@ -105,7 +128,7 @@ export async function getInstallationAuthentication( : void 0; const createdAt = new Date().toISOString(); - await set(state.cache, optionsWithInstallationTokenFromState, { + const cacheOptions = { token, createdAt, expiresAt, @@ -113,10 +136,15 @@ export async function getInstallationAuthentication( permissions, repositoryIds, repositoryNames, - singleFileName, - }); + }; + + if (singleFileName) { + Object.assign(payload, { singleFileName }); + } + + await set(state.cache, optionsWithInstallationTokenFromState, cacheOptions); - return toTokenAuthentication({ + const cacheData = { installationId, token, createdAt, @@ -125,6 +153,11 @@ export async function getInstallationAuthentication( permissions, repositoryIds, repositoryNames, - singleFileName, - }); + }; + + if (singleFileName) { + Object.assign(cacheData, { singleFileName }); + } + + return toTokenAuthentication(cacheData); } From 5433408310e65feab1e854bb3349f84b8fb2a06a Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Tue, 24 Sep 2024 22:07:49 +0200 Subject: [PATCH 5/6] refactor(get-app-authentication): allow undefined as value for timeDifference parameter --- src/get-app-authentication.ts | 24 +++++++++++++----------- src/get-installation-authentication.ts | 14 ++++---------- src/types.ts | 6 +++--- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/get-app-authentication.ts b/src/get-app-authentication.ts index 519ae3185..a8be1b911 100644 --- a/src/get-app-authentication.ts +++ b/src/get-app-authentication.ts @@ -7,19 +7,21 @@ export async function getAppAuthentication({ privateKey, timeDifference, }: State & { - timeDifference?: number; + timeDifference?: number | undefined; }): Promise { try { - const appAuthentication = timeDifference - ? await githubAppJwt({ - id: appId, - privateKey, - now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference, - }) - : await githubAppJwt({ - id: appId, - privateKey, - }); + const authOptions = { + id: appId, + privateKey, + }; + + if (timeDifference) { + Object.assign(authOptions, { + now: Math.floor(Date.now() / 1000) + timeDifference, + }); + } + + const appAuthentication = await githubAppJwt(authOptions); return { type: "app", diff --git a/src/get-installation-authentication.ts b/src/get-installation-authentication.ts index 686bcdda7..9a471fa09 100644 --- a/src/get-installation-authentication.ts +++ b/src/get-installation-authentication.ts @@ -40,6 +40,7 @@ export async function getInstallationAuthentication( state.cache, optionsWithInstallationTokenFromState, ); + if (result) { const { token, @@ -52,7 +53,7 @@ export async function getInstallationAuthentication( repositorySelection, } = result; - const authParams = { + return toTokenAuthentication({ installationId, token, createdAt, @@ -61,15 +62,8 @@ export async function getInstallationAuthentication( repositorySelection, repositoryIds, repositoryNames, - }; - - if (singleFileName) { - Object.assign(authParams, { - singleFileName, - }); - } - - return toTokenAuthentication(authParams); + singleFileName, + }); } } diff --git a/src/types.ts b/src/types.ts index 67d54d135..07543f520 100644 --- a/src/types.ts +++ b/src/types.ts @@ -162,9 +162,9 @@ export type InstallationAccessTokenData = { expiresAt: UTC_TIMESTAMP; permissions: Permissions; repositorySelection: REPOSITORY_SELECTION; - repositoryIds: number[] | undefined; - repositoryNames: string[] | undefined; - singleFileName?: string; + repositoryIds?: number[] | undefined; + repositoryNames?: string[] | undefined; + singleFileName?: string | undefined; }; export type CacheData = InstallationAccessTokenData; From f2606334682f92521d2d750bce484e5976f2206f Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Wed, 25 Sep 2024 00:16:00 +0200 Subject: [PATCH 6/6] build(types): add --exactOptionalPropertyTypes option --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4f20d20b3..db2a270df 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "lint:fix": "prettier --write '{src,test,scripts}/**/*.{ts,md}' README.md *.json", "pretest": "npm run -s lint", "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage", - "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 test/typescript-validate.ts" + "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 --exactOptionalPropertyTypes test/typescript-validate.ts" }, "repository": "github:octokit/auth-app.js", "keywords": [