在GitHub操作中注释拉入请求 [英] Commenting a pull request in a GitHub action

查看:10
本文介绍了在GitHub操作中注释拉入请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在GitHub操作的Pull请求中插入常规注释。我似乎不能把它做对。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操作只是上下文,所以在这一点上我可能是错的。

推荐答案

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

因此,这里有一种使用curl的方法。

.github/workflows/whatever.yml中:

name: Some workflow

on:
  issue_comment:
    types: [created]

jobs:
  comment:
    if: contains(github.event.comment.body, 'special string')

    runs-on: ubuntu-latest

    steps:
      - name: Add comment to PR
        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" }'

这篇关于在GitHub操作中注释拉入请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆