Last active
September 2, 2021 15:10
-
-
Save adityak74/a9655bf7eff5879f6db72cd4952c0787 to your computer and use it in GitHub Desktop.
Octokit Script to Merge all PRs at once
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 { Octokit } = require("octokit"); | |
const dotenv = require('dotenv'); | |
const path = require('path'); | |
// to load the PAT you created | |
dotenv.config({ path: path.join(__dirname, '..', '..', `.env.${process.env.NODE_ENV || 'development'}`) }); | |
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo | |
const octokit = new Octokit({ auth: process.env.GIT_PAT }); | |
// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user | |
async function mergeAllPullRequests() { | |
await octokit.rest.users.getAuthenticated(); | |
octokit.rest.pulls.list({ | |
owner: "gitusername", | |
repo: "yourrepo", | |
state: "open" | |
}).then(async ({ data }) => { | |
for(let i = 0; i < data.length; i++) { | |
console.info('Merging PR', data[i].number); | |
try { | |
const mergeData = await octokit.rest.pulls.merge({ | |
owner: "adityak74", | |
repo: "newfields-app", | |
pull_number: data[i].number, | |
merge_method: "squash" | |
}); | |
console.info(`${mergeData.data.message} with hash : ${mergeData.data.sha}`); | |
} catch (error) { | |
console.error(error); | |
continue; | |
} | |
} | |
}).catch(err => { | |
console.error('Error listing pull requests', err); | |
}); | |
} | |
mergeAllPullRequests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment