0

We have a check that runs on PRs:

name: check_time
on:
  pull_request:
    types:
      - synchronize
      - opened
      - reopened
  issue_comment:
    types:
      - created
jobs:
  check_time:
    runs-on: ubuntu-latest
    # run this if this event is not triggered by comment created (i.e. triggered by PR opened)
    # or run this if the comment was created on a PR
    if: ${{ !github.event.comment || (github.event.issue.pull_request && contains(github.event.comment.body, '/check_time')) }}
    steps:
    ...

We'd like to be able to trigger this check on a schedule. So, every so often, for every open pull request, have this check rerun. Here is the attempted implementation below:

name: trigger_check_time
on:
  workflow_dispatch:
    inputs:
      reason:
        description: 'Manual trigger of check-time'
        required: false
  schedule:
    - cron: '5 20 * * *'

jobs:
  provide_prs_json:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set_matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v3

      - name: List Pull Requests
        id: list_prs
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const response = await github.rest.pulls.list({
              owner: 'xxxx',
              repo: 'yyyy',
            });
            const prs = response.data.map(pr => pr.number);
            console.log(`found ${prs.length} prs: ${JSON.stringify(prs)}`);
            return prs;

      - name: Set Matrix
        id: set_matrix
        run: echo ::set-output name=matrix::'${{ steps.list_prs.outputs.result }}'

  dispatch_event:
    needs: provide_prs_json
    runs-on: ubuntu-latest
    strategy:
      # the real magic happens here - create dynamic matrix from the json
      matrix:
        pr: ${{ fromJson(needs.provide_prs_json.outputs.matrix) }}
    steps:
      - name: Comment PR
        id: comment_pr
        uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          result-encoding: string
          script: |
            try {
              github.rest.issues.createComment({
                owner: 'xxxx',
                repo: 'yyyy',
                issue_number: ${{ matrix.pr }},
                body: "/check_time",
              });
            } catch (e) {}

The above all works fine in the sense that it finds all the PRs and comments in their threads but the comment does not trigger the check_time workflow to run. I assume this is because events github-actions do not trigger other events? Is there any way to comment as a user instead?

I also looked at using workflow_dispatch event, but not sure how to have the check_time workflow run in the context of each PR (i.e. if the check_time workflow fails, the PRs should no longer be mergeable).

riQQ
  • 9,878
  • 7
  • 49
  • 66
istrau2
  • 337
  • 5
  • 15
  • Have you tried using a PAT instead of the default `GITHUB_TOKEN`? See https://stackoverflow.com/questions/60418323/triggering-a-new-workflow-from-another-workflow – riQQ Jun 16 '22 at 00:53
  • Try `workflow_run`: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run – rethab Jun 16 '22 at 06:00
  • @riQQ thanks for the response, that actually seems to have triggered the run. Unfortunately, the run is not affecting the PR (i.e. whether it succeeds or fails does not affect if the PR can be merged). It just kind of runs without having any affect. – istrau2 Jun 16 '22 at 08:32
  • @istrau2 you can manually create a checkrun, see https://stackoverflow.com/a/67516774 – riQQ Jun 17 '22 at 16:19
  • @istrau2 I think it's because the context of the trigger is the default branch instead of the Pull Request that triggered the comment. I wish Github would support naturally using the PR Context which triggered the workflow instead of the default branch. – Mark Han Oct 26 '22 at 03:43

0 Answers0