-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (139 loc) · 4.38 KB
/
index.js
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
const axios = require("axios");
const { Command } = require("commander");
const fs = require("fs");
const toml = require("toml");
const program = new Command();
const draftPRs = [];
const approvedPRs = [];
const openPRs = [];
if (!fs.existsSync("./config.toml")) {
console.error("Error: config.toml not found");
process.exit(1);
}
const file = fs.readFileSync("./config.toml", "utf-8");
config = toml.parse(file);
program.version("1.0.0");
program.action(async () => {
const { owner, team_slug: team, token, exclude_bots: excludeBots } = config;
const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
};
console.log("\n\x1b[32m%s\x1b[0m", "Let's Yank this!");
console.log("\x1b[33m%s\x1b[0m%s", "Organization: ", owner);
console.log("\x1b[33m%s\x1b[0m%s", "Team: ", team);
console.log("\nFetching repositories...");
const response = await axios.get(
`https://api.github.com/orgs/${owner}/repos?per_page=100`,
{ headers }
);
if (response.status !== 200) {
console.error(`Error: ${response.status} ${response.statusText}`);
process.exit(1);
}
const repoNames = response.data.map((repo) => repo.name);
console.log(
"Found",
repoNames.length,
"repositories for organization",
owner
);
const filterByTeam = async (repoName) => {
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repoName}/teams`,
{ headers }
);
if (response.status !== 200) {
console.error(`Error: ${response.status} ${response.statusText}`);
process.exit(1);
}
const teamNames = response.data.map((obj) => obj.slug);
return teamNames.includes(team);
};
const filterAsync = async (array, filterFunc) => {
const results = await Promise.all(array.map(filterFunc));
return array.filter((_v, i) => results[i]);
};
const getOpenPullRequests = async (repoName) => {
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repoName}/pulls?state=open`,
{ headers }
);
if (response.status !== 200) {
console.error(`Error: ${response.status} ${response.statusText}`);
process.exit(1);
}
return response.data.filter((pr) => {
if (excludeBots) {
return pr.user.type !== "Bot";
} else {
return true;
}
});
};
const enhancePullRequestWithReview = async (pullRequest) => {
const {
number,
head: {
repo: { name },
},
} = pullRequest;
const response = await axios.get(
`https://api.github.com/repos/${owner}/${name}/pulls/${number}/reviews`,
{ headers }
);
if (response.status !== 200) {
console.error(`Error: ${response.status} ${response.statusText}`);
process.exit(1);
}
const results = response.data;
const reviewStates = results.map((review) => review.state);
return { ...pullRequest, reviewStates };
};
filterAsync(repoNames, filterByTeam).then(async (repos) => {
console.log("Found", repos.length, "repositories for team", team);
const result = await Promise.all(repos.map(getOpenPullRequests));
const openPullRequests = result.flat();
console.log(
"Found",
openPullRequests.length,
"open pull requests in these repos"
);
const openPullRequestsWithReviews = await Promise.all(
openPullRequests.map(enhancePullRequestWithReview)
);
console.log("Getting reviews for these pull requests...");
console.log("\n");
openPullRequestsWithReviews.forEach((pr) => {
if (pr.draft) {
console.log("\x1b[36m%s\x1b[0m (Draft)", pr.title);
draftPRs.push(pr);
} else if (pr.reviewStates.includes("APPROVED")) {
console.log("\x1b[32m%s\x1b[0m", pr.title); // green
approvedPRs.push(pr);
} else {
console.log("\x1b[33m%s\x1b[0m", pr.title); // yellow
openPRs.push(pr);
}
console.log(pr._links.html.href);
if (pr.reviewStates.length !== 0) {
console.log("Review States:", pr.reviewStates.join(", "));
}
console.log("\n");
});
const data = {
draftPRs,
approvedPRs,
openPRs,
};
fs.writeFile("./data.json", JSON.stringify(data), (err) => {
if (err) {
console.error(err);
return;
}
console.log("File has been created");
});
});
});
program.parse(process.argv);