From 88a20dcdff9fd270012e2f45435b4df71a717165 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Tue, 5 Mar 2024 23:42:08 +0900 Subject: [PATCH 1/4] extract options --- src/option.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/option.ts diff --git a/src/option.ts b/src/option.ts new file mode 100644 index 0000000..f3c3707 --- /dev/null +++ b/src/option.ts @@ -0,0 +1,31 @@ +import { getInput, setFailed } from '@actions/core' +import { context } from '@actions/github' + +type Option = { + token: string + prNumber: number +} + +function getPrNumber() { + if (typeof context.payload.pull_request?.number === 'number') { + return context.payload.pull_request.number + } + + const url = getInput('pr_url', { required: false }) + const numberFromUrl = Number(url.substring(url.lastIndexOf('/') + 1)) + return numberFromUrl +} + +export function getOption(): Option { + const token = getInput('github_token', { required: true }) + + const prNumber = getPrNumber() + if (isNaN(prNumber) || prNumber === 0) { + setFailed('pr number is not set properly') + } + + return { + token, + prNumber + } +} From 27ee641442db1142f9a368c03de139e30e948147 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Tue, 5 Mar 2024 23:42:32 +0900 Subject: [PATCH 2/4] fix input to pass pr_url instead of calculated pr_number This change makes workflow scripts simpler --- action.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index 166443d..0180a42 100644 --- a/action.yml +++ b/action.yml @@ -13,10 +13,12 @@ inputs: description: 'a github access token' required: true default: ${{ github.token }} - pr_number: + pr_url: description: - 'a target pr number. This is set for events triggered by issue_comment' - required: false + 'the target pr url. This is used for taking pr number in workflows + triggered by "issue_comment" event' + required: true + default: ${{ github.event.issue.pull_request.url }} # Define your outputs here. outputs: From 45eff8c3cb2267d91eb45ba16c9bcdf81630bac6 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Tue, 5 Mar 2024 23:44:27 +0900 Subject: [PATCH 3/4] apply this change --- .github/workflows/test-action.yml | 12 --------- dist/index.js | 42 +++++++++++++++++++++++++------ src/main.ts | 10 ++------ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index af1848a..667fba0 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -19,21 +19,9 @@ jobs: id: checkout uses: actions/checkout@v4 - - name: Get PR_NUMBER for issue_comment triggered events - id: get-pr-number - if: github.event.issue.pull_request - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - PR_URL="${{ github.event.issue.pull_request.url }}" - PR_NUM=${PR_URL##*/} - echo "pr-number=$PR_NUM" >> "$GITHUB_OUTPUT" - - name: Test Local Action id: test-action uses: ./ - with: - pr_number: ${{ steps.get-pr-number.outputs.pr-number }} - name: Print Output id: output diff --git a/dist/index.js b/dist/index.js index b2f2639..90a33ae 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29008,6 +29008,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.run = void 0; const core = __importStar(__nccwpck_require__(9093)); const github_1 = __nccwpck_require__(5942); +const option_1 = __nccwpck_require__(6335); const uniqueStringArray = (texts) => { if (texts.length === 0) return []; @@ -29026,13 +29027,7 @@ const uniqueStringArray = (texts) => { */ async function run() { try { - const prNumber = github_1.context.payload.pull_request?.number || - Number(core.getInput('pr_number', { required: false })); - if (isNaN(prNumber) || prNumber === 0) { - core.setFailed('pr number is not set properly'); - return; - } - const token = core.getInput('github_token', { required: true }); + const { token, prNumber } = (0, option_1.getOption)(); const octokit = (0, github_1.getOctokit)(token); const owner = github_1.context.repo.owner; const repo = github_1.context.repo.repo; @@ -29072,6 +29067,39 @@ the number of the comments is ${comments.length} and the review comments is ${re exports.run = run; +/***/ }), + +/***/ 6335: +/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.getOption = void 0; +const core_1 = __nccwpck_require__(9093); +const github_1 = __nccwpck_require__(5942); +function getPrNumber() { + if (typeof github_1.context.payload.pull_request?.number === 'number') { + return github_1.context.payload.pull_request.number; + } + const url = (0, core_1.getInput)('pr_url', { required: false }); + const numberFromUrl = Number(url.substring(url.lastIndexOf('/') + 1)); + return numberFromUrl; +} +function getOption() { + const token = (0, core_1.getInput)('github_token', { required: true }); + const prNumber = getPrNumber(); + if (isNaN(prNumber) || prNumber === 0) { + (0, core_1.setFailed)('pr number is not set properly'); + } + return { + token, + prNumber + }; +} +exports.getOption = getOption; + + /***/ }), /***/ 9491: diff --git a/src/main.ts b/src/main.ts index b0ea662..28d0ac2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import { getOctokit, context } from '@actions/github' +import { getOption } from './option' const uniqueStringArray = (texts: string[]): string[] => { if (texts.length === 0) return [] @@ -22,15 +23,8 @@ const uniqueStringArray = (texts: string[]): string[] => { */ export async function run(): Promise { try { - const prNumber = - context.payload.pull_request?.number || - Number(core.getInput('pr_number', { required: false })) - if (isNaN(prNumber) || prNumber === 0) { - core.setFailed('pr number is not set properly') - return - } + const { token, prNumber } = getOption() - const token = core.getInput('github_token', { required: true }) const octokit = getOctokit(token) const owner = context.repo.owner const repo = context.repo.repo From e2d4f31bca0f7293fa1e1de4b8acc173097ee550 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Tue, 5 Mar 2024 23:51:42 +0900 Subject: [PATCH 4/4] add type annotation --- src/option.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/option.ts b/src/option.ts index f3c3707..6042850 100644 --- a/src/option.ts +++ b/src/option.ts @@ -6,7 +6,7 @@ type Option = { prNumber: number } -function getPrNumber() { +function getPrNumber(): number { if (typeof context.payload.pull_request?.number === 'number') { return context.payload.pull_request.number }