-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
224 lines (189 loc) · 5.53 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
import dotenv from "dotenv";
dotenv.config();
import { Octokit } from "octokit";
import yaml from "yaml";
import fs from "fs";
import path from "path";
// Load the GitHub Personal Access Token and organization name from environment variables
const token = process.env.GITHUB_TOKEN;
const org = process.env.ORG_NAME;
const specificRepo = process.env.SPECIFIC_REPO; // Optionally set this to run on a single repo
const reposDir = path.join(process.cwd(), ".github", "repos");
// Initialize Octokit with the GitHub token
const octokit = new Octokit({ auth: token });
function branchRuleToNewBranch(query, defaultBranch) {
const branchData = {
name: (query.pattern === defaultBranch) ? "default" : query.pattern,
protection: {
enforce_admins: query.isAdminEnforced,
required_pull_request_reviews: null,
restrictions: null,
required_status_checks: null,
},
};
if (query.requiresApprovingReviews) {
branchData.protection.required_pull_request_reviews = {
required_approving_review_count: query.requiredApprovingReviewCount,
};
}
if (query.requiresStatusChecks) {
branchData.protection.required_status_checks = {
strict: query.requiresStrictStatusChecks,
contexts: query.requiredStatusCheckContexts
};
}
return branchData;
}
async function getBranchProtectionRulesData(owner, repo) {
const query = `
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
branchProtectionRules(first: 100) {
nodes {
pattern
requiresApprovingReviews
requiredApprovingReviewCount
requiresStatusChecks
requiredStatusCheckContexts
requiresStrictStatusChecks
restrictsPushes
restrictsReviewDismissals
isAdminEnforced
}
}
}
}
`;
try {
const result = await octokit.graphql(query, {
owner: owner,
repo: repo,
});
return result.repository.branchProtectionRules.nodes;
} catch (error) {
console.error(error.message);
}
}
async function getRepoRulesetsData(owner, repo) {
try {
const result = await octokit.rest.repos.getRepoRulesets({
owner: owner,
repo: repo,
includes_parents: false,
});
return result.data;
} catch (error) {
// console.error(error.message);
}
}
async function getRulesetData(owner, repo, rulesetId) {
try {
const result = await octokit.rest.repos.getRepoRuleset({
owner: owner,
repo: repo,
ruleset_id: rulesetId,
});
return result.data;
} catch (error) {
console.error(error.message);
}
}
function rulesetDataToNewRuleset(rulesetData) {
delete rulesetData.id;
delete rulesetData.source;
delete rulesetData.source_type;
delete rulesetData.created_at;
delete rulesetData.updated_at;
delete rulesetData.node_id;
delete rulesetData.current_user_can_bypass;
delete rulesetData._links;
return rulesetData;
}
async function getRepoData(owner, repo) {
try {
const result = await octokit.rest.repos.get({
owner: owner,
repo: repo,
});
return result.data;
} catch (error) {
console.error(error.message);
}
}
async function processRepository(repoName) {
let rulesets = [];
let branches = [];
// Fetch repository settings
const repoData = await getRepoData(org, repoName);
if (repoData.archived) {
// Skip archived repositories
return;
}
if ([".github"].includes(repoData.name)) {
// Skip the .github repository
return;
}
console.log(`Processing repository: ${repoName}`);
const branchRulesData = await getBranchProtectionRulesData(org, repoName);
for (const branchRule of branchRulesData || []) {
const newBranch = branchRuleToNewBranch(branchRule, repoData.default_branch);
branches.push(newBranch);
}
const repoRulesetsData = await getRepoRulesetsData(org, repoName);
for (const ruleset of repoRulesetsData || []) {
const rulesetData = await getRulesetData(org, repoName, ruleset.id);
const newRuleset = rulesetDataToNewRuleset(rulesetData);
rulesets.push(newRuleset);
}
const jsonData = {};
if (branches.length > 0) {
jsonData.branches = branches;
}
if (rulesets.length > 0) {
jsonData.rulesets = rulesets;
}
const filePath = path.join(reposDir, `${repoName}.yml`);
// Retain any existing props from the repo settings file that are not rulesets or branches
if (fs.existsSync(filePath)) {
const existingData = yaml.parse(fs.readFileSync(filePath, "utf8"));
for (const key in existingData) {
if (!["rulesets", "branches"].includes(key)) {
jsonData[key] = existingData[key];
}
}
}
function isEmptyObject(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
// return if jsonData is an empty object
if (isEmptyObject(jsonData)) {
return;
}
const yamlData = yaml.stringify(jsonData);
fs.writeFileSync(filePath, yamlData, "utf8");
}
async function main() {
if (!fs.existsSync(reposDir)) {
fs.mkdirSync(reposDir);
}
if (specificRepo) {
// Process a specific repository
await processRepository(specificRepo);
} else {
// Process all repositories in the organization
for await (const response of octokit.paginate.iterator(
octokit.rest.repos.listForOrg,
{
org,
type: "all",
}
)) {
for (const repo of response.data) {
await processRepository(repo.name);
}
}
}
console.log("Repository settings processing complete.");
}
main();