From a4c2b678c61e0099c67a99e5d3cd5ce2e528b182 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 10:54:10 +0200 Subject: [PATCH 01/14] WIP --- .github/workflows/build.yml | 20 ++++++ .../.eslintrc.cjs | 14 ++++ .../external-contributor-gh-action/action.yml | 9 +++ .../external-contributor-gh-action/index.mjs | 64 +++++++++++++++++++ .../package.json | 22 +++++++ package.json | 1 + 6 files changed, 130 insertions(+) create mode 100644 dev-packages/external-contributor-gh-action/.eslintrc.cjs create mode 100644 dev-packages/external-contributor-gh-action/action.yml create mode 100644 dev-packages/external-contributor-gh-action/index.mjs create mode 100644 dev-packages/external-contributor-gh-action/package.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c3bda957409b..a954ea303d47 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -224,6 +224,26 @@ jobs: message: | ⚠️ This PR is opened against **master**. You probably want to open it against **develop**. + job_external_contributor: + name: Mention external contributor in changelog + needs: job_get_metadata + runs-on: ubuntu-20.04 + if: | + github.event_name == 'pull_request' + && github.event.pull_request.author_association != 'COLLABORATOR' + && github.event.pull_request.author_association != 'MEMBER' + && github.event.pull_request.author_association != 'OWNER' + permissions: + pull-requests: write + steps: + - uses: actions/checkout@v4 + - uses: ./dev-packages/external-contributor-gh-action + with: + name: ${{ github.event.pull_request.user.login }} + - uses: parkerbxyz/suggest-changes@v1 + with: + comment: 'Please commit the suggested changes from markdownlint.' + job_build: name: Build needs: [job_get_metadata, job_install_deps] diff --git a/dev-packages/external-contributor-gh-action/.eslintrc.cjs b/dev-packages/external-contributor-gh-action/.eslintrc.cjs new file mode 100644 index 000000000000..8c67e0037908 --- /dev/null +++ b/dev-packages/external-contributor-gh-action/.eslintrc.cjs @@ -0,0 +1,14 @@ +module.exports = { + extends: ['../../.eslintrc.js'], + parserOptions: { + sourceType: 'module', + ecmaVersion: 'latest', + }, + + overrides: [ + { + files: ['*.mjs'], + extends: ['@sentry-internal/sdk/src/base'], + }, + ], +}; diff --git a/dev-packages/external-contributor-gh-action/action.yml b/dev-packages/external-contributor-gh-action/action.yml new file mode 100644 index 000000000000..8c6fb9c04944 --- /dev/null +++ b/dev-packages/external-contributor-gh-action/action.yml @@ -0,0 +1,9 @@ +name: 'external-contributor-gh-action' +description: 'Add external contributors to CHANGELOG.md' +inputs: + name: + required: true + description: 'The name of the external contributor' +runs: + using: 'node20' + main: 'index.mjs' diff --git a/dev-packages/external-contributor-gh-action/index.mjs b/dev-packages/external-contributor-gh-action/index.mjs new file mode 100644 index 000000000000..f8ca08e6e01d --- /dev/null +++ b/dev-packages/external-contributor-gh-action/index.mjs @@ -0,0 +1,64 @@ +import { promises as fs } from 'node:fs'; +import path from 'node:path'; +import * as core from '@actions/core'; + +const UNRELEASED_HEADING = `## Unreleased + +- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott +`; + +const contributorMessageRegex = /Work in this release was contributed by (.+)\. Thank you for your contribution!/; + +async function run() { + const { getInput } = core; + + const name = getInput('name'); + + if (!name) { + return; + } + + const cwd = process.cwd(); + const changelogFilePath = path.resolve(cwd, 'CHANGELOG.md'); + + const changelogStr = await fs.readFile(changelogFilePath, 'utf8'); + + // Find the unreleased section + const start = changelogStr.indexOf(UNRELEASED_HEADING) + UNRELEASED_HEADING.length; + const end = changelogStr.slice(start).indexOf('## '); + + const inBetween = changelogStr.slice(start, start + end); + + const existing = contributorMessageRegex.exec(inBetween); + + // If the contributor message already exists, add the new contributor to the list + if (existing) { + const users = existing[1].split(/(?:,? and )|(?:, )/); + if (!users.includes(name)) { + users.push(name); + } + + const formatter = new Intl.ListFormat('en', { + style: 'long', + type: 'conjunction', + }); + + const newContributors = formatter.format(users); + const newChangelog = changelogStr.replace( + contributorMessageRegex, + `Work in this release was contributed by ${newContributors}. Thank you for your contribution!`, + ); + + fs.writeFile(changelogFilePath, newChangelog); + return; + } + + // If the contributor message does not exist, add it + const newChangelog = changelogStr.replace( + UNRELEASED_HEADING, + `${UNRELEASED_HEADING}\nWork in this release was contributed by ${name}. Thank you for your contribution!\n`, + ); + fs.writeFile(changelogFilePath, newChangelog); +} + +run(); diff --git a/dev-packages/external-contributor-gh-action/package.json b/dev-packages/external-contributor-gh-action/package.json new file mode 100644 index 000000000000..6b69533b7ba2 --- /dev/null +++ b/dev-packages/external-contributor-gh-action/package.json @@ -0,0 +1,22 @@ +{ + "name": "@sentry-internal/external-contributor-gh-action", + "description": "An internal Github Action to add external contributors to the CHANGELOG.md file.", + "version": "8.8.0", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "private": true, + "main": "index.mjs", + "type": "module", + "scripts": { + "lint": "eslint . --format stylish", + "fix": "eslint . --format stylish --fix" + }, + "dependencies": { + "@actions/core": "1.10.1" + }, + "volta": { + "extends": "../../package.json" + } +} diff --git a/package.json b/package.json index 42a1b6cd0b89..b4676d07be73 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "dev-packages/overhead-metrics", "dev-packages/test-utils", "dev-packages/size-limit-gh-action", + "dev-packages/external-contributor-gh-action", "dev-packages/rollup-utils" ], "devDependencies": { From 285bb7149844b1bcd71ae2f70ea6da5338a7c1fb Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 10:56:22 +0200 Subject: [PATCH 02/14] WIP test... --- .github/workflows/build.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a954ea303d47..376096ce6570 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -230,9 +230,6 @@ jobs: runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' - && github.event.pull_request.author_association != 'COLLABORATOR' - && github.event.pull_request.author_association != 'MEMBER' - && github.event.pull_request.author_association != 'OWNER' permissions: pull-requests: write steps: From 387f8ba97439c2e81c4553957e065923fdb0de38 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 10:58:31 +0200 Subject: [PATCH 03/14] fix it --- .github/workflows/build.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 376096ce6570..d2b5c3b7fa2a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -234,10 +234,12 @@ jobs: pull-requests: write steps: - uses: actions/checkout@v4 - - uses: ./dev-packages/external-contributor-gh-action + - name: Add external contributor to CHANGELOG.md + uses: ./dev-packages/external-contributor-gh-action with: name: ${{ github.event.pull_request.user.login }} - - uses: parkerbxyz/suggest-changes@v1 + - name: Propose changes + uses: parkerbxyz/suggest-changes@v1 with: comment: 'Please commit the suggested changes from markdownlint.' From eafe9866ad4ba5387ddf20ec1a8e511f59cb96ce Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 11:00:52 +0200 Subject: [PATCH 04/14] run after install --- .github/workflows/build.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d2b5c3b7fa2a..daef1dad7fd1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -226,7 +226,7 @@ jobs: job_external_contributor: name: Mention external contributor in changelog - needs: job_get_metadata + needs: job_install_deps runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' @@ -234,6 +234,17 @@ jobs: pull-requests: write steps: - uses: actions/checkout@v4 + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version-file: 'package.json' + - name: Check dependency cache + uses: actions/cache/restore@v4 + with: + path: ${{ env.CACHED_DEPENDENCY_PATHS }} + key: ${{ needs.job_install_deps.outputs.dependency_cache_key }} + fail-on-cache-miss: true + - name: Add external contributor to CHANGELOG.md uses: ./dev-packages/external-contributor-gh-action with: From eb45709fd28ae2ccaf84a2a209318ef226d52f90 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 11:17:40 +0200 Subject: [PATCH 05/14] fix it --- .github/workflows/build.yml | 5 +++-- .../external-contributor-gh-action/index.mjs | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index daef1dad7fd1..52c83fdb7a39 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -225,12 +225,13 @@ jobs: ⚠️ This PR is opened against **master**. You probably want to open it against **develop**. job_external_contributor: - name: Mention external contributor in changelog + name: External Contributors needs: job_install_deps runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' permissions: + contents: read pull-requests: write steps: - uses: actions/checkout@v4 @@ -252,7 +253,7 @@ jobs: - name: Propose changes uses: parkerbxyz/suggest-changes@v1 with: - comment: 'Please commit the suggested changes from markdownlint.' + comment: 'Add external contributor to CHANGELOG.md' job_build: name: Build diff --git a/dev-packages/external-contributor-gh-action/index.mjs b/dev-packages/external-contributor-gh-action/index.mjs index f8ca08e6e01d..1fb19560f1fc 100644 --- a/dev-packages/external-contributor-gh-action/index.mjs +++ b/dev-packages/external-contributor-gh-action/index.mjs @@ -12,13 +12,15 @@ const contributorMessageRegex = /Work in this release was contributed by (.+)\. async function run() { const { getInput } = core; - const name = getInput('name'); + const name = getInput('name') || '@test-user'; if (!name) { return; } - const cwd = process.cwd(); + const ghUserName = name.startsWith('@') ? name : `@${name}`; + + const cwd = path.join(process.cwd(), '../..'); const changelogFilePath = path.resolve(cwd, 'CHANGELOG.md'); const changelogStr = await fs.readFile(changelogFilePath, 'utf8'); @@ -34,8 +36,8 @@ async function run() { // If the contributor message already exists, add the new contributor to the list if (existing) { const users = existing[1].split(/(?:,? and )|(?:, )/); - if (!users.includes(name)) { - users.push(name); + if (!users.includes(ghUserName)) { + users.push(ghUserName); } const formatter = new Intl.ListFormat('en', { @@ -50,15 +52,21 @@ async function run() { ); fs.writeFile(changelogFilePath, newChangelog); + + // eslint-disable-next-line no-console + console.log('Added contributor to list of existing contributors.'); return; } // If the contributor message does not exist, add it const newChangelog = changelogStr.replace( UNRELEASED_HEADING, - `${UNRELEASED_HEADING}\nWork in this release was contributed by ${name}. Thank you for your contribution!\n`, + `${UNRELEASED_HEADING}\nWork in this release was contributed by ${ghUserName}. Thank you for your contribution!\n`, ); fs.writeFile(changelogFilePath, newChangelog); + + // eslint-disable-next-line no-console + console.log('Added contributor message.'); } run(); From 9f0e0bbc83e5fc259393183b06de03d4771f9b38 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 11:24:27 +0200 Subject: [PATCH 06/14] oops fix it... --- dev-packages/external-contributor-gh-action/index.mjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev-packages/external-contributor-gh-action/index.mjs b/dev-packages/external-contributor-gh-action/index.mjs index 1fb19560f1fc..7eff418e9205 100644 --- a/dev-packages/external-contributor-gh-action/index.mjs +++ b/dev-packages/external-contributor-gh-action/index.mjs @@ -12,7 +12,7 @@ const contributorMessageRegex = /Work in this release was contributed by (.+)\. async function run() { const { getInput } = core; - const name = getInput('name') || '@test-user'; + const name = getInput('name'); if (!name) { return; @@ -20,7 +20,7 @@ async function run() { const ghUserName = name.startsWith('@') ? name : `@${name}`; - const cwd = path.join(process.cwd(), '../..'); + const cwd = process.cwd(); const changelogFilePath = path.resolve(cwd, 'CHANGELOG.md'); const changelogStr = await fs.readFile(changelogFilePath, 'utf8'); @@ -65,8 +65,8 @@ async function run() { ); fs.writeFile(changelogFilePath, newChangelog); - // eslint-disable-next-line no-console - console.log('Added contributor message.'); + // eslint-disable-next-line no-console + console.log('Added contributor message.'); } run(); From 63d44d3b865b8476bc4953fdba35e57bcb41607e Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 13:45:29 +0200 Subject: [PATCH 07/14] auto commit? --- .github/workflows/build.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 52c83fdb7a39..04ed6d397663 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -231,8 +231,7 @@ jobs: if: | github.event_name == 'pull_request' permissions: - contents: read - pull-requests: write + contents: write steps: - uses: actions/checkout@v4 - name: Set up Node @@ -250,10 +249,10 @@ jobs: uses: ./dev-packages/external-contributor-gh-action with: name: ${{ github.event.pull_request.user.login }} - - name: Propose changes - uses: parkerbxyz/suggest-changes@v1 + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 with: - comment: 'Add external contributor to CHANGELOG.md' + commit_message: "Add external contributor to CHANGELOG.md" job_build: name: Build From c3497deeaf1d7d0554f59e153e484d5fb30f9c12 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 13:50:56 +0200 Subject: [PATCH 08/14] fix it --- .github/workflows/build.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 04ed6d397663..7fafdd9e1693 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -234,6 +234,8 @@ jobs: contents: write steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} - name: Set up Node uses: actions/setup-node@v4 with: @@ -253,6 +255,8 @@ jobs: uses: stefanzweifel/git-auto-commit-action@v5 with: commit_message: "Add external contributor to CHANGELOG.md" + skip_fetch: true + skip_checkout: true job_build: name: Build From 12b44005c606081933b228b5c91ef91b2cbba86a Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 14:35:15 +0200 Subject: [PATCH 09/14] create PR for change? --- .github/workflows/build.yml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7fafdd9e1693..d49ef020d75e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -251,12 +251,14 @@ jobs: uses: ./dev-packages/external-contributor-gh-action with: name: ${{ github.event.pull_request.user.login }} - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v5 - with: - commit_message: "Add external contributor to CHANGELOG.md" - skip_fetch: true - skip_checkout: true + - name: Create PR with changes + uses: peter-evans/create-pull-request@v6 + with: + commit-message: "ref: Add external contributor to CHANGELOG.md" + title: "ref: Add external contributor to CHANGELOG.md" + branch: 'external-contributor/patch-${{ github.event.pull_request.user.login }}' + delete-branch: true + path: 'CHANGELOG.md' job_build: name: Build From 4248fb3cfc9de5758b5a6b334901830337d309ba Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 14:43:38 +0200 Subject: [PATCH 10/14] does this work now?? --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d49ef020d75e..02c57fb3ae4c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -258,7 +258,6 @@ jobs: title: "ref: Add external contributor to CHANGELOG.md" branch: 'external-contributor/patch-${{ github.event.pull_request.user.login }}' delete-branch: true - path: 'CHANGELOG.md' job_build: name: Build From 914544a01471ad453f9e7274165b75bfdb01fe7c Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 15:03:47 +0200 Subject: [PATCH 11/14] fix permissions?? --- .github/workflows/build.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 02c57fb3ae4c..d46f98b1994f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -230,8 +230,6 @@ jobs: runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' - permissions: - contents: write steps: - uses: actions/checkout@v4 with: From 19ed5c194f9214083553e7592409bb7d9a421065 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 15:14:41 +0200 Subject: [PATCH 12/14] tweak it --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d46f98b1994f..b2d29ef85bde 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -256,6 +256,7 @@ jobs: title: "ref: Add external contributor to CHANGELOG.md" branch: 'external-contributor/patch-${{ github.event.pull_request.user.login }}' delete-branch: true + body: This PR adds the external contributor to the CHANGELOG.md file, so that they are credited for their contribution. job_build: name: Build From 7f4a7206a7c63fd181d6dc0cfc9204b71f7b81cd Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 15:24:43 +0200 Subject: [PATCH 13/14] Revert "WIP test..." This reverts commit b0c4a8ae85856b5e3c3ea4c14e02f624128916f5. --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b2d29ef85bde..a9cc0dfd76b4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -230,6 +230,9 @@ jobs: runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' + && github.event.pull_request.author_association != 'COLLABORATOR' + && github.event.pull_request.author_association != 'MEMBER' + && github.event.pull_request.author_association != 'OWNER' steps: - uses: actions/checkout@v4 with: From 4e49fd2b9f2d379b835165f0dc4dee49a4070fe2 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 10 Jun 2024 15:59:29 +0200 Subject: [PATCH 14/14] run only on open & reopen --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9cc0dfd76b4..f9ebb39777f5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -230,6 +230,7 @@ jobs: runs-on: ubuntu-20.04 if: | github.event_name == 'pull_request' + && (github.action == 'opened' || github.action == 'reopened') && github.event.pull_request.author_association != 'COLLABORATOR' && github.event.pull_request.author_association != 'MEMBER' && github.event.pull_request.author_association != 'OWNER'