Make a call to GitHub API from Workflow job

Hello,

I have a workflow that has 2 jobs. The first job runs angular tests and saves them to tests.log file. The second job just prints the content of the tests.log

What I want however is to post the content of the tests.log file as a general comment on specific Pull Request. So,

  1. Someone makes a Pull Request

  2. The workflow gets triggered and it runs the tests.

  3. Test results are saved into tests.log file for the next job in the sequence.

  4. The next job in the sequence uses the content of tests.log file and posts a comment to the current Pull Request. Content of the comment are test logs from the tests.log file.

I managed with 1-3 steps of my workflow. But for step 4 I don’t understand how to create a comment for a Pull Request; I managed to access and just read logs from tests.logs file. Search results for “GitHub API calls in workflow” and simmilar resulted in how to make API calls to get Workflow or Actions of a project. But I need an opposite thing - I need to call GitHub API inside a workflow

There’s actually an action that calls the Github API: https://github.com/octokit/request-action

You can use that to create a comment something like:

jobs:
  addCommentToPr:
    runs-on: ubuntu-latest
    steps:
      - uses: octokit/request-action@v2.x
        id: add_pr_comment
        with:
          route: POST /repos/:repository/issues/:issue_number/comments
          repository: ${{ github.repository }}
          issue_number: ${{ github.event.number }}
          body: A test comment
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

It seems weird that I had to pass the pull request number as the issue number, and it may not be entirely correct, but it certainly worked when I tested it.  Here’s the documentation on the API about comments on PRs (you’ve already read this I’m sure): https://developer.github.com/v3/pulls/comments/

Hi @alishstrong , 

There is an action to add comment on PR, the comment content is from a file .  If use GITHUB_TOKEN , the comment is added by github-actions bot . 

The file path need to be a relative path against  .github/workflows . For example, your logs file is located in .github/workflows , then just specify the logs file name in finename variable. 

If the logs file is under the root path of your repo, you need to use …/…/tests.log .