|
1 |
| -const core = require('@actions/core') |
2 |
| -const fs = require('fs') |
3 |
| -const path = require('path') |
4 |
| -const { spawn } = require('child_process') |
5 |
| -const { Toolkit } = require('actions-toolkit') |
| 1 | +const core = require("@actions/core"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | +const { spawn } = require("child_process"); |
| 5 | +const { Toolkit } = require("actions-toolkit"); |
6 | 6 |
|
7 |
| -const MAX_LINES = 5 |
| 7 | +const MAX_LINES = 5; |
8 | 8 |
|
9 |
| -const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1) |
| 9 | +const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1); |
10 | 10 |
|
11 |
| -const urlPrefix = 'https://github.com/' |
| 11 | +const urlPrefix = "https://github.com/"; |
12 | 12 |
|
13 | 13 | const toUrlFormat = (item) => {
|
14 |
| - if (typeof item === 'object') { |
15 |
| - return Object.hasOwnProperty.call(item.payload, 'issue') |
| 14 | + if (typeof item === "object") { |
| 15 | + return Object.hasOwnProperty.call(item.payload, "issue") |
16 | 16 | ? `[#${item.payload.issue.number}](${urlPrefix}/${item.repo.name}/issues/${item.payload.issue.number})`
|
17 |
| - : `[#${item.payload.pull_request.number}](${urlPrefix}/${item.repo.name}/pull/${item.payload.pull_request.number})` |
| 17 | + : `[#${item.payload.pull_request.number}](${urlPrefix}/${item.repo.name}/pull/${item.payload.pull_request.number})`; |
18 | 18 | }
|
19 |
| - return `[${item}](${urlPrefix}/${item})` |
20 |
| -} |
| 19 | + return `[${item}](${urlPrefix}/${item})`; |
| 20 | +}; |
21 | 21 |
|
22 | 22 | const exec = (cmd, args = []) =>
|
23 | 23 | new Promise((resolve, reject) => {
|
24 |
| - console.log(`Started: ${cmd} ${args.join(' ')}`) |
25 |
| - const app = spawn(cmd, args, { stdio: 'inherit' }) |
26 |
| - app.on('close', (code) => { |
| 24 | + console.log(`Started: ${cmd} ${args.join(" ")}`); |
| 25 | + const app = spawn(cmd, args, { stdio: "inherit" }); |
| 26 | + app.on("close", (code) => { |
27 | 27 | if (code !== 0) {
|
28 |
| - err = new Error(`Invalid status code: ${code}`) |
29 |
| - err.code = code |
30 |
| - return reject(err) |
| 28 | + err = new Error(`Invalid status code: ${code}`); |
| 29 | + err.code = code; |
| 30 | + return reject(err); |
31 | 31 | }
|
32 |
| - return resolve(code) |
33 |
| - }) |
34 |
| - app.on('error', reject) |
35 |
| - }) |
| 32 | + return resolve(code); |
| 33 | + }); |
| 34 | + app.on("error", reject); |
| 35 | + }); |
36 | 36 |
|
37 | 37 | const commitFile = async () => {
|
38 |
| - await exec('git', [ |
39 |
| - 'config', |
40 |
| - '--global', |
41 |
| - 'user.email', |
42 |
| - |
43 |
| - ]) |
44 |
| - await exec('git', ['config', '--global', 'user.name', 'readme-bot']) |
45 |
| - await exec('git', ['add', 'README.md']) |
46 |
| - await exec('git', ['commit', '-m', 'update']) |
47 |
| - await exec('git', ['push']) |
48 |
| -} |
| 38 | + await exec("git", [ |
| 39 | + "config", |
| 40 | + "--global", |
| 41 | + "user.email", |
| 42 | + |
| 43 | + ]); |
| 44 | + await exec("git", ["config", "--global", "user.name", "readme-bot"]); |
| 45 | + await exec("git", ["add", "README.md"]); |
| 46 | + await exec("git", ["commit", "-m", "update"]); |
| 47 | + await exec("git", ["push"]); |
| 48 | +}; |
49 | 49 |
|
50 | 50 | const serializers = {
|
51 | 51 | IssueCommentEvent: (item) => {
|
52 | 52 | return `🗣 Commented on ${toUrlFormat(item)} in ${toUrlFormat(
|
53 | 53 | item.repo.name
|
54 |
| - )}` |
| 54 | + )}`; |
55 | 55 | },
|
56 | 56 | IssuesEvent: (item) => {
|
57 | 57 | return `❗️ ${capitalize(item.payload.action)} issue ${toUrlFormat(
|
58 | 58 | item
|
59 |
| - )} in ${toUrlFormat(item.repo.name)}` |
| 59 | + )} in ${toUrlFormat(item.repo.name)}`; |
60 | 60 | },
|
61 | 61 | PullRequestEvent: (item) => {
|
62 |
| - const emoji = item.payload.action === 'opened' ? '💪' : '❌' |
| 62 | + const emoji = item.payload.action === "opened" ? "💪" : "❌"; |
63 | 63 | const line = item.payload.pull_request.merged
|
64 |
| - ? '🎉 Merged' |
65 |
| - : `${emoji} ${capitalize(item.payload.action)}` |
66 |
| - return `${line} PR ${toUrlFormat(item)} in ${toUrlFormat(item.repo.name)}` |
| 64 | + ? "🎉 Merged" |
| 65 | + : `${emoji} ${capitalize(item.payload.action)}`; |
| 66 | + return `${line} PR ${toUrlFormat(item)} in ${toUrlFormat(item.repo.name)}`; |
67 | 67 | },
|
68 |
| -} |
| 68 | +}; |
69 | 69 |
|
70 | 70 | Toolkit.run(
|
71 | 71 | async (tools) => {
|
72 |
| - const GH_USERNAME = core.getInput('USERNAME') |
| 72 | + const GH_USERNAME = core.getInput("USERNAME"); |
73 | 73 |
|
74 | 74 | // Get the user's public events
|
75 |
| - tools.log.debug(`Getting activity for ${GH_USERNAME}`) |
| 75 | + tools.log.debug(`Getting activity for ${GH_USERNAME}`); |
76 | 76 | const events = await tools.github.activity.listPublicEventsForUser({
|
77 | 77 | username: GH_USERNAME,
|
78 | 78 | per_page: 100,
|
79 |
| - }) |
| 79 | + }); |
80 | 80 | tools.log.debug(
|
81 | 81 | `Activity for ${GH_USERNAME}, ${events.data.length} events found.`
|
82 |
| - ) |
| 82 | + ); |
83 | 83 |
|
84 | 84 | const content = events.data
|
85 | 85 | // Filter out any boring activity
|
86 | 86 | .filter((event) => serializers.hasOwnProperty(event.type))
|
87 | 87 | // We only have five lines to work with
|
88 | 88 | .slice(0, MAX_LINES)
|
89 | 89 | // Call the serializer to construct a string
|
90 |
| - .map((item) => serializers[item.type](item)) |
| 90 | + .map((item) => serializers[item.type](item)); |
91 | 91 |
|
92 |
| - const readmeContent = fs.readFileSync('./README.md', 'utf-8').split('\n') |
| 92 | + const readmeContent = fs.readFileSync("./README.md", "utf-8").split("\n"); |
93 | 93 |
|
94 | 94 | let startIdx = readmeContent.findIndex(
|
95 |
| - (content) => content === '<!--START_SECTION:activity-->' |
96 |
| - ) |
| 95 | + (content) => content === "<!--START_SECTION:activity-->" |
| 96 | + ); |
97 | 97 | if (
|
98 |
| - readmeContent.includes('<!--START_SECTION:activity-->') && |
99 |
| - !readmeContent.includes('<!--END_SECTION:activity-->') |
| 98 | + readmeContent.includes("<!--START_SECTION:activity-->") && |
| 99 | + !readmeContent.includes("<!--END_SECTION:activity-->") |
100 | 100 | ) {
|
101 |
| - startIdx++ |
| 101 | + startIdx++; |
102 | 102 | content.forEach((line, idx) =>
|
103 | 103 | readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`)
|
104 |
| - ) |
105 |
| - readmeContent.splice(startIdx + content.length, 0, '<!--END_SECTION:activity-->') |
106 |
| - fs.writeFileSync('./README.md', readmeContent.join('\n')) |
| 104 | + ); |
| 105 | + readmeContent.splice( |
| 106 | + startIdx + content.length, |
| 107 | + 0, |
| 108 | + "<!--END_SECTION:activity-->" |
| 109 | + ); |
| 110 | + fs.writeFileSync("./README.md", readmeContent.join("\n")); |
107 | 111 | try {
|
108 |
| - await commitFile() |
| 112 | + await commitFile(); |
109 | 113 | } catch (err) {
|
110 |
| - tools.log.debug('Something went wrong') |
111 |
| - return tools.exit.failure(err) |
| 114 | + tools.log.debug("Something went wrong"); |
| 115 | + return tools.exit.failure(err); |
112 | 116 | }
|
113 |
| - tools.exit.success('Created initial setup') |
| 117 | + tools.exit.success("Created initial setup"); |
114 | 118 | }
|
115 | 119 |
|
116 | 120 | const endIdx = readmeContent.findIndex(
|
117 |
| - (content) => content === '<!--END_SECTION:activity-->' |
118 |
| - ) |
119 |
| - const oldContent = readmeContent.slice(startIdx + 1, endIdx).join('\n') |
120 |
| - console.log() |
| 121 | + (content) => content === "<!--END_SECTION:activity-->" |
| 122 | + ); |
| 123 | + const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n"); |
| 124 | + console.log(); |
121 | 125 | const newContent = content
|
122 | 126 | .map((line, idx) => `${idx + 1}. ${line}`)
|
123 |
| - .join('\n') |
| 127 | + .join("\n"); |
124 | 128 |
|
125 | 129 | if (oldContent.trim() === newContent.trim())
|
126 |
| - tools.exit.success('No changes detected') |
| 130 | + tools.exit.success("No changes detected"); |
127 | 131 |
|
128 |
| - startIdx++ |
| 132 | + startIdx++; |
129 | 133 | content.forEach(
|
130 | 134 | (line, idx) => (readmeContent[startIdx + idx] = `${idx + 1}. ${line}`)
|
131 |
| - ) |
132 |
| - fs.writeFileSync('./README.md', readmeContent.join('\n')) |
| 135 | + ); |
| 136 | + fs.writeFileSync("./README.md", readmeContent.join("\n")); |
133 | 137 | try {
|
134 |
| - await commitFile() |
| 138 | + await commitFile(); |
135 | 139 | } catch (err) {
|
136 |
| - tools.log.debug('Something went wrong') |
137 |
| - return tools.exit.failure(err) |
| 140 | + tools.log.debug("Something went wrong"); |
| 141 | + return tools.exit.failure(err); |
138 | 142 | }
|
139 |
| - tools.exit.success('Updated ') |
| 143 | + tools.exit.success("Updated "); |
140 | 144 | },
|
141 | 145 | {
|
142 |
| - event: 'schedule', |
143 |
| - secrets: ['GITHUB_TOKEN'], |
| 146 | + event: "schedule", |
| 147 | + secrets: ["GITHUB_TOKEN"], |
144 | 148 | }
|
145 |
| -) |
| 149 | +); |
0 commit comments