Created
December 27, 2020 12:44
-
-
Save GiladShoham/15e5d9ad77aadc69a70ea754b0e8f5c7 to your computer and use it in GitHub Desktop.
cli tool for approve PRs via cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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