peter-evans / slash-command-dispatch

A GitHub action that facilitates "ChatOps" by creating repository dispatch events for slash commands
MIT License
570 stars 53 forks source link

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

Closed lilinghai closed 3 years ago

lilinghai commented 3 years ago

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 commented 3 years ago

Hi @lilinghai

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

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 commented 3 years ago

Closing this for now.

asford commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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/pull_request_status_action@v1.0.0
       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/pull_request_status_action@v1.0.0
       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/setup-terraform@v1.2.0
      - name: Terraform Init
        id: init
        run: terraform init
      - name: Terraform Apply
        id: apply
        run: terraform apply

      - name: Set PR Status
        if: ${{ always() }}
        uses: niteoweb/pull_request_status_action@v1.0.0
        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`,
        })