This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/airflow-cancel-workflow-runs.git
commit 0ba85cabd56ddb3e40adf6dc76ae4bf7ee88461e Author: Jarek Potiuk <[email protected]> AuthorDate: Wed Oct 7 18:22:15 2020 +0200 Added optional message when cancelling workflow. (#6) --- README.md | 18 ++++++++++-------- action.yml | 4 ++++ dist/index.js | 13 ++++++++----- src/main.ts | 11 ++++++++--- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7946ea9..a2fcca2 100644 --- a/README.md +++ b/README.md @@ -105,14 +105,15 @@ and `schedule` events are no longer needed. ## Inputs -| Input | Required | Default | Comment | -|------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `token` | yes | | The github token passed from `${{ secrets.GITHUB_TOKEN }}` | -| `cancelMode` | no | `duplicates` | The mode to run cancel on. The available options are `duplicates`, `self`, `failedJobs`, `namedJobs` | -| `sourceRunId` | no | | Useful only in `workflow_run` triggered events. It should be set to the id of the workflow triggering the run `${{ github.event.workflow_run.id }}` in case cancel operation should cancel the source workflow. | -| `notifyPRCancel` | no | | Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why they are being cancelled. | -| `notifyPRMessageStart` | no | | Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the message specified at the start of the workflow - adding the link to the triggered workflow_run. | -| `jobNameRegexps` | no | | An array of job name regexps. Only runs containing any job name matching any of of the regexp in this array are considered for cancelling in `failedJobs` and `namedJobs` cancel modes. | +| Input | Required | Default | Comment | +|-------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `token` | yes | | The github token passed from `${{ secrets.GITHUB_TOKEN }}` | +| `cancelMode` | no | `duplicates` | The mode to run cancel on. The available options are `duplicates`, `self`, `failedJobs`, `namedJobs` | +| `sourceRunId` | no | | Useful only in `workflow_run` triggered events. It should be set to the id of the workflow triggering the run `${{ github.event.workflow_run.id }}` in case cancel operation should cancel the source workflow. | +| `notifyPRCancel` | no | | Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why they are being cancelled. | +| `notifyPRCancelMessage` | no | | Optional cancel message to use instead of the default one when notifyPRCancel is true. | +| `notifyPRMessageStart` | no | | Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the message specified at the start of the workflow - adding the link to the triggered workflow_run. | +| `jobNameRegexps` | no | | An array of job name regexps. Only runs containing any job name matching any of of the regexp in this array are considered for cancelling in `failedJobs` and `namedJobs` cancel modes. | The job cancel modes work as follows: @@ -380,6 +381,7 @@ jobs: cancelMode: duplicates token: ${{ secrets.GITHUB_TOKEN }} notifyPRCancel: true + notifyPRCancelMessage: Cancelled because image building failed. notifyPRMessageStart: | Note! The Docker Images for the build are prepared in a separate workflow, that you will not see in the list of checks. diff --git a/action.yml b/action.yml index 932b45f..0666499 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,10 @@ inputs: Boolean. If set to true, it notifies the cancelled PRs with a comment containing reason why they are being cancelled. required: false + notifyPRCancelMessage: + description: | + Optional cancel message to use instead of the default one when notifyPRCancel is true. + required: false notifyPRMessageStart: description: | Only for workflow_run events triggered by the PRs. If not empty, it notifies those PRs with the diff --git a/dist/index.js b/dist/index.js index a7796dd..7f28bf7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1756,7 +1756,7 @@ function findAndCancelRuns(octokit, selfRunId, sourceWorkflowId, sourceRunId, ow } for (const pullRequestNumber of pullRequestToNotify) { const selfWorkflowRunUrl = `https://github.com/${owner}/${repo}/actions/runs/${selfRunId}`; - yield addCommentToPullRequest(octokit, owner, repo, pullRequestNumber, `[The Build Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`); + yield addCommentToPullRequest(octokit, owner, repo, pullRequestNumber, `[The Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}`); } core.info('\n###### Finished cancelling runs ##########\n'); } @@ -1832,7 +1832,7 @@ function getOrigin(octokit, runId, owner, repo) { ]; }); } -function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRMessageStart, jobNameRegexps) { +function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRCancelMessage, notifyPRMessageStart, jobNameRegexps) { return __awaiter(this, void 0, void 0, function* () { core.info('\n###################################################################################\n'); core.info(`All parameters: owner: ${owner}, repo: ${repo}, run id: ${sourceRunId}, ` + @@ -1842,7 +1842,9 @@ function performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, own let reason = ''; if (cancelMode === CancelMode.SELF) { core.info(`# Cancelling source run: ${sourceRunId} for workflow ${sourceWorkflowId}.`); - reason = `The job has been cancelled by another workflow.`; + reason = notifyPRCancelMessage + ? notifyPRCancelMessage + : `The job has been cancelled by another workflow.`; } else if (cancelMode === CancelMode.FAILED_JOBS) { core.info(`# Cancel all runs for workflow ${sourceWorkflowId} where job names matching ${jobNameRegexps} failed.`); @@ -1876,6 +1878,7 @@ function run() { const eventName = getRequiredEnv('GITHUB_EVENT_NAME'); const cancelMode = core.getInput('cancelMode') || CancelMode.DUPLICATES; const notifyPRCancel = (core.getInput('notifyPRCancel') || 'false').toLowerCase() === 'true'; + const notifyPRCancelMessage = core.getInput('notifyPRCancelMessage'); const notifyPRMessageStart = core.getInput('notifyPRMessageStart'); const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId; const jobNameRegexpsString = core.getInput('jobNameRegexps'); @@ -1923,8 +1926,8 @@ function run() { body: `${notifyPRMessageStart} [The workflow run](${selfWorkflowRunUrl})` }); } - const cancelledRuns = yield performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRMessageStart, jobNameRegexps); - core.setOutput('cancelledRuns', JSON.stringify(cancelledRuns)); + const cancelledRuns = yield performCancelJob(octokit, selfRunId, sourceWorkflowId, sourceRunId, owner, repo, headRepo, headBranch, sourceEventName, cancelMode, notifyPRCancel, notifyPRCancelMessage, notifyPRMessageStart, jobNameRegexps); + verboseOutput('cancelledRuns', JSON.stringify(cancelledRuns)); }); } run() diff --git a/src/main.ts b/src/main.ts index 056ccb8..44ef1bc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -444,7 +444,7 @@ async function findAndCancelRuns( owner, repo, pullRequestNumber, - `[The Build Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}` + `[The Workflow run](${selfWorkflowRunUrl}) is cancelling this PR. ${reason}` ) } core.info( @@ -568,6 +568,7 @@ async function performCancelJob( sourceEventName: string, cancelMode: CancelMode, notifyPRCancel: boolean, + notifyPRCancelMessage: string, notifyPRMessageStart: string, jobNameRegexps: string[] ): Promise<number[]> { @@ -587,7 +588,9 @@ async function performCancelJob( core.info( `# Cancelling source run: ${sourceRunId} for workflow ${sourceWorkflowId}.` ) - reason = `The job has been cancelled by another workflow.` + reason = notifyPRCancelMessage + ? notifyPRCancelMessage + : `The job has been cancelled by another workflow.` } else if (cancelMode === CancelMode.FAILED_JOBS) { core.info( `# Cancel all runs for workflow ${sourceWorkflowId} where job names matching ${jobNameRegexps} failed.` @@ -643,6 +646,7 @@ async function run(): Promise<void> { (core.getInput('cancelMode') as CancelMode) || CancelMode.DUPLICATES const notifyPRCancel = (core.getInput('notifyPRCancel') || 'false').toLowerCase() === 'true' + const notifyPRCancelMessage = core.getInput('notifyPRCancelMessage') const notifyPRMessageStart = core.getInput('notifyPRMessageStart') const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId const jobNameRegexpsString = core.getInput('jobNameRegexps') @@ -732,11 +736,12 @@ async function run(): Promise<void> { sourceEventName, cancelMode, notifyPRCancel, + notifyPRCancelMessage, notifyPRMessageStart, jobNameRegexps ) - core.setOutput('cancelledRuns', JSON.stringify(cancelledRuns)) + verboseOutput('cancelledRuns', JSON.stringify(cancelledRuns)) } run()
