[英]Github API GET Approved Pull Request Reviews List
I'm working on a Github Action with the Github API endpoint for the list of reviews but it returns the whole history, including dismissals and comments, but the only ones I need are the ones where the state is "APPROVED".
问题是,如果 PR 有超过 100 个评论对象(每页最多 100 个),我无法找到将在下一页上的已批准对象,因为它按时间顺序返回。
有没有其他方法可以让拉取请求审查批准 state?
我的代码如下:
async function evaluateReviews() {
const result = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
owner: owner,
repo: repo,
pull_number: pullNumber
});
const numberOfApprovalsRequired = core.getInput('number-of-approvals');
const reviews = result.data
var approvals = 0
reviews.forEach((item, i) => {
if (item.state == "APPROVED") {
approvals += 1;
}
});
if (approvals >= numberOfApprovalsRequired) {
addApprovedLabel();
}
}
我在这里发布答案,因此它可能会帮助遇到同样问题的人。
I opened a ticket on Github support, and they answered by saying that the endpoint to retrieve only approved pull request review is not available via REST API, however, I can get the count by using their GraphQL API.
通过使用 GraphQL 将之前的请求更改为新请求,我能够准确地获得所需的内容。
这是代码。
async function evaluateReviews() {
try {
const approvedResponse = await octokit.graphql(`
query($name: String!, $owner: String!, $pull_number: Int!) {
repository(name: $name, owner: $owner) {
pullRequest(number: $pull_number) {
reviews(states: APPROVED) {
totalCount
}
}
}
}
`, {
"name": name,
"owner": owner,
"pull_number": pullNumber
});
const approvalsRequired = core.getInput('number-of-approvals');
const approvals = approvedResponse.repository.pullRequest.reviews.totalCount
if (approvals >= approvalsRequired) {
addApprovedLabel();
}
} catch (error) {
console.log(`ERROR: ${error}`);
}
}
参考:
GitHub GraphQL Explorer 示例:
{
repository(name: "fetch", owner: "github") {
pullRequest(number: 913) {
reviews(states: APPROVED) {
totalCount
}
}
}
}
回复:
{
"data": {
"repository": {
"pullRequest": {
"reviews": {
"totalCount": 3
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.