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

"Checks" can't be updated when using comment trigger #87

Closed
lilinghai opened this issue Oct 10, 2020 · 5 comments
Closed

"Checks" can't be updated when using comment trigger #87

lilinghai opened this issue Oct 10, 2020 · 5 comments

Comments

@lilinghai
Copy link

lilinghai commented Oct 10, 2020

The "Checks" can't be updated when I use slash-command-dispatch to trigger workflow,but it still displays the "pull request trigger" workflow results. This is important for the participants to know the result of workflow results from "Checks". Is there a way to solve it?

企业微信截图_2cc5c108-57dd-44ec-a911-0367a85a2878
企业微信截图_108ef42a-3b16-4a9e-b21a-86d21cdbd0ac

@peter-evans
Copy link
Owner

Hi @lilinghai

GitHub Actions will only create Checks in pull requests for the following workflow types:

  • pull_request
  • pull_request_review
  • pull_request_review_comment
  • pull_request_target
  • push

Unfortunately, you cannot add a Check with the result of a repository_dispatch event.

Does that answer your question? Let me know if I've not understood what you are trying to do.

@peter-evans
Copy link
Owner

Closing this for now.

@asford
Copy link

asford commented Oct 19, 2020

@lilinghai @peter-evans It's possible to manually update the commit status or checks status as part of the workflow triggered by the slash command. We use:

https://github.com/niteoweb/pull_request_status_action

and

https://github.com/marketplace/actions/github-checks

@peter-evans
Copy link
Owner

@asford That's interesting. Sounds like you are creating your own custom status check on the PR and updating it when the command completes?

Would you be willing to share your workflows so I can see how you have used it with slash-command-dispatch?

@asford
Copy link

asford commented Oct 20, 2020

Of course, here's a lightly edited example to clarify how the workflow is used.

The workflow implements a poor-souls GitOps flow for terraform. We open PRs for terraform changes, use the standard branch protection features (review, other checks, etc) to prepare the PR and merge via /terraform-apply. The /terraform-apply slash command checks and if-and-only-if the PR is mergeable it runs terraform apply and merges. This then shows up as a terraform-apply-command status check on the commit, though this is primarily informative for our application.

A similar workflow pattern could implement slash-command mediated checks. You could even use these for branch-protection by specifying the <command-name>-command workflow as a required check.

name: pull-request-slash-commands
on:
  issue_comment:
    types: [created]
jobs:
  pull-request-slash-commands:
    runs-on: ubuntu-latest
    steps:
      - name: Dispatch PR Slash Commands
        uses: peter-evans/slash-command-dispatch@v2
        with:
          issue-type: pull-request
          token: ${{ secrets.ACTIONS_ACCESS_TOKEN }}
          permission: maintain
          commands: |
            terraform-apply
name: terraform-apply-command
on:
  repository_dispatch:
    types: [terraform-apply-command]
jobs:
  # Set mergeable_state docs at:
  # https://github.com/octokit/octokit.net/issues/1763
  # https://docs.github.com/en/free-pro-team@latest/graphql/reference/enums#mergestatestatus
  # Only merge on green-and-clean.
  #
  # The comparisons below are a bit funky, as the 'true'/'false' output is returned
  # as a string. You *must* compare against the string literal, rather than relying
  # on boolean operators.
  check-mergeable:
    runs-on: ubuntu-18.04
    outputs:
      mergeable: ${{
          (
            github.event.client_payload.pull_request.mergeable_state == 'clean' ||
            github.event.client_payload.pull_request.mergeable_state == 'has_hooks'
          )
        }}
    steps:
     - name: Set PR Status Pending
       uses: niteoweb/[email protected]
       with:
         pr_number: ${{ github.event.client_payload.pull_request.number }}
         state: "pending"
         repository: ${{ github.repository }}
         context: ${{ github.workflow }}
         target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
       env:
         GITHUB_TOKEN: ${{ github.token }}

  report-no-merge:
    needs: check-mergeable
    if: ${{ needs.check-mergeable.outputs.mergeable == 'false' }}
    runs-on: ubuntu-18.04
    steps:
     - name: Create Plan Comment
       uses: peter-evans/create-or-update-comment@v1
       with:
         issue-number: ${{ github.event.client_payload.pull_request.number }}
         body: |
           I'm sorry @${{ github.actor }}, I'm afraid I can't do that.
           I think you know what the problem is just as well as I do.
           This PR is too important for me to allow you to jeopardize it.
     - name: Set PR Status Error
       uses: niteoweb/[email protected]
       with:
         pr_number: ${{ github.event.client_payload.pull_request.number }}
         state: "failure"
         repository: ${{ github.repository }}
         context: ${{ github.workflow }}
         target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
       env:
         GITHUB_TOKEN: ${{ github.token }}

  apply-and-merge:
    needs: check-mergeable
    if: ${{ needs.check-mergeable.outputs.mergeable == 'true' }}
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout Merge Commit
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.client_payload.pull_request.merge_commit_sha }}
          
      - name: Setup Terraform
        uses: hashicorp/[email protected]
      - name: Terraform Init
        id: init
        run: terraform init
      - name: Terraform Apply
        id: apply
        run: terraform apply
        
      - name: Set PR Status
        if: ${{ always() }}
        uses: niteoweb/[email protected]
        with:
          pr_number: ${{ github.event.client_payload.pull_request.number }}
          state: ${{ job.status }}
          repository: ${{ github.repository }}
          context: ${{ github.workflow }}
          target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Create Apply Comment
        if: ${{ always() }}
        uses: peter-evans/create-or-update-comment@v1
        with:
          issue-number: ${{ github.event.client_payload.pull_request.number }}
          body: |
            ## Terraform
            #### ⚙️ Init  `${{ steps.init.outcome }}`
            #### 🏗️ Apply `${{ steps.apply.outcome }}`
            <details><summary>stdout</summary>
            ```terraform
            ${{ steps.apply.outputs.stdout }}
            ```
            </details>
            <details><summary>stderr</summary>
            ```terraform
            ${{ steps.apply.outputs.stderr }}
            ```
            </details>
            
            Workflow: [${{ github.workflow }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
            Action: `${{ github.event_name }}`
            Pusher: @${{ github.actor }}

      - name: "Merge pull request"
        uses: "actions/github-script@v2"
        with:
          script: |
            const pull_request = context.payload.client_payload.pull_request
            const repository = context.repo
            await github.pulls.merge({
              owner: repository.owner,
              repo: repository.repo,
              pull_number: pull_request.number,
              merge_method: "squash",
              commit_title: `${pull_request.title} (${pull_request.number})\n`,
            })

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