From 060699ff27344809b44bcb1b1434891fde2c5ee2 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 15:23:42 -0500 Subject: [PATCH 1/8] feat: add console.log output of the PR as it comes from the API I find having the output of the data we are working with for an action to be helpful in further development and troubleshooting of action issues. --- src/action.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/action.js b/src/action.js index f71dd2d..f2bba16 100644 --- a/src/action.js +++ b/src/action.js @@ -16,6 +16,8 @@ const run = async () => { const assignees = pr.assignees const author = pr.user + console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`) + if ( (!assignees || assignees.length === 0) && author.login !== 'dependabot[bot]' From 412c89aea3d31b0d8ea924228cba86baaf2671a7 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 13:25:30 -0500 Subject: [PATCH 2/8] build: upgrade to node 20 for the action runtime Address a deprecation by GitHub: Node.js 16 actions are deprecated. Please update the following actions to use Node.js 20: planningcenter/planningcenter/pull-assign-action@v1 References: https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/ --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index a8b2f1a..332495b 100644 --- a/action.yml +++ b/action.yml @@ -6,5 +6,5 @@ inputs: required: false default: ${{ github.token }} runs: - using: "node16" + using: node20 main: "dist/index.js" From 10ba62b94fc9ce83f8dae37f338a8b3ba27f2e7d Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 13:22:32 -0500 Subject: [PATCH 3/8] fix: there is an attribute just for bots, let's use that I noticed this action sometimes doesn't work. And in testing I see the name for Dependabot is not always what we expect. But I see this is_bot attribute so let's use it. Maybe that's why it sometimes fails. { "author": { "is_bot": true, "login": "app/dependabot" } } --- src/action.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action.js b/src/action.js index f2bba16..8f7d412 100644 --- a/src/action.js +++ b/src/action.js @@ -20,7 +20,7 @@ const run = async () => { if ( (!assignees || assignees.length === 0) && - author.login !== 'dependabot[bot]' + !author.is_bot ) { await client.request( `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, From bcb2026c3560d2f6c213a73477be3243817707c2 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 15:29:32 -0500 Subject: [PATCH 4/8] refactor: move the try catch to the bottom of the script This is mostly for my readability. I found this to be a nice flow when recently working on our planningcenter/plus-one-action GitHub action. --- src/action.js | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/action.js b/src/action.js index 8f7d412..a1c06ba 100644 --- a/src/action.js +++ b/src/action.js @@ -1,37 +1,37 @@ const core = require('@actions/core') const github = require('@actions/github') -const run = async () => { - try { - const githubToken = core.getInput('github_token') +async function run() { + const githubToken = core.getInput('github_token') - if (!githubToken) throw Error(`input 'github_token' is required`) + if (!githubToken) throw Error(`input 'github_token' is required`) - const client = github.getOctokit(githubToken) - const owner = github.context.payload.repository.owner.login - const repo = github.context.payload.repository.name - const issue_number = github.context.payload.pull_request.number + const client = github.getOctokit(githubToken) + const owner = github.context.payload.repository.owner.login + const repo = github.context.payload.repository.name + const issue_number = github.context.payload.pull_request.number - const pr = github.context.payload.pull_request - const assignees = pr.assignees - const author = pr.user + const pr = github.context.payload.pull_request + const assignees = pr.assignees + const author = pr.user - console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`) + console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`) - if ( - (!assignees || assignees.length === 0) && - !author.is_bot - ) { - await client.request( - `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, - { owner, repo, issue_number, assignees: [author.login] } - ) - } - } catch (error) { - console.log('Error:', error) - core.error(error) - core.setFailed(error) + if ( + (!assignees || assignees.length === 0) && + !author.is_bot + ) { + await client.request( + `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, + { owner, repo, issue_number, assignees: [author.login] } + ) } } -run() +try { + run() +} catch (error) { + console.log('Error:', error) + core.error(error) + core.setFailed(error.message) +} From bca89eb1921295b8c83bbd2dc58a65b3c5e8720a Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 15:35:13 -0500 Subject: [PATCH 5/8] fix: older API versions have different attributes for bots "user": { ... "login": "dependabot[bot]", "type": "Bot", ... } --- src/action.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/action.js b/src/action.js index a1c06ba..8f6a8ee 100644 --- a/src/action.js +++ b/src/action.js @@ -19,7 +19,7 @@ async function run() { if ( (!assignees || assignees.length === 0) && - !author.is_bot + (author.is_bot === false || (author.type && author.type !== 'Bot')) ) { await client.request( `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, From 8b6c697dd557c151530b0da0b7b846d0a7a76e50 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 15:49:53 -0500 Subject: [PATCH 6/8] docs: tweak name of action instructions to match action name case --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a6e61bd..bd2af7e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is a GitHub action that assigns a PR owner upon opening a pull request. Sample config, place in `.github/workflows/assign.yml` ``` -name: Pull assign +name: Pull Assign on: pull_request: From aea6582296b41dde2300f206f0d0a27ab056f7fd Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Tue, 4 Jun 2024 15:59:21 -0500 Subject: [PATCH 7/8] fix: only console.log used attributes The PR output was massive and not so helpful. Let's just output the attributes we use in our logic. --- src/action.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/action.js b/src/action.js index 8f6a8ee..cfe4050 100644 --- a/src/action.js +++ b/src/action.js @@ -15,7 +15,8 @@ async function run() { const assignees = pr.assignees const author = pr.user - console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`) + console.log(`assignees: ${JSON.stringify(assignees, undefined, 2)}`) + console.log(`author: ${JSON.stringify(author, undefined, 2)}`) if ( (!assignees || assignees.length === 0) && From 4ca874f8af396c63e972b4b4ba424e3dd54af356 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 4 Jun 2024 21:10:17 +0000 Subject: [PATCH 8/8] Automated build of 513b459370527f6bbe9aaa5959c711d495487065 --- dist/index.js | 53 +++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/dist/index.js b/dist/index.js index c11955c..9800afe 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9687,38 +9687,41 @@ var __webpack_exports__ = {}; const core = __nccwpck_require__(2186) const github = __nccwpck_require__(5438) -const run = async () => { - try { - const githubToken = core.getInput('github_token') +async function run() { + const githubToken = core.getInput('github_token') - if (!githubToken) throw Error(`input 'github_token' is required`) + if (!githubToken) throw Error(`input 'github_token' is required`) - const client = github.getOctokit(githubToken) - const owner = github.context.payload.repository.owner.login - const repo = github.context.payload.repository.name - const issue_number = github.context.payload.pull_request.number + const client = github.getOctokit(githubToken) + const owner = github.context.payload.repository.owner.login + const repo = github.context.payload.repository.name + const issue_number = github.context.payload.pull_request.number - const pr = github.context.payload.pull_request - const assignees = pr.assignees - const author = pr.user + const pr = github.context.payload.pull_request + const assignees = pr.assignees + const author = pr.user - if ( - (!assignees || assignees.length === 0) && - author.login !== 'dependabot[bot]' - ) { - await client.request( - `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, - { owner, repo, issue_number, assignees: [author.login] } - ) - } - } catch (error) { - console.log('Error:', error) - core.error(error) - core.setFailed(error) + console.log(`assignees: ${JSON.stringify(assignees, undefined, 2)}`) + console.log(`author: ${JSON.stringify(author, undefined, 2)}`) + + if ( + (!assignees || assignees.length === 0) && + (author.is_bot === false || (author.type && author.type !== 'Bot')) + ) { + await client.request( + `POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`, + { owner, repo, issue_number, assignees: [author.login] } + ) } } -run() +try { + run() +} catch (error) { + console.log('Error:', error) + core.error(error) + core.setFailed(error.message) +} })();