forked from kentaro-m/add-an-issue-reference-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (50 loc) · 1.54 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
class IssueReferenceCreator {
constructor (tools) {
this.tools = tools
this.args = JSON.parse(this.tools.arguments['_'][0])
}
async go () {
const eventJSON = this.tools.context.payload
// Runs pull_request.opened event only
if (eventJSON.action !== 'opened') {
return
}
// Get the issue number based on branch name
const branchName = eventJSON.pull_request.head.ref
const pattern = new RegExp(`${this.args.branch}-([0-9]+)`);
const issueNumber = this.getIssueNumber(branchName, pattern)
// Skip process to add an issue reference to a pull request
if (issueNumber === 0) {
return
}
// Create the issue comment to the pull request
const issueData = await this.tools.github.issues.get(
this.tools.context.issue({ number: issueNumber })
)
const issueTitle = issueData.data.title
const referenceComment = `Related Issue: #${issueNumber} ${issueTitle}`
const response = await this.tools.github.issues.createComment(
this.tools.context.issue({ body: referenceComment })
)
const result = {
issueNumber,
prNumber: eventJSON.number,
url: response.data.html_url
}
return result
}
/**
* @description Gets the issue number based on branch name
* @param branchName
* @param pattern
* @returns {number}
*/
getIssueNumber(branchName, pattern) {
const result = branchName.match(pattern)
if (result !== null) {
return parseInt(result[1])
}
return 0
}
}
module.exports = IssueReferenceCreator