forked from jamesgeorge007/github-activity-readme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
307 lines (266 loc) · 8.8 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const core = require("@actions/core");
const fs = require("fs");
const { spawn } = require("child_process");
const { Toolkit } = require("actions-toolkit");
// Get config
const GH_USERNAME = core.getInput("GH_USERNAME");
const COMMIT_NAME = core.getInput("COMMIT_NAME");
const COMMIT_EMAIL = core.getInput("COMMIT_EMAIL");
const COMMIT_MSG = core.getInput("COMMIT_MSG");
const MAX_LINES = core.getInput("MAX_LINES");
const TARGET_FILE = core.getInput("TARGET_FILE");
const EMPTY_COMMIT_MSG = core.getInput("EMPTY_COMMIT_MSG");
/**
* Returns the sentence case representation
* @param {String} str - the string
*
* @returns {String}
*/
const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1);
/**
* Returns a URL in markdown format for PR's and issues
* @param {Object | String} item - holds information concerning the issue/PR
*
* @returns {String}
*/
const toUrlFormat = (item) => {
if (typeof item !== "object") {
return `[${item}](https://github.com/${item})`;
}
if (Object.hasOwnProperty.call(item.payload, "comment")) {
return `[#${item.payload.issue.number}](${item.payload.comment.html_url})`;
}
if (Object.hasOwnProperty.call(item.payload, "issue")) {
return `[#${item.payload.issue.number}](${item.payload.issue.html_url})`;
}
if (Object.hasOwnProperty.call(item.payload, "pull_request")) {
return `[#${item.payload.pull_request.number}](${item.payload.pull_request.html_url})`;
}
if (Object.hasOwnProperty.call(item.payload, "release")) {
const release = item.payload.release.name || item.payload.release.tag_name;
return `[${release}](${item.payload.release.html_url})`;
}
};
/**
* Execute shell command
* @param {String} cmd - root command
* @param {String[]} args - args to be passed along with
*
* @returns {Promise<void>}
*/
const exec = (cmd, args = []) =>
new Promise((resolve, reject) => {
const app = spawn(cmd, args);
let stdout = "";
if (app.stdout) {
app.stdout.on("data", (data) => {
stdout += data.toString();
});
}
let stderr = "";
if (app.stderr) {
app.stderr.on("data", (data) => {
stderr += data.toString();
});
}
app.on("close", (code) => {
if (code !== 0 && !stdout.includes("nothing to commit")) {
return reject(new Error(`Exit code: ${code}\n${stdout}`));
}
return resolve(stdout);
});
app.on("error", () => reject(new Error(`Exit code: ${code}\n${stderr}`)));
});
/**
* Make a commit
*
* @returns {Promise<void>}
*/
const commitFile = async (emptyCommit = false) => {
await exec("git", ["config", "--global", "user.email", COMMIT_EMAIL]);
await exec("git", ["config", "--global", "user.name", COMMIT_NAME]);
if (emptyCommit) {
await exec("git", ["commit", "--allow-empty", "-m", EMPTY_COMMIT_MSG]);
} else {
await exec("git", ["add", TARGET_FILE]);
await exec("git", ["commit", "-m", COMMIT_MSG]);
}
await exec("git", ["push"]);
};
/**
* Creates an empty commit if no activity has been detected for over 50 days
* @returns {Promise<void>}
* */
const createEmptyCommit = async () => {
const lastCommitDate = await exec("git", [
"--no-pager",
"log",
"-1",
"--format=%ct",
]);
const commitDate = new Date(parseInt(lastCommitDate, 10) * 1000);
const diffInDays = Math.round(
(new Date() - commitDate) / (1000 * 60 * 60 * 24),
);
core.debug(`Last commit date: ${commitDate}`);
core.debug(`Difference in days: ${diffInDays}`);
if (diffInDays > 50) {
core.info("Create empty commit to keep workflow active");
await commitFile(true);
return "Empty commit pushed";
}
return "No PullRequest/Issue/IssueComment/Release events found. Leaving README unchanged with previous activity";
};
const serializers = {
IssueCommentEvent: (item) => {
return `🗣 Commented on ${toUrlFormat(item)} in ${toUrlFormat(
item.repo.name,
)}`;
},
IssuesEvent: (item) => {
let emoji = "";
switch (item.payload.action) {
case "opened":
emoji = "❗";
break;
case "reopened":
emoji = "🔓";
break;
case "closed":
emoji = "🔒";
break;
}
return `${emoji} ${capitalize(item.payload.action)} issue ${toUrlFormat(
item,
)} in ${toUrlFormat(item.repo.name)}`;
},
PullRequestEvent: (item) => {
const emoji = item.payload.action === "opened" ? "💪" : "❌";
const line = item.payload.pull_request.merged
? "🎉 Merged"
: `${emoji} ${capitalize(item.payload.action)}`;
return `${line} PR ${toUrlFormat(item)} in ${toUrlFormat(item.repo.name)}`;
},
ReleaseEvent: (item) => {
return `🚀 ${capitalize(item.payload.action)} release ${toUrlFormat(
item,
)} in ${toUrlFormat(item.repo.name)}`;
},
};
Toolkit.run(
async (tools) => {
// Get the user's public events
tools.log.debug(`Getting activity for ${GH_USERNAME}`);
const events = await tools.github.activity.listPublicEventsForUser({
username: GH_USERNAME,
per_page: 100,
});
tools.log.debug(
`Activity for ${GH_USERNAME}, ${events.data.length} events found.`,
);
const content = events.data
// Filter out any boring activity
.filter((event) => serializers.hasOwnProperty(event.type))
// We only have five lines to work with
.slice(0, MAX_LINES)
// Call the serializer to construct a string
.map((item) => serializers[item.type](item));
const readmeContent = fs
.readFileSync(`./${TARGET_FILE}`, "utf-8")
.split("\n");
// Find the index corresponding to <!--START_SECTION:activity--> comment
let startIdx = readmeContent.findIndex(
(content) => content.trim() === "<!--START_SECTION:activity-->",
);
// Early return in case the <!--START_SECTION:activity--> comment was not found
if (startIdx === -1) {
return tools.exit.failure(
`Couldn't find the <!--START_SECTION:activity--> comment. Exiting!`,
);
}
// Find the index corresponding to <!--END_SECTION:activity--> comment
const endIdx = readmeContent.findIndex(
(content) => content.trim() === "<!--END_SECTION:activity-->",
);
if (content.length == 0) {
tools.log.info("Found no activity.");
try {
const message = await createEmptyCommit();
tools.exit.success(message);
} catch (err) {
return tools.exit.failure(err.message);
}
}
if (content.length < 5) {
tools.log.info("Found less than 5 activities");
}
if (startIdx !== -1 && endIdx === -1) {
// Add one since the content needs to be inserted just after the initial comment
startIdx++;
content.forEach((line, idx) =>
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`),
);
// Append <!--END_SECTION:activity--> comment
readmeContent.splice(
startIdx + content.length,
0,
"<!--END_SECTION:activity-->",
);
// Update README
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
// Commit to the remote repository
try {
await commitFile();
} catch (err) {
return tools.exit.failure(err.message);
}
tools.exit.success("Wrote to README");
}
const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n");
const newContent = content
.map((line, idx) => `${idx + 1}. ${line}`)
.join("\n");
if (oldContent.trim() === newContent.trim())
tools.exit.success("No changes detected");
startIdx++;
// Recent GitHub Activity content between the comments
const readmeActivitySection = readmeContent.slice(startIdx, endIdx);
if (!readmeActivitySection.length) {
content.some((line, idx) => {
// User doesn't have 5 public events
if (!line) {
return true;
}
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
});
tools.log.success(`Wrote to ${TARGET_FILE}`);
} else {
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
let count = 0;
readmeActivitySection.some((line, idx) => {
// User doesn't have 5 public events
if (!content[count]) {
return true;
}
if (line !== "") {
readmeContent[startIdx + idx] = `${count + 1}. ${content[count]}`;
count++;
}
});
tools.log.success(`Updated ${TARGET_FILE} with the recent activity`);
}
// Update README
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
// Commit to the remote repository
try {
await commitFile();
} catch (err) {
return tools.exit.failure(err.message);
}
tools.exit.success("Pushed to remote repository");
},
{
event: ["schedule", "workflow_dispatch"],
secrets: ["GITHUB_TOKEN"],
},
);