When I merge a pull request, my github action is getting a push event, there is no pull request event at all.
According to this doc: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-pull_request-workflow-when-a-pull-request-merges ,there should be a pull request event with closed type but my action is not getting a pull request event, only a push event. As a result, I cannot check "github.event.pull_request.merged" since the github.event object does not have the pull_request object in it at all.
Is this expected? What other way is there to detect when a PR is merged in the action? One solution I can think of is to check if the commit message contains the string "Merge pull request" but if the user modifies the default merge message and removes that string, then it wont work.
Thank you
Below is our workflow file. There are two jobs, one for handling commits and one for pull requests. The pull request job is not triggering when a PR is merged, only the commit job.
name: Github-Jira Integration
on:
[push, pull_request]
jobs:
commit:
runs-on: win10-machine
env:
JIRA_URL: ${{ secrets.JIRA_URL }}
JIRA_USER: ${{ secrets.JIRA_USER }}
JIRA_PWORD: ${{ secrets.JIRA_PWORD }}
GITHUB_EVENT: ${{ toJson(github.event) }}
if: github.event_name != 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
repository: our_repo
path: generic_tools
- name: Setting up Node.js Version
uses: actions/setup-node@v3
with:
node-version: 14
- name: Installing Node Modules
run: |
cd generic_tools/github-plugin
npm install
npm install -g ts-node
- name: Run
shell: cmd
run: |
ts-node generic_tools/github-plugin/src/app.ts "head_commit"
pull_request:
runs-on: win10-machine
env:
JIRA_URL: ${{ secrets.JIRA_URL }}
JIRA_USER: ${{ secrets.JIRA_USER }}
JIRA_PWORD: ${{ secrets.JIRA_PWORD }}
GITHUB_EVENT: ${{ toJson(github.event) }}
if: github.event_name == 'pull_request' || github.event.pull_request.merged == true
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
repository: our_repo
path: generic_tools
- name: Setting up Node.js Version
uses: actions/setup-node@v3
with:
node-version: 14
- name: Installing Node Modules
run: |
cd generic_tools/github-plugin
npm install
npm install -g ts-node
- name: Run
shell: cmd
run: |
ts-node generic_tools/github-plugin/src/app.ts "pull_request"
The pull_request event has a lot of activity types that could trigger it, enough that there is a caveat on which activity types actually trigger the event, if none are specified;
By default, a workflow only runs when a
pull_requestevent's activity type isopened,synchronize, orreopened. To trigger workflows by different activity types, use thetypeskeyword.
To add the closed event type, all you need to do is change
on:
[push, pull_request]
to
on:
push:
pull_request:
types:
# The 3 default types, if you still want them
- opened
- synchronize
- reopened
# As well as the closed type.
- closed
Note though that both events will independently trigger the workflow, so it'll still run the push event as well as the pull request event.
Replacing a pull_request trigger with a pull_request_target trigger instead is not a wise idea without fully embracing all the caveat differences between them, such as execution context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With