Skip to content

Instantly share code, notes, and snippets.

@GiladShoham
Created December 27, 2020 12:44
Show Gist options
  • Save GiladShoham/15e5d9ad77aadc69a70ea754b0e8f5c7 to your computer and use it in GitHub Desktop.
Save GiladShoham/15e5d9ad77aadc69a70ea754b0e8f5c7 to your computer and use it in GitHub Desktop.
cli tool for approve PRs via cli
const { graphql } = require("@octokit/graphql");
function getGqlClient(token){
return graphql.defaults({
headers: {
authorization: `token ${token}`,
},
});
}
async function getNodeId(gqlClient, owner = "teambit", repo, prNum) {
const queryResult = await gqlClient(`{
repository(owner: "${owner}", name: "${repo}") {
pullRequest(number: ${prNum}){
id
}
}}`);
return queryResult.repository.pullRequest.id;
}
async function approvedPullRequest(gqlClient, nodeId) {
const queryResult = await gqlClient(`mutation {
addPullRequestReview(input: {pullRequestId: "${nodeId}", event: APPROVE}) {
pullRequestReview {
state
}
}
}`);
}
async function main() {
const [,, token, owner, repo, prNumber] = process.argv;
if (!token || !owner || !repo || !prNumber){
throw new Error('missing args');
}
const gqlClient = getGqlClient(token);
const nodeId = await getNodeId(gqlClient, owner, repo, prNumber);
return approvedPullRequest(gqlClient, nodeId);
}
main().then(res => {
console.log('result ', res)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment