@@ -24,6 +24,7 @@ import { byDate, invert } from "../../../shared/src/sort.js";
2424import { commentOrUpdate } from "../comment.js" ;
2525import { extractInputs } from "../context.js" ;
2626import {
27+ ImpactAssessmentSchema ,
2728 brChRevApproval ,
2829 getViolatedRequiredLabelsRules ,
2930 processImpactAssessment ,
@@ -132,6 +133,11 @@ import path from "path";
132133 * @property {string } [target_url]
133134 */
134135
136+ /**
137+ * @typedef {import("../github.js").CheckRuns[0] } CheckRun
138+ * @typedef {import("../github.js").CommitStatuses[0] } CommitStatus
139+ */
140+
135141// Placing these configuration items here until we decide another way to pull them in.
136142const FYI_CHECK_NAMES = [
137143 "Swagger LintDiff" ,
@@ -294,7 +300,12 @@ export default async function summarizeChecks({ github, context, core }) {
294300 return ;
295301 }
296302
297- const targetBranch = context . payload . pull_request ?. base ?. ref ;
303+ const targetBranch =
304+ context . eventName === "pull_request_target"
305+ ? /** @type {import("@octokit/webhooks-types").PullRequestEvent } */ ( context . payload )
306+ . pull_request . base . ref
307+ : undefined ;
308+
298309 core . info ( `PR target branch: ${ targetBranch } ` ) ;
299310
300311 // Default target is this run itself
@@ -343,7 +354,7 @@ export function outputRunDetails(core, requiredCheckRuns, fyiCheckRuns) {
343354 * @param {number } issue_number
344355 * @param {string } head_sha
345356 * @param {string } event_name
346- * @param {string } targetBranch
357+ * @param {string|undefined } targetBranch
347358 * @param {string } target_url
348359 * @returns {Promise<void> }
349360 */
@@ -363,7 +374,7 @@ export async function summarizeChecksImpl(
363374 const prUrl = `https://github.com/${ owner } /${ repo } /pull/${ issue_number } ` ;
364375 core . summary . addRaw ( "PR: " ) ;
365376 core . summary . addLink ( prUrl , prUrl ) ;
366- core . summary . write ( ) ;
377+ await core . summary . write ( ) ;
367378 core . setOutput ( "summary" , process . env . GITHUB_STEP_SUMMARY ) ;
368379
369380 let labelNames = await getExistingLabels ( github , owner , repo , issue_number ) ;
@@ -391,7 +402,7 @@ export async function summarizeChecksImpl(
391402 ) ;
392403 }
393404
394- let labelContext = await updateLabels ( labelNames , impactAssessment ) ;
405+ let labelContext = updateLabels ( labelNames , impactAssessment ) ;
395406
396407 core . info (
397408 `Summarize checks label actions against ${ owner } /${ repo } #${ issue_number } : \n` +
@@ -430,7 +441,7 @@ export async function summarizeChecksImpl(
430441 }
431442 }
432443
433- const [ commentBody , automatedChecksMet ] = await createNextStepsComment (
444+ const [ commentBody , automatedChecksMet ] = createNextStepsComment (
434445 core ,
435446 repo ,
436447 labelNames ,
@@ -447,7 +458,7 @@ export async function summarizeChecksImpl(
447458 `Updating comment '${ NEXT_STEPS_COMMENT_ID } ' on ${ owner } /${ repo } #${ issue_number } with body: ${ commentBody } ` ,
448459 ) ;
449460 core . summary . addRaw ( `\n${ commentBody } \n\n` ) ;
450- core . summary . write ( ) ;
461+ await core . summary . write ( ) ;
451462
452463 // this will remain commented until we're comfortable with the change.
453464 await commentOrUpdate (
@@ -468,7 +479,7 @@ export async function summarizeChecksImpl(
468479 ) ;
469480 core . summary . addHeading ( "Automated Checks Met" , 2 ) ;
470481 core . summary . addCodeBlock ( JSON . stringify ( automatedChecksMet , null , 2 ) ) ;
471- core . summary . write ( ) ;
482+ await core . summary . write ( ) ;
472483}
473484
474485/**
@@ -646,7 +657,7 @@ export async function getCheckRunTuple(
646657 // all checks will be considered as "FYI" until we have an impact assessment, so we can
647658 // determine the target branch, and from there pull branch protect rulesets to ensure we
648659 // are marking the required checks correctly.
649- /** @type {Array<CheckRunData & {_originalData: any , _source: string}> } */
660+ /** @type {Array<CheckRunData & {_originalData: CheckRun|CommitStatus , _source: string}> } */
650661 const allChecks = [ ] ;
651662
652663 allCheckRuns . forEach ( ( checkRun ) => {
@@ -694,14 +705,17 @@ export async function getCheckRunTuple(
694705 } ) ;
695706
696707 // Group by name and take the latest for each
708+ /** @type {Map<string, Array<CheckRunData & {_originalData: CheckRun|CommitStatus, _source: string}>> } */
697709 const checksByName = new Map ( ) ;
698710
699711 allChecks . forEach ( ( check ) => {
700712 const name = check . name ;
701- if ( ! checksByName . has ( name ) ) {
702- checksByName . set ( name , [ ] ) ;
713+ let checks = checksByName . get ( name ) ;
714+ if ( checks ) {
715+ checks . push ( check ) ;
716+ } else {
717+ checksByName . set ( name , [ check ] ) ;
703718 }
704- checksByName . get ( name ) . push ( check ) ;
705719 } ) ;
706720
707721 // For each group, sort by date (newest first) and take the first one
@@ -712,15 +726,12 @@ export async function getCheckRunTuple(
712726 invert (
713727 byDate ( ( check ) => {
714728 if ( check . _source === "checkRun" ) {
715- // Check runs have started_at, completed_at, etc. Use the most recent available date
716- return (
717- check . _originalData . completed_at ||
718- check . _originalData . started_at ||
719- check . _originalData . created_at
720- ) ;
729+ const originalData = /** @type {CheckRun } */ ( check . _originalData ) ;
730+ // Use the most recent available date, or "1970" (oldest possible) if the data contains no dates
731+ return originalData . completed_at || originalData . started_at || "1970" ;
721732 } else {
722- // Commit statuses have created_at and updated_at
723- return check . _originalData . updated_at || check . _originalData . created_at ;
733+ const originalData = /** @type { CommitStatus } */ ( check . _originalData ) ;
734+ return originalData . updated_at ;
724735 }
725736 } ) ,
726737 ) ,
@@ -733,18 +744,17 @@ export async function getCheckRunTuple(
733744 latestCheck . status === "completed" &&
734745 latestCheck . conclusion === "success"
735746 ) {
747+ const originalData = /** @type {CheckRun } */ ( latestCheck . _originalData ) ;
736748 const workflowRuns = await github . paginate ( github . rest . actions . listWorkflowRunsForRepo , {
737749 owner,
738750 repo,
739751 head_sha : head_sha ,
740- check_suite_id : latestCheck . _originalData . check_suite . id ,
752+ check_suite_id : originalData . check_suite ? .id ,
741753 per_page : PER_PAGE_MAX ,
742754 } ) ;
743755
744756 if ( workflowRuns . length === 0 ) {
745- core . warning (
746- `No workflow runs found for check suite ID: ${ latestCheck . _originalData . check_suite . id } ` ,
747- ) ;
757+ core . warning ( `No workflow runs found for check suite ID: ${ originalData . check_suite ?. id } ` ) ;
748758 } else {
749759 // Sort by updated_at to get the most recent run
750760 const sortedRuns = workflowRuns . sort (
@@ -754,7 +764,7 @@ export async function getCheckRunTuple(
754764
755765 if ( workflowRuns . length > 1 ) {
756766 core . info (
757- `Found ${ workflowRuns . length } workflow runs for check suite ID: ${ latestCheck . _originalData . check_suite . id } , using most recent: ${ sortedRuns [ 0 ] . id } ` ,
767+ `Found ${ workflowRuns . length } workflow runs for check suite ID: ${ originalData . check_suite ? .id } , using most recent: ${ sortedRuns [ 0 ] . id } ` ,
758768 ) ;
759769 }
760770 }
@@ -862,14 +872,14 @@ export function getCheckInfo(checkName) {
862872 * @param {typeof import("@actions/core") } core
863873 * @param {string } repo
864874 * @param {string[] } labels
865- * @param {string } targetBranch
875+ * @param {string|undefined } targetBranch
866876 * @param {CheckRunData[] } requiredRuns
867877 * @param {CheckRunData[] } fyiRuns
868878 * @param {boolean } assessmentCompleted
869879 * @param {string } target_url
870- * @returns {Promise< [string, CheckRunResult]> }
880+ * @returns {[string, CheckRunResult] }
871881 */
872- export async function createNextStepsComment (
882+ export function createNextStepsComment (
873883 core ,
874884 repo ,
875885 labels ,
@@ -898,7 +908,7 @@ export async function createNextStepsComment(
898908 . filter ( ( run ) => checkRunIsSuccessful ( run ) === false )
899909 . map ( ( run ) => run . checkInfo ) ;
900910
901- const [ commentBody , automatedChecksMet ] = await buildNextStepsToMergeCommentBody (
911+ const [ commentBody , automatedChecksMet ] = buildNextStepsToMergeCommentBody (
902912 core ,
903913 labels ,
904914 `${ repo } /${ targetBranch } ` ,
@@ -921,9 +931,9 @@ export async function createNextStepsComment(
921931 * @param {CheckMetadata[] } failingFyiChecksInfo
922932 * @param {boolean } assessmentCompleted
923933 * @param {string } target_url
924- * @returns {Promise< [string, CheckRunResult]> }
934+ * @returns {[string, CheckRunResult] }
925935 */
926- async function buildNextStepsToMergeCommentBody (
936+ function buildNextStepsToMergeCommentBody (
927937 core ,
928938 labels ,
929939 targetBranch ,
@@ -936,7 +946,7 @@ async function buildNextStepsToMergeCommentBody(
936946 // Build the comment header
937947 const commentTitle = `<h2>Next Steps to Merge</h2>` ;
938948
939- const violatedReqLabelsRules = await getViolatedRequiredLabelsRules ( core , labels , targetBranch ) ;
949+ const violatedReqLabelsRules = getViolatedRequiredLabelsRules ( core , labels , targetBranch ) ;
940950
941951 // we are "blocked" if we have any violated labelling rules OR if we have any failing required checks
942952 const anyBlockerPresent = failingReqChecksInfo . length > 0 || violatedReqLabelsRules . length > 0 ;
@@ -1182,9 +1192,6 @@ export async function getImpactAssessment(github, core, owner, repo, runId) {
11821192
11831193 await fs . unlink ( tmpZip ) ;
11841194
1185- /** @type {import("./labelling.js").ImpactAssessment } */
1186- // todo: we need to zod this to ensure the structure is correct, however we do not have zod installed at time of run
1187- const impact = JSON . parse ( jsonContent ) ;
1188- return impact ;
1195+ return ImpactAssessmentSchema . parse ( JSON . parse ( jsonContent ) ) ;
11891196}
11901197// #endregion
0 commit comments