Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I trigger it on the branch of the PR? #100

Closed
dunklesToast opened this issue Mar 8, 2021 · 6 comments
Closed

How can I trigger it on the branch of the PR? #100

dunklesToast opened this issue Mar 8, 2021 · 6 comments

Comments

@dunklesToast
Copy link

Hi,

I'm trying to implement a custom workflow which runs on the branch of the PR where the command was triggered.

My trigger looks like this:

name: Dispatcher
on:
  issue_comment:
    types: [created]

jobs:
  slashCommandDispatch:
    runs-on: ubuntu-latest
    if: ${{ github.event.issue.pull_request }}
    steps:
      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v2
        with:
          token: ${{ secrets.REPO_PAT }}
          allow-edits: true
          reaction-token: ${{ secrets.REPO_PAT }}
          permission: write
          dispatch-type: workflow
          ref: ${{ env.GITHUB_REF }}
          commands: |
            test

If I write /test in my PR it'll trigger the dispatcher and also the action which listens on the Event but it will alway use the main branch. Is there a way to use the PR branch? As you can see I tried using the ref argument but thats not recognized.

My custom action to be triggered looks like this:

name: Testing

on:
  workflow_dispatch:

jobs:
  jest:
    name: Run Jest
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

I verfied that the commit hash was wrong in the GitHub UI:
image
This commit is from the main branch and not from my PR:
image

Expected behaviour would be, that it checks out the branch which is associated with the Pull Request where I typed the command. Is this even possible?

@peter-evans
Copy link
Owner

Hi @dunklesToast

It's awkward to do this with workflow_dispatch workflows because those events don't support being able to send a client_payload. To make it work with workflow_dispatch you would need to send it more information using slash-command parameters. e.g. /test branch=my-pr-branch.

I recommend switching your workflow to repository_dispatch and remove the dispatch-type: workflow line from your configuration. This will enable the action to send the pull request context with the dispatch request.

I recommend looking at the demo to see how to setup your command workflow. The /black command executes this workflow. In the workflow you can see here how it fetches what branch to target from the client_context.

@dunklesToast
Copy link
Author

Okay I found out that GitHub doesn't sync the UI with the branch you actually checkout. UI looks like this
image
The Hash is the newest in the main branch but it does checkout my branch. I guess there is no way to make the Action Results show up under the PR?

Like this:
image

@peter-evans
Copy link
Owner

Just to back up a bit, how did you resolve your issue to get this working. Did you follow my recommendation? Or you found another way?

To answer your second question, I think it's possible to have a check show up under the PR for the slash command but I've not attempted it myself yet. See this conversation for some sample code.

@reintroducing
Copy link

reintroducing commented Mar 17, 2021

I am having what I guess you can call a similar issue. I have a workflow file named slash-command-dispatch.yml that looks like this:

name: Slash Command Dispatch

on:
  issue_comment:
    types: [created]

jobs:
  slash-command-dispatch:
    runs-on: selfhosted-fe-platform
    steps:
      - name: Parse Slash Command
        uses: peter-evans/slash-command-dispatch@v2
        with:
          token: ${{ secrets.GH_TOKEN }}
          reaction-token: ${{ secrets.GH_TOKEN }}
          config: >
            [
              {
                "command": "canary",
                "permission": "none",
                "issue_type": "pull-request"
              }
            ]

I then have a canary-command.yml file that should be getting called when I comment /canary in a PR, but it does not seem to be firing at all:

name: canary-command

on:
  repository_dispatch:
    types: [canary-command]

jobs:
  canary:
    runs-on: selfhosted-fe-platform
    env:
      NPM_TOKEN: '${{ secrets.NPM_TOKEN }}'
    steps:
      - name: Check out repo
        uses: actions/checkout@master
      - name: Set up node
        uses: actions/setup-node@v2
        with:
          node-version: '12'
      - name: Install dependencies
        run: npm ci
      - name: Create npm package output
        run: npm run build
      - name: Create canary release
        run: npx auto canary

secrets.GH_TOKEN is the personal access token of a bot user that has admin rights in the repo. Both of these workflows exist in the same repo.

What am I missing here?

@reintroducing
Copy link

@peter-evans Oh, in reading the documentation a bit closer, it appears that repository dispatch will only execute workflows from the default branch (master in my case), but I'm currently working on these in a PR. Do I need to merge the above into master first before my slash commands will work?

@reintroducing
Copy link

Disregard the noise here, that WAS indeed my problem, once on master, all runs well. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants