@@ -51,31 +51,38 @@ async function updateCheckStatus(
51
51
const { context, getOctokit } = github
52
52
const octokit = getOctokit ( process . env . GITHUB_TOKEN ! )
53
53
const { owner, repo } = context . repo
54
- const pullRequest = context . payload . pull_request
55
- const sha = pullRequest ?. head . sha
56
-
57
- const checkParams = {
58
- owner,
59
- repo,
60
- name : checkName ,
61
- head_sha : sha ,
62
- status : "completed" as const ,
63
- conclusion : "failure" as const ,
64
- output : {
65
- title : checkName ,
66
- summary : summary ,
67
- text : text ,
68
- } ,
69
- }
70
54
71
- try {
72
- await octokit . rest . checks . create ( checkParams )
73
- } catch ( error ) {
74
- setFailed ( "Failed to create check: " + error )
55
+ // Can only update status on 'pull_request' events
56
+ if ( context . payload . pull_request ) {
57
+ const pullRequest = context . payload . pull_request
58
+ const sha = pullRequest ?. head . sha
59
+
60
+ const checkParams = {
61
+ owner,
62
+ repo,
63
+ name : checkName ,
64
+ head_sha : sha ,
65
+ status : "completed" as const ,
66
+ conclusion : "failure" as const ,
67
+ output : {
68
+ title : checkName ,
69
+ summary : summary ,
70
+ text : text ,
71
+ } ,
72
+ }
73
+
74
+ try {
75
+ await octokit . rest . checks . create ( checkParams )
76
+ } catch ( error ) {
77
+ setFailed ( "Failed to create check: " + error )
78
+ }
75
79
}
76
80
}
77
81
78
- const postComment = async ( outputMd : string ) => {
82
+ const postComment = async (
83
+ outputMd : string ,
84
+ brokenLinkCount : number = 0
85
+ ) : Promise < string > => {
79
86
try {
80
87
const { context, getOctokit } = github
81
88
const octokit = getOctokit ( process . env . GITHUB_TOKEN ! )
@@ -102,7 +109,6 @@ const postComment = async (outputMd: string) => {
102
109
repo,
103
110
prNumber,
104
111
} )
105
- console . log ( "botComment" , botComment )
106
112
if ( botComment ) {
107
113
console . log ( "Updating Comment" )
108
114
const { data } = await octokit . rest . issues . updateComment ( {
@@ -113,7 +119,7 @@ const postComment = async (outputMd: string) => {
113
119
} )
114
120
115
121
return data . html_url
116
- } else {
122
+ } else if ( brokenLinkCount > 0 ) {
117
123
console . log ( "Creating Comment" )
118
124
const { data } = await octokit . rest . issues . createComment ( {
119
125
owner,
@@ -123,6 +129,7 @@ const postComment = async (outputMd: string) => {
123
129
} )
124
130
return data . html_url
125
131
}
132
+ return ""
126
133
} catch ( error ) {
127
134
setFailed ( "Error commenting: " + error )
128
135
return ""
@@ -212,23 +219,29 @@ async function brokenLinkChecker(): Promise<void> {
212
219
} ,
213
220
end : async ( ) => {
214
221
if ( output . links . length ) {
222
+ // DEBUG
223
+ // console.debug(output.links)
224
+
215
225
// Skip links that returned 308
216
- const links404 = output . links . filter (
226
+ const brokenLinksForAttention = output . links . filter (
217
227
( link ) => link . broken && ! [ "HTTP_308" ] . includes ( link . brokenReason )
218
228
)
219
229
220
230
const outputMd = generateOutputMd ( {
221
231
errors : output . errors ,
222
- links : links404 ,
232
+ links : brokenLinksForAttention ,
223
233
pages : [ ] ,
224
234
sites : [ ] ,
225
235
} )
226
- await postComment ( outputMd )
236
+ const commentUrl = await postComment (
237
+ outputMd ,
238
+ brokenLinksForAttention . length
239
+ )
240
+
241
+ // Update GitHub "check" status
242
+ await updateCheckStatus ( brokenLinksForAttention . length , commentUrl )
227
243
228
- // const commentUrl = await postComment(outputMd);
229
- // NOTE: Do we need this additional check in GH output?
230
- // await updateCheckStatus(output.links.length, commentUrl);
231
- setFailed ( `Found broken links` )
244
+ brokenLinksForAttention . length && setFailed ( `Found broken links` )
232
245
}
233
246
} ,
234
247
} )
0 commit comments