( pullRequest: PullRequestFromGithub, )
| 219 | * for both status and checks results. |
| 220 | */ |
| 221 | export function getStatusesForPullRequest( |
| 222 | pullRequest: PullRequestFromGithub, |
| 223 | ): PullRequestStatusInfo { |
| 224 | const nodes = pullRequest.commits.nodes; |
| 225 | /** The combined github status and github checks object. */ |
| 226 | const {statusCheckRollup} = nodes[nodes.length - 1].commit; |
| 227 | |
| 228 | // If there is no status check rollup (i.e. no status nor checks), we |
| 229 | // consider the pull request status as failing. |
| 230 | if (!statusCheckRollup) { |
| 231 | return { |
| 232 | combinedStatus: PullRequestStatus.FAILING, |
| 233 | statuses: [], |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | /** A map of all of the statuses indexed by their names. */ |
| 238 | const statusMap = new Map<string, PullRequestStatusInfo['statuses'][number]>(); |
| 239 | |
| 240 | statusCheckRollup.contexts.nodes |
| 241 | .sort((a, b) => { |
| 242 | const aTimestamp = Date.parse(a.completedAt || a.createdAt || '0'); |
| 243 | const bTimestamp = Date.parse(b.completedAt || b.createdAt || '0'); |
| 244 | return aTimestamp - bTimestamp; |
| 245 | }) |
| 246 | .forEach((context) => { |
| 247 | switch (context.__typename) { |
| 248 | case 'CheckRun': |
| 249 | statusMap.set(context.name, { |
| 250 | type: 'check' as const, |
| 251 | name: context.name, |
| 252 | status: normalizeGithubCheckState(context.conclusion, context.status), |
| 253 | }); |
| 254 | case 'StatusContext': |
| 255 | statusMap.set(context.context!, { |
| 256 | type: 'status' as const, |
| 257 | name: context.context!, |
| 258 | status: normalizeGithubStatusState(context.state!), |
| 259 | }); |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | return { |
| 264 | combinedStatus: normalizeGithubStatusState(statusCheckRollup.state), |
| 265 | statuses: Array.from(statusMap.values()), |
| 266 | }; |
| 267 | } |
| 268 | |
| 269 | /** Retrieve the normalized PullRequestStatus for the provided github status state. */ |
| 270 | function normalizeGithubStatusState(state: StatusState): PullRequestStatus { |
no test coverage detected