Help: PR status update in workflow invoked via comment

Howdy everyone :raising_hand_man:

Trying to create a workflow that will run some tests when a PR comment is left. The goal is to have the workflow show up on the PR as a status check as well. 

name: Functional Tests

on:
  issue_comment:
    types: [created]
  pull_request_review:
    types: [submitted]

jobs:
  functest:

    # Only run this if the user asked us to
    if: github.event.comment.body == 'run functest'

    runs-on: self-hosted
    container:
      image: python:3.7-buster

    steps:
    - uses: actions/checkout@v2

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with pytest
      run: |
        pip install pytest
        pytest -s basic_functest.py

In the example above, the workflow is triggered with a comment, however it doesn’t show up as a status check for the PR. 

Any help here would be appreciated - thanks! 

Hi @zimventures ,

It’s not supported. Comment for pull request will not create new commit for the repository. According to the status rest api here, you need to have an commit sha firstly, then you can add status to the pull request. Besides, you cannot get the workflow status itself since it’s not completed.

Thanks.

Why can’t it just assume the last commit SHA as the target for the status update? There are certainly plenty of examples out there of actions that perform this type of thing. You’d think it would be allowed directly within a workflow. 

Hi @zimventures ,

Thanks for your reply!

Yes, as an alternative, you can use the last commit in the pull request as the SHA for status creation. And transfer the test result as ‘success, failure…etc’ to API body.

Hi @weide-zhou ,

   I’m a little confused as to what you’re suggesting or showing in your example. Could you please provide the source code? It’s unclear if you’re suggesting that this is something that can happen within the workflow of if you’re using the GitHub API directly to add status messages to the PR. 

Thanks.

Hi @zimventures ,

This can be completed in the workflow with rest api. Code sample as below:

- name: get the one latest&last sha # get the last commit sha in pull_Requst
        run: |
          URL=${{ github.event.issue.pull_request.url }}/commits
          echo "::set-env name=PRSHA::$(curl -u "weide:${{ secrets.GITHUB_TOKEN }}" -s $URL | jq -r '.[] | .sha' | xargs | awk -F " " '{print $NF}')"
      - name: echo PRSHA # check the lastest SHA
        run: |
          echo $PRSHA
      - name: create the status # create the status on the lastest commit sha from pull request
        run: |
          curl -X POST -u "weide:${{ secrets.PAT0309 }}" -H "Content-Type: application/json" "https://api.github.com/repos/${{ github.repository }}/statuses/$PRSHA" --data '{ "state": "failure", "target_url": "https://example.com/build/status", "description": "The build failed!", "context": "continuous-integration/statuscreation7"}'

Note:

  1. You can transfer the test result as a status (success, failure…) to API body.

  2. You need to figure out the latest/last commit SHA in pull request with rest api, if you use other commit(eg: the 1st commit), the status will be created on the SHA but not added into PR.

statusadded.png

Thanks.

1 Like