Add targetBranch output indicating what is the target branch

Depending on type of the "source" event target branch output
indicates either the direct target branch (for pushes)
or targetBranch for the PR (for Pull Request)
3 files changed
tree: c890622f2dbcd957a1374e1e44f534db05c8e101
  1. .github/
  2. __tests__/
  3. dist/
  4. src/
  5. .eslintignore
  6. .eslintrc.json
  7. .gitignore
  8. .pre-commit-config.yaml
  9. .prettierignore
  10. .prettierrc.json
  11. action.yml
  12. jest.config.js
  13. LICENSE
  14. package-lock.json
  15. package.json
  16. README.md
  17. tsconfig.json
  18. yamllint-config.yml
README.md

Get Workflow Origin action

Table of Contents generated with DocToc

Context and motivation

Get Workflow Origin is an action that provides information about the pull requests that triggered the workflow for the pull_request and pull_request_review events or for the workflow_run event that is triggered by one of those events.

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

Inputs

InputRequiredDefaultComment
tokenyesThe github token passed from ${{ secrets.GITHUB_TOKEN }}
sourceRunIdnoIn case of ‘workflow_run’ event it should be set to ${{ github.event.workflow_run.id }}

Outputs

OutputNo sourceRunId specifiedThe sourceRunId set to ${{ github.event.workflow_run.id }}
sourceHeadRepoCurrent repository. Format: owner/repoRepository of the run that triggered this workflow_run. Format: owner/repo
sourceHeadBranchCurrent branch.Branch of the run that triggered this workflow_run. Might be forked repo, if it is a pull_request.
sourceHeadShaCurrent commit SHA: {{ github.sha }}Commit sha of the run that triggered this workflow_run.
mergeCommitShaMerge commit SHA if PR-triggered event.Merge commit SHA if PR-triggered event.
targetCommitShaTarget commit SHA (merge if present, otherwise source).Target commit SHA (merge if present, otherwise source).
pullRequestNumberNumber of the associated Pull Request (if PR triggered)Number of the associated Pull Request (if PR triggered)
pullRequestLabelsStringified 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)
targetBranchTarget branch of the pull request or target branch for pushTarget branch of the pull request or target branch for push
sourceEventCurrent event: ${{ github.event }}Event of the run that triggered this workflow_run

Examples

Workflow Run event

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 }}
      targetBranch: ${{ steps.source-run-info.outputs.targetBranch }}
      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

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 }}
      targetBranch: ${{ steps.source-run-info.outputs.targetBranch }}
      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

It is highly recommended tu use pre commit. The pre-commits installed via pre-commit tool handle automatically linting (including automated fixes) as well as building and packaging Javascript index.js from the main.ts Typescript code, so you do not have to run it yourself.

License

MIT License covers the scripts and documentation in this project.