|
| 1 | +// |
| 2 | +// This script will add a dependabot.yml file to all repositories in provided in a txt file. |
| 3 | +// Generate a list of repos with ../gh-cli/generate-repositories-list.sh |
| 4 | +// input file should look like this: |
| 5 | +// joshjohanning-org/test-repo-1 |
| 6 | +// joshjohanning-org/test-repo-2 |
| 7 | +// |
| 8 | +// Usage: |
| 9 | +// node add-dependabot-file-to-repositories.js ./repos.txt ./dependabot.yml |
| 10 | +// |
| 11 | + |
| 12 | +const { Octokit } = require("octokit"); |
| 13 | +const fs = require('fs'); |
| 14 | + |
| 15 | +// TODO: USER CONFIG - CHANGE THESE VALUES |
| 16 | +const gitUsername = 'josh-issueops-bot[bot]'; // name of GitHub App with [bot] appended |
| 17 | +const gitEmail = '149130343+josh-issueops-bot[bot]@users.noreply.github.com'; // if using GitHub App, find the user ID number by calling: gh api '/users/josh-issueops-bot[bot]' |
| 18 | +const overwrite = false; // if true, will overwrite existing CODEOWNERS file |
| 19 | +// |
| 20 | + |
| 21 | +// const - do not change these values |
| 22 | +const repositoriesInput = process.argv[2]; |
| 23 | +const dependabotYml = process.argv[3]; |
| 24 | +const path = '.github/dependabot.yml'; |
| 25 | +// |
| 26 | + |
| 27 | +// pre-run checks |
| 28 | +if (!process.env.GITHUB_TOKEN) { |
| 29 | + console.error("Please set the GITHUB_TOKEN environment variable."); |
| 30 | + process.exit(1); |
| 31 | +} |
| 32 | + |
| 33 | +if (!fs.existsSync(repositoriesInput)) { |
| 34 | + console.error(`Could not find ${repositoriesInput}`); |
| 35 | + process.exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +if (!fs.existsSync(dependabotYml)) { |
| 39 | + console.error(`Could not find ${dependabotYml}`); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | +// |
| 43 | + |
| 44 | +let octokit = new Octokit({ |
| 45 | + auth: process.env.GITHUB_TOKEN, |
| 46 | + baseUrl: 'https://api.github.com' |
| 47 | +}); |
| 48 | + |
| 49 | +const repositories = fs.readFileSync(repositoriesInput, 'utf8'); |
| 50 | + |
| 51 | +async function main() { |
| 52 | + await findDependabotYml({repositories}); |
| 53 | +} |
| 54 | + |
| 55 | +const findDependabotYml = async ({repositories}) => { |
| 56 | + const repos = repositories |
| 57 | + .split("\n") |
| 58 | + .map((s) => s.trim()) |
| 59 | + .filter((s) => s.length > 0); |
| 60 | + for (const url of repos) { |
| 61 | + let parts = url.split("/"); |
| 62 | + let org = parts[parts.length-2]; |
| 63 | + let repo = parts[parts.length-1]; |
| 64 | + let sha; |
| 65 | + console.log(`\n>>> Processing ${org}/${repo} ... `) |
| 66 | + |
| 67 | + try { |
| 68 | + res = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', { |
| 69 | + owner: org, |
| 70 | + repo: repo, |
| 71 | + path: path, |
| 72 | + headers: { |
| 73 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + sha=sha=res.data.sha; |
| 78 | + } |
| 79 | + catch (error) { |
| 80 | + console.debug(`Could not find dependabot.yml file in ${path}`); |
| 81 | + } |
| 82 | + |
| 83 | + // only if file doesn't exist or overwrite is true |
| 84 | + if (!sha || overwrite) { |
| 85 | + await addDependabotYml(org, repo, sha); |
| 86 | + } else { |
| 87 | + console.log(`${path} already exists in ${org}/${repo}`) |
| 88 | + } |
| 89 | + |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +async function addDependabotYml(org, repo, sha) { |
| 94 | + let commitMessage; |
| 95 | + if (sha) { |
| 96 | + commitMessage = 'Updating dependabot.yml file'; |
| 97 | + } |
| 98 | + else { |
| 99 | + commitMessage = 'Adding dependabot.yml file'; |
| 100 | + } |
| 101 | + |
| 102 | + console.log(`Doing: ${commitMessage} in ${org}/${repo}`); |
| 103 | + |
| 104 | + // convert local dependabot.yml file to base64 |
| 105 | + const buffer = fs.readFileSync(dependabotYml); |
| 106 | + const base64Content = buffer.toString('base64'); |
| 107 | + |
| 108 | + try { |
| 109 | + await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', { |
| 110 | + owner: org, |
| 111 | + repo: repo, |
| 112 | + path: path, |
| 113 | + sha: sha, |
| 114 | + message: commitMessage, |
| 115 | + committer: { |
| 116 | + name: gitUsername, |
| 117 | + email: gitEmail |
| 118 | + }, |
| 119 | + content: base64Content, |
| 120 | + headers: { |
| 121 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 122 | + } |
| 123 | + }) |
| 124 | + console.log(`Successful: ${commitMessage} in ${org}/${repo}`); |
| 125 | + } |
| 126 | + |
| 127 | + catch (error) { |
| 128 | + console.error(error); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +main(); |
0 commit comments