From 357cfa7788066fbf300a3f6ba53b8dc03c2ba501 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 09:49:20 -0800 Subject: [PATCH 01/10] Fix --- packages/chronus/src/source-control/git.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/chronus/src/source-control/git.ts b/packages/chronus/src/source-control/git.ts index a3b6a498..1b47fba2 100644 --- a/packages/chronus/src/source-control/git.ts +++ b/packages/chronus/src/source-control/git.ts @@ -158,10 +158,13 @@ export function createGitSourceControl(repositoryPath: string): GitRepository { } async function listChangedFilesFromBase(baseBranch: string) { - let remoteBase: string; + let remoteBase: string | undefined; try { remoteBase = await findRemoteForBranch(baseBranch); } catch { + // ignore + } + if (remoteBase === undefined) { remoteBase = `refs/remotes/origin/${baseBranch}`; } return await listChangedFilesSince(remoteBase); From c463e68cf5cdc3fcdfdeef50541c61e2dea39108 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 09:56:13 -0800 Subject: [PATCH 02/10] test --- .github/workflows/changes.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/changes.yml b/.github/workflows/changes.yml index 448dd789..41086180 100644 --- a/.github/workflows/changes.yml +++ b/.github/workflows/changes.yml @@ -8,6 +8,8 @@ jobs: changes: if: ${{ github.actor != 'dependabot[bot]' }} runs-on: ubuntu-latest + permissions: + pull-requests: write steps: - uses: actions/checkout@v4 with: From 1cd81b846e3492f8c213649ce11965336df29341 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 10:00:19 -0800 Subject: [PATCH 03/10] . --- .github/workflows/changes.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/changes.yml b/.github/workflows/changes.yml index 41086180..448dd789 100644 --- a/.github/workflows/changes.yml +++ b/.github/workflows/changes.yml @@ -8,8 +8,6 @@ jobs: changes: if: ${{ github.actor != 'dependabot[bot]' }} runs-on: ubuntu-latest - permissions: - pull-requests: write steps: - uses: actions/checkout@v4 with: From 3372582de057c88f4f004f245d73aef5664c816d Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 10:15:40 -0800 Subject: [PATCH 04/10] same --- .github/workflows/changes.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/changes.yml b/.github/workflows/changes.yml index 448dd789..7e999440 100644 --- a/.github/workflows/changes.yml +++ b/.github/workflows/changes.yml @@ -4,6 +4,9 @@ on: pull_request: branches: ["main"] +permissions: + pull-requests: write + jobs: changes: if: ${{ github.actor != 'dependabot[bot]' }} From 6312931d7dca6812546d7e4748874fb76f7ebbbd Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:21:55 -0800 Subject: [PATCH 05/10] Try fix --- packages/chronus/package.json | 2 +- packages/github-pr-commenter/src/cli.ts | 116 ++++++++++++++++++++---- 2 files changed, 98 insertions(+), 20 deletions(-) diff --git a/packages/chronus/package.json b/packages/chronus/package.json index 6fb8c79a..31359a7d 100644 --- a/packages/chronus/package.json +++ b/packages/chronus/package.json @@ -34,6 +34,7 @@ "@changesets/apply-release-plan": "^7.0.0", "@changesets/parse": "^0.4.0", "@changesets/write": "^0.3.0", + "globby": "^14.0.0", "js-yaml": "^4.1.0", "micromatch": "^4.0.5", "picocolors": "^1.0.0", @@ -60,7 +61,6 @@ "@typescript-eslint/parser": "^6.10.0", "cross-spawn": "^7.0.3", "eslint": "^8.56.0", - "globby": "^14.0.0", "rimraf": "^5.0.5", "typescript": "^5.3.3" } diff --git a/packages/github-pr-commenter/src/cli.ts b/packages/github-pr-commenter/src/cli.ts index 188e8475..dadc19c4 100644 --- a/packages/github-pr-commenter/src/cli.ts +++ b/packages/github-pr-commenter/src/cli.ts @@ -5,47 +5,127 @@ try { } catch { // package only present in dev. } -import { context, getOctokit } from "@actions/github"; +import { getOctokit, context as githubActionContext } from "@actions/github"; import type { ChangeStatus, PackageStatus } from "@chronus/chronus"; import { getWorkspaceStatus } from "@chronus/chronus"; +// eslint-disable-next-line no-console +console.log("Github context", githubActionContext); + +interface Context { + repoName: string; + repoOwner: string; + prNumber: number; + headRef: string; + prTitle?: string; +} + +function getGithubContext(): Context | undefined { + if (githubActionContext === undefined) { + return undefined; + } + if (githubActionContext.payload.pull_request === undefined) { + throw new Error("Cannot run outside of a pull request"); + } + return { + repoName: githubActionContext.repo.repo, + repoOwner: githubActionContext.repo.owner, + prNumber: githubActionContext.issue.number, + headRef: githubActionContext.payload.pull_request.head.ref, + prTitle: githubActionContext.payload.pull_request.title, + }; +} + +function getAzureDevopsContext(): Context | undefined { + const repo = process.env["BUILD_REPOSITORY_NAME"]; + if (repo === undefined) { + return undefined; + } + const [repoOwner, repoName] = repo.split("/", 2); + const values = { + prNumber: process.env["SYSTEM_PULLREQUEST_PULLREQUESTNUMBER"], + repoName, + repoOwner, + headRef: process.env["SYSTEM_PULLREQUEST_SOURCEBRANCH"], + }; + + return values.prNumber && values.repoName && values.repoOwner && values.headRef ? (values as any) : undefined; +} + +function getGenericContext(): Context | undefined { + const values = { + prNumber: process.env["GH_PR_NUMBER"], + repoName: process.env["GH_REPO_OWNER"], + repoOwner: process.env["GH_REPO_REPO"], + headRef: process.env["GH_PR_HEAD_REF"], + prTitle: process.env["GH_PR_TITLE"], + }; + + return values.prNumber && values.repoName && values.repoOwner && values.headRef ? (values as any) : undefined; +} + const magicString = ""; async function main() { const token = process.env.GITHUB_TOKEN; if (!token) { throw new Error("GITHUB_TOKEN environment variable is not set"); } + const context = getGithubContext() ?? getAzureDevopsContext() ?? getGenericContext(); + + if (!context?.prNumber) { + throw new Error("PR number not found. Set $GH_PR_NUMBER"); + } + + if (context?.repoOwner === undefined) { + throw new Error("PR number not found. Set $GH_REPO_OWNER"); + } + if (!context?.repoName) { + throw new Error("PR number not found. Set $GH_REPO_REPO"); + } + if (!context?.headRef) { + throw new Error("PR number not found. Set $GH_PR_HEAD_REF"); + } + const github = getOctokit(token); + const pr = await github.rest.pulls.get({ + pull_number: context.prNumber, + owner: context.repoOwner, + repo: context.repoName, + }); const status = await getWorkspaceStatus(process.cwd()); - const content = resolveComment(status); + if (!context.prTitle) { + context.prTitle = pr.data.title; + } + + const content = resolveComment(status, pr.data.head, context as any); const comments = await github.rest.issues.listComments({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, + issue_number: context.prNumber, + owner: context.repoOwner, + repo: context.repoName, }); const existingComment = comments.data.find((x) => x.body?.includes(magicString)); if (existingComment) { github.rest.issues.updateComment({ comment_id: existingComment.id, - owner: context.repo.owner, - repo: context.repo.repo, + owner: context.repoOwner, + repo: context.repoName, body: content, }); } else { github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, + issue_number: context.prNumber, + owner: context.repoOwner, + repo: context.repoName, body: content, }); } } -function resolveComment(status: ChangeStatus): string { +function resolveComment(status: ChangeStatus, pr: any, context: Required): string { const undocummentedPackages = [...status.packages.values()].filter((x) => x.changed && !x.documented); const documentedPackages = [...status.packages.values()].filter((x) => x.changed && x.documented); @@ -53,7 +133,7 @@ function resolveComment(status: ChangeStatus): string { if (undocummentedPackages.length > 0) { content.push( - `:x: There is undocummented changes. Run \`chronus add\` to add a changeset or [click here](${addChangeSetUrl(undocummentedPackages)}).`, + `:x: There is undocummented changes. Run \`chronus add\` to add a changeset or [click here](${addChangeSetUrl(undocummentedPackages, pr, context)}).`, ); content.push(""); content.push(`**The following packages have changes but are not documented.**`); @@ -76,14 +156,12 @@ function resolveComment(status: ChangeStatus): string { return content.join("\n"); } -function addChangeSetUrl(undocummentedPackages: PackageStatus[]): string { - if (context.payload.pull_request === undefined) throw new Error("Not a pull request"); - const repoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}`; +function addChangeSetUrl(undocummentedPackages: PackageStatus[], pr: any, context: Required): string { + const repoUrl = pr.head.repo.html_url; - const ref = context.payload.pull_request.head.ref; const date = new Date(); const id = [ - ref.replace(/\//g, "-"), + context.headRef.replace(/\//g, "-"), date.getFullYear(), date.getMonth(), date.getDate(), @@ -92,8 +170,8 @@ function addChangeSetUrl(undocummentedPackages: PackageStatus[]): string { date.getSeconds(), ].join("-"); const filename = `.changeset/${id}.md`; - const content = renderChangesetTemplate(undocummentedPackages, context.payload.pull_request.title); - return `${repoUrl}/new/${ref}?filename=${filename}&value=${encodeURIComponent(content)}`; + const content = renderChangesetTemplate(undocummentedPackages, context.prTitle); + return `${repoUrl}/new/${context.headRef}?filename=${filename}&value=${encodeURIComponent(content)}`; } function renderChangesetTemplate(undocummentedPackages: PackageStatus[], title: string): string { From ae2bde102a54586b79972073c1adf150d01dc539 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:23:09 -0800 Subject: [PATCH 06/10] Create fix-resolve-ref-2024-0-29-18-16-6.md --- .changeset/fix-resolve-ref-2024-0-29-18-16-6.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-resolve-ref-2024-0-29-18-16-6.md diff --git a/.changeset/fix-resolve-ref-2024-0-29-18-16-6.md b/.changeset/fix-resolve-ref-2024-0-29-18-16-6.md new file mode 100644 index 00000000..0ce45939 --- /dev/null +++ b/.changeset/fix-resolve-ref-2024-0-29-18-16-6.md @@ -0,0 +1,5 @@ +--- +"@chronus/chronus": patch +--- + +Fix various issues(Missing depedencies on globby), resolving correct upstream branch From 7f3b1f5643b9b7baaca32d986f05c0bdd47ecccc Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:24:33 -0800 Subject: [PATCH 07/10] missing --- pnpm-lock.yaml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6290ec9c..c8094e45 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,6 +54,9 @@ importers: '@changesets/write': specifier: ^0.3.0 version: 0.3.0 + globby: + specifier: ^14.0.0 + version: 14.0.0 js-yaml: specifier: ^4.1.0 version: 4.1.0 @@ -127,9 +130,6 @@ importers: eslint: specifier: ^8.56.0 version: 8.56.0 - globby: - specifier: ^14.0.0 - version: 14.0.0 rimraf: specifier: ^5.0.5 version: 5.0.5 @@ -1392,7 +1392,7 @@ packages: /@sindresorhus/merge-streams@1.0.0: resolution: {integrity: sha512-rUV5WyJrJLoloD4NDN1V1+LDMDWOa4OTsT4yYJwQNpTU6FWxkxHpL7eu4w+DmiH8x/EAM1otkPE1+LaspIbplw==} engines: {node: '>=18'} - dev: true + dev: false /@types/aws-lambda@8.10.132: resolution: {integrity: sha512-fXP6xK+f0Ir9dt4Tp2NjMmu/nOcQb8e0c8b7z7ql1xo/r4h/uJjGe+1aeH11yhbWU2wakJ5i4gtQAviu6h8OOg==} @@ -3112,7 +3112,7 @@ packages: path-type: 5.0.0 slash: 5.1.0 unicorn-magic: 0.1.0 - dev: true + dev: false /gopd@1.0.1: resolution: {integrity: sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==} @@ -4063,7 +4063,7 @@ packages: /path-type@5.0.0: resolution: {integrity: sha512-5HviZNaZcfqP95rwpv+1HDgUamezbqdSYTyzjTvwtJSnIH+3vnbmWsItli8OFEndS984VT55M3jduxZbX351gg==} engines: {node: '>=12'} - dev: true + dev: false /pathe@1.1.2: resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==} @@ -4479,7 +4479,7 @@ packages: /slash@5.1.0: resolution: {integrity: sha512-ZA6oR3T/pEyuqwMgAKT0/hAv8oAXckzbkmR0UkUosQ+Mc4RxGoJkRmwHgHufaenlyAgE1Mxgpdcrf75y6XcnDg==} engines: {node: '>=14.16'} - dev: true + dev: false /smartwrap@2.0.2: resolution: {integrity: sha512-vCsKNQxb7PnCNd2wY1WClWifAc2lwqsG8OaswpJkVJsvMGcnEntdTCDajZCkk93Ay1U3t/9puJmb525Rg5MZBA==} @@ -4890,7 +4890,7 @@ packages: /unicorn-magic@0.1.0: resolution: {integrity: sha512-lRfVq8fE8gz6QMBuDM6a+LO3IAzTi05H6gCVaUpir2E1Rwpo4ZUog45KpNXKC/Mn3Yb9UDuHumeFTo9iV/D9FQ==} engines: {node: '>=18'} - dev: true + dev: false /unique-string@3.0.0: resolution: {integrity: sha512-VGXBUVwxKMBUznyffQweQABPRRW1vHZAbadFZud4pLFAqRGvv/96vafgjWFqzourzr8YonlQiPgH0YCJfawoGQ==} From 75843cb4d3b1a504ff87e91dbf8b99064d9cf25f Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:28:04 -0800 Subject: [PATCH 08/10] l --- packages/github-pr-commenter/src/cli.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/github-pr-commenter/src/cli.ts b/packages/github-pr-commenter/src/cli.ts index dadc19c4..bed55e3e 100644 --- a/packages/github-pr-commenter/src/cli.ts +++ b/packages/github-pr-commenter/src/cli.ts @@ -9,9 +9,6 @@ import { getOctokit, context as githubActionContext } from "@actions/github"; import type { ChangeStatus, PackageStatus } from "@chronus/chronus"; import { getWorkspaceStatus } from "@chronus/chronus"; -// eslint-disable-next-line no-console -console.log("Github context", githubActionContext); - interface Context { repoName: string; repoOwner: string; @@ -21,6 +18,9 @@ interface Context { } function getGithubContext(): Context | undefined { + // eslint-disable-next-line no-console + console.log("Github context", githubActionContext); + if (githubActionContext === undefined) { return undefined; } @@ -71,7 +71,8 @@ async function main() { throw new Error("GITHUB_TOKEN environment variable is not set"); } const context = getGithubContext() ?? getAzureDevopsContext() ?? getGenericContext(); - + // eslint-disable-next-line no-console + console.log("Resolved context", context); if (!context?.prNumber) { throw new Error("PR number not found. Set $GH_PR_NUMBER"); } From 77ae3452140dae454778858ba99ffce5a168ea13 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:29:47 -0800 Subject: [PATCH 09/10] . --- packages/github-pr-commenter/src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/github-pr-commenter/src/cli.ts b/packages/github-pr-commenter/src/cli.ts index bed55e3e..763b4169 100644 --- a/packages/github-pr-commenter/src/cli.ts +++ b/packages/github-pr-commenter/src/cli.ts @@ -100,7 +100,7 @@ async function main() { context.prTitle = pr.data.title; } - const content = resolveComment(status, pr.data.head, context as any); + const content = resolveComment(status, pr.data, context as any); const comments = await github.rest.issues.listComments({ issue_number: context.prNumber, From b363aa3442cf89139ba3a2e8318a3129fe58adfb Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Jan 2024 11:30:50 -0800 Subject: [PATCH 10/10] Create fix-resolve-ref-2024-0-29-19-30-13.md --- .changeset/fix-resolve-ref-2024-0-29-19-30-13.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fix-resolve-ref-2024-0-29-19-30-13.md diff --git a/.changeset/fix-resolve-ref-2024-0-29-19-30-13.md b/.changeset/fix-resolve-ref-2024-0-29-19-30-13.md new file mode 100644 index 00000000..7f70811e --- /dev/null +++ b/.changeset/fix-resolve-ref-2024-0-29-19-30-13.md @@ -0,0 +1,5 @@ +--- +"@chronus/github-pr-commenter": patch +--- + +Allow to run this not in github actions(Azure pipelines or custom ci system)