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/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: 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 diff --git a/src/option.ts b/src/option.ts new file mode 100644 index 0000000..6042850 --- /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(): number { + 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 + } +}