From b721fbbd0abb454c5c5d0a1439658d16f90a07e4 Mon Sep 17 00:00:00 2001 From: akash1810 Date: Fri, 3 May 2024 09:53:08 +0100 Subject: [PATCH 1/2] ci: Compile each package individually The `common` package has a compile error: ``` TS2742: The inferred type of createPullRequest cannot be named without a reference to `octokit-plugin-create-pull-request/node_modules/@octokit/types`. This is likely not portable. A type annotation is necessary. ``` It looks like compiling at the root isn't granular enough, as the error wasn't reported during builds. This change updates to compiling each project individually, so that we catch such issues at CI. --- package.json | 3 ++- packages/best-practices/package.json | 3 ++- packages/cdk/package.json | 3 ++- packages/cli/package.json | 3 ++- packages/cloudbuster/package.json | 3 ++- packages/common/package.json | 3 ++- packages/data-audit/package.json | 3 ++- packages/dependency-graph-integrator/package.json | 3 ++- packages/dev-environment/package.json | 3 ++- packages/diagrams/package.json | 3 ++- packages/github-actions-usage/package.json | 3 ++- packages/interactive-monitor/package.json | 3 ++- packages/repocop/package.json | 3 ++- packages/snyk-integrator/package.json | 3 ++- tsconfig.json | 1 - 15 files changed, 28 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index ae8ec654f..be2bfeaa3 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "test": "npm run test --workspaces --if-present", "test-update": "jest -u", "synth": "npm run synth --workspace=cdk", - "typecheck": "tsc", + "pretypecheck": "npm -w common run typecheck", + "typecheck": "npm run typecheck --workspaces", "build": "npm run build --workspaces --if-present", "lint": "eslint packages/** --ext .ts --no-error-on-unmatched-pattern" }, diff --git a/packages/best-practices/package.json b/packages/best-practices/package.json index aa3e73955..b10955bb3 100644 --- a/packages/best-practices/package.json +++ b/packages/best-practices/package.json @@ -4,7 +4,8 @@ "description": "A markdown file of best practices generated by a script from a definitions file", "type": "module", "scripts": { - "generate": "tsx src/index.ts" + "generate": "tsx src/index.ts", + "typecheck": "tsc --noEmit" }, "devDependencies": { "markdown-table": "^3.0.3" diff --git a/packages/cdk/package.json b/packages/cdk/package.json index beb80d6ac..d3beb685a 100644 --- a/packages/cdk/package.json +++ b/packages/cdk/package.json @@ -4,7 +4,8 @@ "scripts": { "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects cdk", "synth": "cdk synth --path-metadata false --version-reporting false", - "diff:code": "cdk diff --path-metadata false --version-reporting false --profile deployTools ServiceCatalogue-CODE" + "diff:code": "cdk diff --path-metadata false --version-reporting false --profile deployTools ServiceCatalogue-CODE", + "typecheck": "tsc --noEmit" }, "devDependencies": { "@guardian/cdk": "58.0.0", diff --git a/packages/cli/package.json b/packages/cli/package.json index 913f56c7c..fa09dab8f 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -2,7 +2,8 @@ "name": "cli", "version": "0.0.0", "scripts": { - "start": "tsx src/index.ts" + "start": "tsx src/index.ts", + "typecheck": "tsc --noEmit" }, "type": "module", "devDependencies": { diff --git a/packages/cloudbuster/package.json b/packages/cloudbuster/package.json index 524c39751..fc3753671 100644 --- a/packages/cloudbuster/package.json +++ b/packages/cloudbuster/package.json @@ -2,6 +2,7 @@ "name": "cloudbuster", "version": "1.0.0", "scripts": { - "start": "APP=cloudbuster tsx src/run-locally.ts" + "start": "APP=cloudbuster tsx src/run-locally.ts", + "typecheck": "tsc --noEmit" } } diff --git a/packages/common/package.json b/packages/common/package.json index 967cea3d9..6d5ed02d0 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -4,7 +4,8 @@ "type": "module", "scripts": { "postinstall": "prisma generate", - "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects common" + "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects common", + "typecheck": "tsc" }, "dependencies": { "@aws-sdk/client-secrets-manager": "^3.568.0", diff --git a/packages/data-audit/package.json b/packages/data-audit/package.json index e7f4d4a50..f72135b8d 100644 --- a/packages/data-audit/package.json +++ b/packages/data-audit/package.json @@ -6,7 +6,8 @@ "test": "echo \"Error: no test specified\"", "start": "APP=data-audit tsx src/run-locally.ts", "prebuild": "rm -rf dist", - "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk --external:@prisma/client --external:prisma" + "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk --external:@prisma/client --external:prisma", + "typecheck": "tsc --noEmit" }, "dependencies": { "@aws-sdk/client-lambda": "^3.568.0", diff --git a/packages/dependency-graph-integrator/package.json b/packages/dependency-graph-integrator/package.json index af18b9226..176209ff6 100644 --- a/packages/dependency-graph-integrator/package.json +++ b/packages/dependency-graph-integrator/package.json @@ -7,7 +7,8 @@ "scripts": { "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk", "start": "tsx src/run-locally.ts", - "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects dependency-graph-integrator" + "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects dependency-graph-integrator", + "typecheck": "tsc --noEmit" }, "dependencies": { "@aws-sdk/client-sns": "^3.568.0", diff --git a/packages/dev-environment/package.json b/packages/dev-environment/package.json index 8452fdcc2..e09b38f5e 100644 --- a/packages/dev-environment/package.json +++ b/packages/dev-environment/package.json @@ -3,7 +3,8 @@ "version": "0.0.0", "scripts": { "start": "./script/start", - "test": "find . | egrep '.yml|.yaml' | xargs yamllint" + "test": "find . | egrep '.yml|.yaml' | xargs yamllint", + "typecheck": "echo 'Nothing to typecheck'" }, "devDependencies": { "yaml-lint": "^1.7.0" diff --git a/packages/diagrams/package.json b/packages/diagrams/package.json index 42ef80e62..76641ea2f 100644 --- a/packages/diagrams/package.json +++ b/packages/diagrams/package.json @@ -4,7 +4,8 @@ "description": "Diagrams as code", "type": "module", "scripts": { - "generate": "mmdc -i diagram.mmd -o output.svg " + "generate": "mmdc -i diagram.mmd -o output.svg ", + "typecheck": "echo 'Nothing to typecheck'" }, "devDependencies": { "@mermaid-js/mermaid-cli": "10.8.0" diff --git a/packages/github-actions-usage/package.json b/packages/github-actions-usage/package.json index 7d66673b6..f8ef2cf56 100644 --- a/packages/github-actions-usage/package.json +++ b/packages/github-actions-usage/package.json @@ -5,7 +5,8 @@ "test": "node --import tsx --test **/*.test.ts", "start": "APP=github-actions-usage tsx src/run-locally.ts", "prebuild": "rm -rf dist", - "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@prisma/client --external:prisma" + "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@prisma/client --external:prisma", + "typecheck": "tsc --noEmit" }, "type": "module", "dependencies": { diff --git a/packages/interactive-monitor/package.json b/packages/interactive-monitor/package.json index 569418cbe..3901fd687 100644 --- a/packages/interactive-monitor/package.json +++ b/packages/interactive-monitor/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk", "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects interactive-monitor", - "start": "tsx src/run-locally.ts" + "start": "tsx src/run-locally.ts", + "typecheck": "tsc --noEmit" }, "author": "guardian", "dependencies": { diff --git a/packages/repocop/package.json b/packages/repocop/package.json index 97ec439ea..8be9aad9e 100644 --- a/packages/repocop/package.json +++ b/packages/repocop/package.json @@ -8,7 +8,8 @@ "scripts": { "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk --external:@prisma/client --external:prisma", "start": "APP=repocop tsx src/run-locally.ts", - "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects repocop" + "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects repocop", + "typecheck": "tsc --noEmit" }, "dependencies": { "@aws-sdk/client-cloudwatch": "^3.568.0", diff --git a/packages/snyk-integrator/package.json b/packages/snyk-integrator/package.json index d7d37a3fc..def180c59 100644 --- a/packages/snyk-integrator/package.json +++ b/packages/snyk-integrator/package.json @@ -7,7 +7,8 @@ "scripts": { "build": "esbuild src/index.ts --bundle --platform=node --target=node20 --outdir=dist --external:@aws-sdk", "start": "APP=snyk-integrator tsx src/run-locally.ts", - "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects snyk-integrator" + "test": "jest --detectOpenHandles --config ../../jest.config.js --selectProjects snyk-integrator", + "typecheck": "tsc --noEmit" }, "dependencies": { "@aws-sdk/client-sns": "^3.568.0", diff --git a/tsconfig.json b/tsconfig.json index 044e12101..7ddfa73a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "@guardian/tsconfig/tsconfig.json", "compilerOptions": { - "noEmit": true, "module": "commonjs", "esModuleInterop": true, "baseUrl": ".", From 83ab33436630d3db0170a2d84f96e5c6abba9a59 Mon Sep 17 00:00:00 2001 From: akash1810 Date: Fri, 3 May 2024 09:55:39 +0100 Subject: [PATCH 2/2] fix: Add explicit return type to `createPullRequest` There is a compilation error currently: ``` TS2742: The inferred type of createPullRequest cannot be named without a reference to `octokit-plugin-create-pull-request/node_modules/@octokit/types`. This is likely not portable. A type annotation is necessary. ``` The return type of `composeCreatePullRequest` is a bit tricky to infer. We're (currently) only using the `html_url` property, so update `createPullRequest` to return that, and therefore the return type of `createPullRequest` becomes `Promise`. --- packages/common/src/pull-requests.ts | 30 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/common/src/pull-requests.ts b/packages/common/src/pull-requests.ts index b1a755a7b..450d435b2 100644 --- a/packages/common/src/pull-requests.ts +++ b/packages/common/src/pull-requests.ts @@ -23,18 +23,24 @@ export function generateBranchName(prefix: string) { return `${prefix}-${randomBytes(8).toString('hex')}`; } +/** + * Creates or updates a pull request, and return its URL. + * On error, an exception is thrown, or undefined is returned. + */ export async function createPullRequest( octokit: Octokit, - { + props: CreatePullRequestOptions, +): Promise { + const { repoName, title, body, branchName, baseBranch = 'main', changes, - }: CreatePullRequestOptions, -) { - return await composeCreatePullRequest(octokit, { + } = props; + + const response = await composeCreatePullRequest(octokit, { owner: 'guardian', repo: repoName, title, @@ -46,6 +52,8 @@ export async function createPullRequest( files, })), }); + + return response?.data.html_url; } type PullRequestParameters = @@ -99,7 +107,7 @@ export async function createPrAndAddToProject( ); if (!existingPullRequest) { - const response = await createPullRequest(octokit, { + const pullRequestUrl = await createPullRequest(octokit, { repoName, title: prTitle, body: prBody, @@ -113,12 +121,12 @@ export async function createPrAndAddToProject( }, ], }); - console.log( - 'Pull request successfully created:', - response?.data.html_url, - ); - await addPrToProject(stage, repoName, boardNumber, author); - console.log('Updated project board'); + + if (pullRequestUrl) { + console.log('Pull request successfully created:', pullRequestUrl); + await addPrToProject(stage, repoName, boardNumber, author); + console.log('Updated project board'); + } } else { console.log( `Existing pull request found. Skipping creating a new one.`,