-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
52 lines (41 loc) · 1.58 KB
/
app.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
console.log('started nodejs...')
const helpers = require('./helpers')
//require octokit rest.js
//more info at https://github.com/octokit/rest.js
const { Octokit } = require('@octokit/rest')
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`,
})
//set eventOwner and eventRepo based on action's env variables
const eventOwnerAndRepo = process.env.GITHUB_REPOSITORY
const eventOwner = helpers.getOwner(eventOwnerAndRepo)
const eventRepo = helpers.getRepo(eventOwnerAndRepo)
async function commitChecker() {
try {
//read contents of action's event.json
const eventData = await helpers.readFilePromise('..' + process.env.GITHUB_EVENT_PATH)
if (eventData) {
const eventJSON = JSON.parse(eventData)
//check if branch name starts with an issue number
const branchIssueNumber = helpers.getIssueFromBranch(eventJSON.ref)
if (branchIssueNumber) {
//for each commit, check commit message for comment
eventJSON.commits.forEach((commit) => {
const commitComment = helpers.checkForCommitActions(commit.message)
//if comment in commit message, add comment to related issue
if (commitComment) {
const comment = `${commitComment} *from @${
commit.author.username
} in [${commit.id.substring(0, 6)}](${commit.url})*`
helpers.addComment(octokit, eventOwner, eventRepo, branchIssueNumber, comment)
}
})
}
}
} catch (error) {
console.log(error)
}
}
//run the function
commitChecker()
module.exports.commitChecker = commitChecker