Skip to content

Commit 2dca00e

Browse files
committed
feat: adding dependabot file script
1 parent 46104fa commit 2dca00e

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

scripts/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
# scripts
22

3+
## add-dependabot-file-to-repositories.js
4+
5+
Add `dependabot.yml` file to a list of repositories.
6+
7+
The script is expecting:
8+
9+
- an environment variable named `GITHUB_TOKEN` with a GitHub PAT that has `repo` scope
10+
- dependencies installed via `npm i octokit fs`
11+
- Update the `gitUsername`, `gitEmail`, and `overwrite` const at the top of the script accordingly
12+
13+
Script usage:
14+
15+
```bash
16+
export GITHUB_TOKEN=ghp_abc
17+
npm i octokit fs papaparse
18+
node ./add-dependabot-file-to-repositories.js ./repos.txt ./dependabot.yml
19+
```
20+
21+
The `repos.txt` should be in the following format:
22+
23+
```
24+
joshjohanning-org/test-repo-1
25+
joshjohanning-org/test-repo-2
26+
joshjohanning-org/test-repo-3
27+
```
28+
329
## ado_workitems_to_github_issues.ps1
430

531
Migrate work items from Azure DevOps to GitHub issues - this just links out to a [separate repo](https://github.com/joshjohanning/ado_workitems_to_github_issues)
@@ -97,7 +123,7 @@ export REPOSITORIES="https://github.com/joshjohanning-org/codeowners-scripting-t
97123
https://github.com/joshjohanning-org/codeowners-scripting-test-2
98124
"
99125
npm i octokit fs papaparse
100-
node ./call-codeowners.js
126+
node ./update-codeowners-mappings.js
101127

102128
```
103129

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)