-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathissue-report.ts
144 lines (135 loc) · 3.78 KB
/
issue-report.ts
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
import { type Octokit } from '@octokit/rest'
import coreLib from '@actions/core'
type CRIArgs = {
core: typeof coreLib
octokit: Octokit
reportTitle: string
reportBody: string
reportRepository: string
reportLabel: string
}
export async function createReportIssue({
core,
octokit,
reportTitle,
reportBody,
reportRepository,
reportLabel,
}: CRIArgs) {
const [owner, repo] = reportRepository.split('/')
// Create issue
let newReport
try {
const { data } = await octokit.request('POST /repos/{owner}/{repo}/issues', {
owner,
repo,
title: reportTitle,
body: reportBody,
labels: [reportLabel],
})
newReport = data
core.info(`Created new report issue at ${newReport.html_url}\n`)
} catch (error: any) {
core.error(error)
core.setFailed('Error creating new issue')
throw error
}
return newReport
}
type LRArgs = {
core: typeof coreLib
octokit: Octokit
newReport: any
reportRepository: string
reportAuthor: string
reportLabel: string
}
export async function linkReports({
core,
octokit,
newReport,
reportRepository,
reportAuthor,
reportLabel,
}: LRArgs) {
const [owner, repo] = reportRepository.split('/')
core.info('Attempting to link reports...')
// Find previous report issue
let previousReports
try {
previousReports = await octokit.rest.issues.listForRepo({
owner,
repo,
creator: reportAuthor,
labels: reportLabel,
state: 'all', // We want to get the previous report, even if it is closed
sort: 'created',
direction: 'desc',
per_page: 25,
})
previousReports = previousReports.data
} catch (error) {
core.setFailed('Error listing issues for repo')
throw error
}
core.info(`Found ${previousReports.length} previous reports`)
if (previousReports.length <= 1) {
core.info('No previous reports to link to')
return
}
// 2nd report should be most recent previous report
const previousReport = previousReports[1]
// Comment the old report link on the new report
try {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: newReport.number,
body: `⬅️ [Previous report](${previousReport.html_url})`,
})
core.info(`Linked old report to new report via comment on new report, #${newReport.number}`)
} catch (error) {
core.setFailed(`Error commenting on newReport, #${newReport.number}`)
throw error
}
// Comment on all previous reports that are still open
for (const previousReport of previousReports) {
if (previousReport.state === 'closed' || previousReport.html_url === newReport.html_url) {
continue
}
// If an old report is not assigned to someone we close it
const shouldClose = !previousReport.assignees?.length
let body = `➡️ [Newer report](${newReport.html_url})`
if (shouldClose) {
body += '\n\nClosing in favor of newer report since there are no assignees on this issue'
}
try {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: previousReport.number,
body,
})
core.info(
`Linked old report to new report via comment on old report: #${previousReport.number}.`,
)
} catch (error) {
core.setFailed(`Error commenting on previousReport, #${previousReport.number}`)
throw error
}
if (shouldClose) {
try {
await octokit.rest.issues.update({
owner,
repo,
issue_number: previousReport.number,
state: 'closed',
})
core.info(`Closing old report: #${previousReport.number} because it doesn't have assignees`)
} catch (error) {
core.setFailed(`Error closing previousReport, #${previousReport.number}`)
throw error
}
}
}
}