-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
134350d
commit 5b26b87
Showing
1 changed file
with
79 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,149 @@ | ||
const core = require('@actions/core') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { spawn } = require('child_process') | ||
const { Toolkit } = require('actions-toolkit') | ||
const core = require("@actions/core"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { spawn } = require("child_process"); | ||
const { Toolkit } = require("actions-toolkit"); | ||
|
||
const MAX_LINES = 5 | ||
const MAX_LINES = 5; | ||
|
||
const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1) | ||
const capitalize = (str) => str.slice(0, 1).toUpperCase() + str.slice(1); | ||
|
||
const urlPrefix = 'https://github.com/' | ||
const urlPrefix = "https://github.com/"; | ||
|
||
const toUrlFormat = (item) => { | ||
if (typeof item === 'object') { | ||
return Object.hasOwnProperty.call(item.payload, 'issue') | ||
if (typeof item === "object") { | ||
return Object.hasOwnProperty.call(item.payload, "issue") | ||
? `[#${item.payload.issue.number}](${urlPrefix}/${item.repo.name}/issues/${item.payload.issue.number})` | ||
: `[#${item.payload.pull_request.number}](${urlPrefix}/${item.repo.name}/pull/${item.payload.pull_request.number})` | ||
: `[#${item.payload.pull_request.number}](${urlPrefix}/${item.repo.name}/pull/${item.payload.pull_request.number})`; | ||
} | ||
return `[${item}](${urlPrefix}/${item})` | ||
} | ||
return `[${item}](${urlPrefix}/${item})`; | ||
}; | ||
|
||
const exec = (cmd, args = []) => | ||
new Promise((resolve, reject) => { | ||
console.log(`Started: ${cmd} ${args.join(' ')}`) | ||
const app = spawn(cmd, args, { stdio: 'inherit' }) | ||
app.on('close', (code) => { | ||
console.log(`Started: ${cmd} ${args.join(" ")}`); | ||
const app = spawn(cmd, args, { stdio: "inherit" }); | ||
app.on("close", (code) => { | ||
if (code !== 0) { | ||
err = new Error(`Invalid status code: ${code}`) | ||
err.code = code | ||
return reject(err) | ||
err = new Error(`Invalid status code: ${code}`); | ||
err.code = code; | ||
return reject(err); | ||
} | ||
return resolve(code) | ||
}) | ||
app.on('error', reject) | ||
}) | ||
return resolve(code); | ||
}); | ||
app.on("error", reject); | ||
}); | ||
|
||
const commitFile = async () => { | ||
await exec('git', [ | ||
'config', | ||
'--global', | ||
'user.email', | ||
'[email protected]', | ||
]) | ||
await exec('git', ['config', '--global', 'user.name', 'readme-bot']) | ||
await exec('git', ['add', 'README.md']) | ||
await exec('git', ['commit', '-m', 'update']) | ||
await exec('git', ['push']) | ||
} | ||
await exec("git", [ | ||
"config", | ||
"--global", | ||
"user.email", | ||
"[email protected]", | ||
]); | ||
await exec("git", ["config", "--global", "user.name", "readme-bot"]); | ||
await exec("git", ["add", "README.md"]); | ||
await exec("git", ["commit", "-m", "update"]); | ||
await exec("git", ["push"]); | ||
}; | ||
|
||
const serializers = { | ||
IssueCommentEvent: (item) => { | ||
return `🗣 Commented on ${toUrlFormat(item)} in ${toUrlFormat( | ||
item.repo.name | ||
)}` | ||
)}`; | ||
}, | ||
IssuesEvent: (item) => { | ||
return `❗️ ${capitalize(item.payload.action)} issue ${toUrlFormat( | ||
item | ||
)} in ${toUrlFormat(item.repo.name)}` | ||
)} in ${toUrlFormat(item.repo.name)}`; | ||
}, | ||
PullRequestEvent: (item) => { | ||
const emoji = item.payload.action === 'opened' ? '💪' : '❌' | ||
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)}` | ||
? "🎉 Merged" | ||
: `${emoji} ${capitalize(item.payload.action)}`; | ||
return `${line} PR ${toUrlFormat(item)} in ${toUrlFormat(item.repo.name)}`; | ||
}, | ||
} | ||
}; | ||
|
||
Toolkit.run( | ||
async (tools) => { | ||
const GH_USERNAME = core.getInput('USERNAME') | ||
const GH_USERNAME = core.getInput("USERNAME"); | ||
|
||
// Get the user's public events | ||
tools.log.debug(`Getting activity for ${GH_USERNAME}`) | ||
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)) | ||
.map((item) => serializers[item.type](item)); | ||
|
||
const readmeContent = fs.readFileSync('./README.md', 'utf-8').split('\n') | ||
const readmeContent = fs.readFileSync("./README.md", "utf-8").split("\n"); | ||
|
||
let startIdx = readmeContent.findIndex( | ||
(content) => content === '<!--START_SECTION:activity-->' | ||
) | ||
(content) => content === "<!--START_SECTION:activity-->" | ||
); | ||
if ( | ||
readmeContent.includes('<!--START_SECTION:activity-->') && | ||
!readmeContent.includes('<!--END_SECTION:activity-->') | ||
readmeContent.includes("<!--START_SECTION:activity-->") && | ||
!readmeContent.includes("<!--END_SECTION:activity-->") | ||
) { | ||
startIdx++ | ||
startIdx++; | ||
content.forEach((line, idx) => | ||
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`) | ||
) | ||
readmeContent.splice(startIdx + content.length, 0, '<!--END_SECTION:activity-->') | ||
fs.writeFileSync('./README.md', readmeContent.join('\n')) | ||
); | ||
readmeContent.splice( | ||
startIdx + content.length, | ||
0, | ||
"<!--END_SECTION:activity-->" | ||
); | ||
fs.writeFileSync("./README.md", readmeContent.join("\n")); | ||
try { | ||
await commitFile() | ||
await commitFile(); | ||
} catch (err) { | ||
tools.log.debug('Something went wrong') | ||
return tools.exit.failure(err) | ||
tools.log.debug("Something went wrong"); | ||
return tools.exit.failure(err); | ||
} | ||
tools.exit.success('Created initial setup') | ||
tools.exit.success("Created initial setup"); | ||
} | ||
|
||
const endIdx = readmeContent.findIndex( | ||
(content) => content === '<!--END_SECTION:activity-->' | ||
) | ||
const oldContent = readmeContent.slice(startIdx + 1, endIdx).join('\n') | ||
console.log() | ||
(content) => content === "<!--END_SECTION:activity-->" | ||
); | ||
const oldContent = readmeContent.slice(startIdx + 1, endIdx).join("\n"); | ||
console.log(); | ||
const newContent = content | ||
.map((line, idx) => `${idx + 1}. ${line}`) | ||
.join('\n') | ||
.join("\n"); | ||
|
||
if (oldContent.trim() === newContent.trim()) | ||
tools.exit.success('No changes detected') | ||
tools.exit.success("No changes detected"); | ||
|
||
startIdx++ | ||
startIdx++; | ||
content.forEach( | ||
(line, idx) => (readmeContent[startIdx + idx] = `${idx + 1}. ${line}`) | ||
) | ||
fs.writeFileSync('./README.md', readmeContent.join('\n')) | ||
); | ||
fs.writeFileSync("./README.md", readmeContent.join("\n")); | ||
try { | ||
await commitFile() | ||
await commitFile(); | ||
} catch (err) { | ||
tools.log.debug('Something went wrong') | ||
return tools.exit.failure(err) | ||
tools.log.debug("Something went wrong"); | ||
return tools.exit.failure(err); | ||
} | ||
tools.exit.success('Updated ') | ||
tools.exit.success("Updated "); | ||
}, | ||
{ | ||
event: 'schedule', | ||
secrets: ['GITHUB_TOKEN'], | ||
event: "schedule", | ||
secrets: ["GITHUB_TOKEN"], | ||
} | ||
) | ||
); |