@@ -51,31 +51,38 @@ async function updateCheckStatus(
5151 const { context, getOctokit } = github
5252 const octokit = getOctokit ( process . env . GITHUB_TOKEN ! )
5353 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- }
7054
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+ }
7579 }
7680}
7781
78- const postComment = async ( outputMd : string ) => {
82+ const postComment = async (
83+ outputMd : string ,
84+ brokenLinkCount : number = 0
85+ ) : Promise < string > => {
7986 try {
8087 const { context, getOctokit } = github
8188 const octokit = getOctokit ( process . env . GITHUB_TOKEN ! )
@@ -102,7 +109,6 @@ const postComment = async (outputMd: string) => {
102109 repo,
103110 prNumber,
104111 } )
105- console . log ( "botComment" , botComment )
106112 if ( botComment ) {
107113 console . log ( "Updating Comment" )
108114 const { data } = await octokit . rest . issues . updateComment ( {
@@ -113,7 +119,7 @@ const postComment = async (outputMd: string) => {
113119 } )
114120
115121 return data . html_url
116- } else {
122+ } else if ( brokenLinkCount > 0 ) {
117123 console . log ( "Creating Comment" )
118124 const { data } = await octokit . rest . issues . createComment ( {
119125 owner,
@@ -123,6 +129,7 @@ const postComment = async (outputMd: string) => {
123129 } )
124130 return data . html_url
125131 }
132+ return ""
126133 } catch ( error ) {
127134 setFailed ( "Error commenting: " + error )
128135 return ""
@@ -212,23 +219,29 @@ async function brokenLinkChecker(): Promise<void> {
212219 } ,
213220 end : async ( ) => {
214221 if ( output . links . length ) {
222+ // DEBUG
223+ // console.debug(output.links)
224+
215225 // Skip links that returned 308
216- const links404 = output . links . filter (
226+ const brokenLinksForAttention = output . links . filter (
217227 ( link ) => link . broken && ! [ "HTTP_308" ] . includes ( link . brokenReason )
218228 )
219229
220230 const outputMd = generateOutputMd ( {
221231 errors : output . errors ,
222- links : links404 ,
232+ links : brokenLinksForAttention ,
223233 pages : [ ] ,
224234 sites : [ ] ,
225235 } )
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 )
227243
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` )
232245 }
233246 } ,
234247 } )
0 commit comments