![](/img/trans.png)
[英]How to programatically create pull request with changed file using GitHub API
[英]How to update a pull request by GitHub APP API?
我开发了 GitHub APP 来管理拉取请求过程,我正在使用两个 API
1.状态检查 API
2.更新拉取请求 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 调用,我需要令牌,我以这种方式生成令牌:
在 GitHub 帐户中创建应用程序
在 APP 中创建了 PEM 私钥
在代码中,使用 PEM 密钥创建了 JWT 令牌
使用 JWT 令牌创建安装令牌,如下所示
Header
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);
});
我想更新代码正在运行,我可以使用 Github APP 更新拉取请求。
我提到的问题发生在我尝试更改拉取请求中的“maintainer_can_modify”时,这是不允许的,只有所有者才允许,然后我将其删除。 但就我而言,我想添加评论、更改标题等。
我将把这个问题作为构建 GitHub 应用程序以使用 NodeJS 更新拉取请求的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.