首页 > 解决方案 > 如何通过 GitHub APP API 更新拉取请求?

问题描述

我开发了 GitHub APP 来管理拉取请求过程,我正在使用两个 API
1.状态检查 API
2. update-a-pull-request API
当请求更新由一个人创建的现有拉取请求时,我收到此错误消息的用户。

如何获得具有正确权限的安装令牌?

{
  "message": "Validation Failed",
  "errors": [
    {
      "message": "Only the pull request author may change the maintainer_can_modify value",
      "resource": "PullRequest",
      "field": "maintainer_can_modify",
      "code": "invalid"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/pulls/#update-a-pull-request"
}

要执行 API 调用,我需要令牌,我以这种方式生成令牌:

标题

function generateJwtToken() {
    return jsonwebtoken.sign(
        {
            iat: Math.floor(new Date() / 1000),
            exp: Math.floor(new Date() / 1000) + 60,
            iss: ISSUER_ID,
        },
        PEM,
        {algorithm: 'RS256'},
    );
}
let githubHeadersToAccessToken = {
    'User-Agent': 'nodejs',
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + generateJwtToken(),
    'Accept': 'application/vnd.github.machine-man-preview+json'
};

获取安装令牌

payloadToken = {
    "repository_ids": [
    ],
    "permissions": {
        "pull_requests": "write",
        "statuses": "write",
        "administration" : "write",
        "organization_administration": "write",
        "contents": "write",
        "team_discussions": "write",
    }
};
request.get({
    url: 'https://api.github.com/app/installations',
    headers: githubHeadersToAccessToken
}, function optionalCallback(err, httpResponse, body) {
    if (err) {
        return console.error('Connected App get token failed:', err);
    }
    console.log('APP Installation id');
    console.log('Installation id: ' + JSON.parse(body)[0].id);
    app_installed_id = JSON.parse(body)[0].id;

    request.post({
        url: 'https://api.github.com/app/installations/'+app_installed_id+'/access_tokens',
        headers: githubHeadersToAccessToken,
        json: payloadToken
    }, function optionalCallback(err, httpResponse, body) {
        if (err) {
            return console.error('Failed to get access token to GitHub: ', err);
        }
        //console.log(body);
        console.log('Access token to GitHub: ' + body.token);
    });
});

更新由其中一位用户创建的现有拉取请求

   request.post({
        url: 'https://api.github.com/repos/'+owner+'/'+repo+'/statuses/'+sha,
        headers: githubHeadersApiV3,
        json: {
            "state": status,
            "context": "PR <> GUS Validation"
        }
    }, function optionalCallback(err, httpResponse, body) {
        if (err) {
            return console.error('Connected App get token failed:', err);
        }
        //console.log(body);
        console.log('commit sha: ' + sha + ' updated with status: ' + status);
    });

标签: node.jsgithub-apigithub-app

解决方案


我想更新代码正在运行,我可以使用 Github APP 更新拉取请求。
我提到的问题发生在我尝试更改拉取请求中的“maintainer_can_modify”时,这是不允许的,只有所有者才允许,然后我将其删除。但就我而言,我想添加评论、更改标题等。

我将把这个问题作为构建 GitHub 应用程序以使用 NodeJS 更新拉取请求的示例。


推荐阅读