This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/airflow-get-workflow-origin.git
commit ae55e40e2356a057dd482be44b205db8e4126658 Author: Jarek Potiuk <[email protected]> AuthorDate: Sat Oct 24 11:58:31 2020 +0200 Adds pullRequestLabels output --- README.md | 108 +++++++++++++++++++++++++++++++++++++++++++++++++--------- dist/index.js | 5 ++- src/main.ts | 7 ++-- 3 files changed, 96 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 1af4d6f..601c99e 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ - [Inputs and outputs](#inputs-and-outputs) - [Inputs](#inputs) - [Outputs](#outputs) +- [Examples](#examples) + - [Workflow Run event](#workflow-run-event) + - [Workflow Run event](#workflow-run-event-1) - [Development environment](#development-environment) - [License](#license) @@ -20,13 +23,20 @@ # Context and motivation -Get Workflow Origin is an action that provides missing information for `workflow_run` events -Often in the `workflow_run` event you want to get more information about the source run than the -one provided directly via GitHub context. For example you would like to know what is the merge -commit generated by pull request in case the workflow was triggered by a pull request. +Get Workflow Origin is an action that provides information about the pull requests that triggered the +workflow for the `workflow_run` and `pull_request` events. -This action provides outputs that give the missing information. You should add this action as first -one in the "workflow_run" event and then you will be able to use those outputs. +Often in those events you want to get more information about the source run than the +one provided directly via GitHub context. + +For example, you would like to know what is the merge commit generated by pull request in case +the workflow is triggered by a pull request, or labels associated with the Pull Request. + +This action provides outputs that give that information. You should add this action as first +one in your workflow and then you will be able to use those outputs using 'needs' dependency. + +The `sourceRunId` input should not be specified in case of the `pull_request` event, but it should +be set to `${{ github.event.workflow_run.id }}` in case of the `workflow_run` event. # Inputs and outputs @@ -35,19 +45,85 @@ one in the "workflow_run" event and then you will be able to use those outputs. | Input | Required | Default | Comment | |-------------------------|----------|--------------|-----------------------------------------------------------------------------------------------------| | `token` | yes | | The github token passed from `${{ secrets.GITHUB_TOKEN }}` | -| `sourceRunId` | yes | | It should be set to the id of the workflow triggering the run `${{ github.event.workflow_run.id }}` | +| `sourceRunId` | no | | In case of 'workflow_run' event it should be set to `${{ github.event.workflow_run.id }}` | ## Outputs -| Output | No `sourceRunId` specified | The `sourceRunId` set to `${{ github.event.workflow_run.id }}` | -|---------------------|---------------------------------------------------------|------------------------------------------------------------------------------------------------------| -| `sourceHeadRepo` | Current repository. Format: `owner/repo` | Repository of the run that triggered this `workflow_run`. Format: `owner/repo` | -| `sourceHeadBranch` | Current branch. | Branch of the run that triggered this `workflow_run`. Might be forked repo, if it is a pull_request. | -| `sourceHeadSha` | Current commit SHA: `{{ github.sha }}` | Commit sha of the run that triggered this `workflow_run`. | -| `mergeCommitSha` | Merge commit SHA if PR-triggered event. | Merge commit SHA if PR-triggered event. | -| `targetCommitSha` | Target commit SHA (merge if present, otherwise source). | Target commit SHA (merge if present, otherwise source). | -| `pullRequestNumber` | Number of the associated Pull Request (if PR triggered) | Number of the associated Pull Request (if PR triggered) | -| `sourceEvent` | Current event: ``${{ github.event }}`` | Event of the run that triggered this `workflow_run` | +| Output | No `sourceRunId` specified | The `sourceRunId` set to `${{ github.event.workflow_run.id }}` | +|---------------------|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| +| `sourceHeadRepo` | Current repository. Format: `owner/repo` | Repository of the run that triggered this `workflow_run`. Format: `owner/repo` | +| `sourceHeadBranch` | Current branch. | Branch of the run that triggered this `workflow_run`. Might be forked repo, if it is a pull_request. | +| `sourceHeadSha` | Current commit SHA: `{{ github.sha }}` | Commit sha of the run that triggered this `workflow_run`. | +| `mergeCommitSha` | Merge commit SHA if PR-triggered event. | Merge commit SHA if PR-triggered event. | +| `targetCommitSha` | Target commit SHA (merge if present, otherwise source). | Target commit SHA (merge if present, otherwise source). | +| `pullRequestNumber` | Number of the associated Pull Request (if PR triggered) | Number of the associated Pull Request (if PR triggered) | +| `pullRequestLabels` | Stringified JSON array of Labels of the associated Pull Request (if PR triggered) | Stringified JSON array of Labels of the associated Pull Request (if PR triggered) | +| `sourceEvent` | Current event: ``${{ github.event }}`` | Event of the run that triggered this `workflow_run` | + +# Examples + +### Workflow Run event + +```yaml +name: Get information +on: + pull_request: + branches: ['main'] + +jobs: + get-info: + name: "Get information about the source run" + runs-on: ubuntu-latest + outputs: + sourceHeadRepo: ${{ steps.workflow-run-info.outputs.sourceHeadRepo }} + sourceHeadBranch: ${{ steps.workflow-run-info.outputs.sourceHeadBranch }} + sourceHeadSha: ${{ steps.workflow-run-info.outputs.sourceHeadSha }} + mergeCommitSha: ${{ steps.workflow-run-info.outputs.mergeCommitSha }} + targetCommitSha: ${{ steps.workflow-run-info.outputs.targetCommitSha }} + pullRequestNumber: ${{ steps.workflow-run-info.outputs.pullRequestNumber }} + pullRequestLabels: ${{ steps.workflow-run-info.outputs.pullRequestLabels }} + sourceEvent: ${{ steps.workflow-run-info.outputs.sourceEvent }} + steps: + - name: "Get information about the current run" + uses: potiuk/get-workflow-origin@v1_1 + id: workflow-run-info + with: + token: ${{ secrets.GITHUB_TOKEN }} +``` + + +### Workflow Run event + +```yaml +name: Build +on: + workflow_run: + workflows: ['CI'] + types: ['requested'] + +jobs: + get-info: + name: "Get information about the source run" + runs-on: ubuntu-latest + outputs: + sourceHeadRepo: ${{ steps.source-run-info.outputs.sourceHeadRepo }} + sourceHeadBranch: ${{ steps.source-run-info.outputs.sourceHeadBranch }} + sourceHeadSha: ${{ steps.source-run-info.outputs.sourceHeadSha }} + mergeCommitSha: ${{ steps.source-run-info.outputs.mergeCommitSha }} + targetCommitSha: ${{ steps.source-run-info.outputs.targetCommitSha }} + pullRequestNumber: ${{ steps.source-run-info.outputs.pullRequestNumber }} + pullRequestLabels: ${{ steps.source-run-info.outputs.pullRequestLabels }} + sourceEvent: ${{ steps.source-run-info.outputs.sourceEvent }} + + steps: + - name: "Get information about the origin 'CI' run" + uses: potiuk/get-workflow-origin@v1_1 + id: source-run-info + with: + token: ${{ secrets.GITHUB_TOKEN }} + sourceRunId: ${{ github.event.workflow_run.id }}``` +``` + ## Development environment diff --git a/dist/index.js b/dist/index.js index a8818f2..f67f8c6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1546,15 +1546,14 @@ function run() { core.info(`Repository: ${repository}, Owner: ${owner}, Repo: ${repo}, ` + `Event name: ${eventName},` + `sourceWorkflowId: ${sourceWorkflowId}, sourceRunId: ${sourceRunId}, selfRunId: ${selfRunId}, `); - if (eventName !== 'workflow_run') { - throw Error(`This action is only useful in "workflow_run" triggered runs and you used it in ${eventName}`); - } const [headRepo, headBranch, sourceEventName, headSha, mergeCommitSha, pullRequest] = yield getOrigin(octokit, sourceRunId, owner, repo); verboseOutput('sourceHeadRepo', headRepo); verboseOutput('sourceHeadBranch', headBranch); verboseOutput('sourceHeadSha', headSha); verboseOutput('sourceEvent', sourceEventName); verboseOutput('pullRequestNumber', pullRequest ? pullRequest.number.toString() : ''); + const labelNames = pullRequest ? pullRequest.labels.map(x => x.name) : []; + verboseOutput('pullRequestLabels', JSON.stringify(labelNames)); verboseOutput('mergeCommitSha', mergeCommitSha); verboseOutput('targetCommitSha', pullRequest ? mergeCommitSha : headSha); }); diff --git a/src/main.ts b/src/main.ts index b516883..63d0c23 100644 --- a/src/main.ts +++ b/src/main.ts @@ -132,11 +132,6 @@ async function run(): Promise<void> { `sourceWorkflowId: ${sourceWorkflowId}, sourceRunId: ${sourceRunId}, selfRunId: ${selfRunId}, ` ) - if (eventName !== 'workflow_run') { - throw Error( - `This action is only useful in "workflow_run" triggered runs and you used it in ${eventName}` - ) - } const [ headRepo, headBranch, @@ -154,6 +149,8 @@ async function run(): Promise<void> { 'pullRequestNumber', pullRequest ? pullRequest.number.toString() : '' ) + const labelNames = pullRequest ? pullRequest.labels.map(x => x.name) : [] + verboseOutput('pullRequestLabels', JSON.stringify(labelNames)) verboseOutput('mergeCommitSha', mergeCommitSha) verboseOutput('targetCommitSha', pullRequest ? mergeCommitSha : headSha) }
