在GitHub操作中评论拉取请求

时间:2019-09-23 16:53:57

标签: github github-api

我正在尝试在GitHub动作的拉取请求中插入常规注释。我似乎无法正确解决。 Octokit, the underlying library, allows you to create reviewComments to PRs,但是那些引用是提交,而这不是我想要的,我想要一个简单的注释。我认为我可以只使用octokit.issues.createComment。但是,这似乎不起作用。 这是代码

import * as core from '@actions/core';
const {GitHub, context} = require('@actions/github')
const parse = require('parse-diff')

async function run() {
    try {
        // get information on everything
        const token = core.getInput('github-token', {required: true})
        const github = new GitHub(token, {} )
        console.log( context )
        const PR_number = context.payload.pull_request.number

        // Check if the body contains required string
        const bodyContains = core.getInput('bodyContains')

        if ( context.payload.pull_request.body.indexOf( bodyContains) < 0  ) {
            core.setFailed("The body of the PR does not contain " + bodyContains);
            console.log( "Actor " + context.actor + " pr number " PR_number)
            const result = await github.issues.createComment({
                owner: context.actor,
                repo: context.payload.repository.full_name,
                issue_number: PR_number,
                body: "We need to have the word " + bodyContains + " in the body of the pull request"
            });
            console.log(result)
       } // more irrelevant stuff below
}}

这似乎简直阻碍了“未找到”。我似乎无法找出这是类型问题还是其他问题。从理论上讲,所有者,回购单,发行号以及主体应该正确,并且正确打印。任何帮助将不胜感激。这可能是GitHub API领域中的一个更普遍的问题,因为GitHub动作只是上下文,所以我在那里可能是错的。

3 个答案:

答案 0 :(得分:3)

2020年做到这一点的方法是使用官方的Github Script actions。请勿混淆,GitHub API的问题和PR都是相同的。

on:
  # Trigger the workflow on push or pull request
  pull_request:
    branches:
      - master
      - develop

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '? Thanks for reporting!'
            })

见过:https://github.com/actions/github-script#comment-on-an-issue

答案 1 :(得分:2)

我最初尝试使用Respost,但是不允许设置原始body字符串。

所以这是使用curl的一种方法。

.github/workflows/whatever.yml中:

name: Some workflow

on:
  issue_comment:
    types: [created]

jobs:
  comment:
    runs-on: ubuntu-latest

    steps:
      - uses: rytswd/comvent@v0.1.0
        id: comvent
        with:
          keyword: "special string"
      - name: Add comment to PR
        if: steps.comvent.outputs.comvent != ''
        env:
          URL: ${{ github.event.issue.comments_url }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          curl \
            -X POST \
            $URL \
            -H "Content-Type: application/json" \
            -H "Authorization: token $GITHUB_TOKEN" \
            --data '{ "body": "blah blah" }'

答案 2 :(得分:0)

您还可以使用@actions/github,这将允许您使用octokit client来简化操作:

const core = require('@actions/core');
const github = require('@actions/github');

async function run() {
  try {
    const message = core.getInput('message');
    const github_token = core.getInput('GITHUB_TOKEN');

    const context = github.context;
    if (context.payload.pull_request == null) {
        core.setFailed('No pull request found.');
        return;
    }
    const pull_request_number = context.payload.pull_request.number;

    const octokit = new github.GitHub(github_token);
    const new_comment = octokit.issues.createComment({
        ...context.repo,
        issue_number: pull_request_number,
        body: message
      });

  } catch (error) {
    core.setFailed(error.message);
  }
}

run();

来自此repo