Skip to content

Commit

Permalink
Improve runtime assertion (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Aug 16, 2023
1 parent dd1ffc9 commit 9bcf759
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
23 changes: 10 additions & 13 deletions src/queries/completedCheckSuite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert'
import { CompletedCheckSuiteQuery, CompletedCheckSuiteQueryVariables } from '../generated/graphql'
import { CheckAnnotation, CheckRun, CheckStep } from '../generated/graphql-types'
import { Octokit } from '../types'
Expand Down Expand Up @@ -95,9 +96,8 @@ export const queryCompletedCheckSuite = async (
}

const extractCheckRuns = (r: CompletedCheckSuiteQuery): CompletedCheckSuite['node']['checkRuns'] => {
if (r.node?.__typename !== 'CheckSuite') {
throw new Error(`invalid __typename ${String(r.node?.__typename)} !== CheckSuite`)
}
assert(r.node != null)
assert.strictEqual(r.node.__typename, 'CheckSuite')

const checkRuns: CompletedCheckRun[] = []
for (const checkRun of r.node.checkRuns?.nodes ?? []) {
Expand Down Expand Up @@ -147,16 +147,13 @@ const extractCheckRuns = (r: CompletedCheckSuiteQuery): CompletedCheckSuite['nod
}

const extractCommit = (r: CompletedCheckSuiteQuery): CompletedCheckSuite['node']['commit'] => {
if (r.node?.__typename !== 'CheckSuite') {
throw new Error(`invalid __typename ${String(r.node?.__typename)} !== CheckSuite`)
}
if (r.node.commit.file?.object?.__typename !== 'Blob') {
throw new Error(`invalid __typename ${String(r.node.commit.file?.object?.__typename)} !== Blob`)
}
const { text } = r.node.commit.file.object
if (text == null) {
throw new Error(`invalid text ${String(text)}`)
}
assert(r.node != null)
assert.strictEqual(r.node.__typename, 'CheckSuite')
assert(r.node.commit.file != null)
assert(r.node.commit.file.object != null)
assert.strictEqual(r.node.commit.file.object.__typename, 'Blob')
const text = r.node.commit.file.object.text
assert(text != null)
return {
file: {
object: {
Expand Down
18 changes: 10 additions & 8 deletions src/workflowRun/parse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert'
import * as yaml from 'js-yaml'

export type WorkflowDefinition = {
Expand All @@ -10,17 +11,18 @@ export type WorkflowDefinition = {
}

export const parseWorkflowFile = (s: string): WorkflowDefinition => {
const parsed = yaml.load(s)
if (typeof parsed !== 'object' || parsed === null) {
throw new Error(`workflow is not valid object: ${typeof parsed}`)
}
const workflow = parsed as WorkflowDefinition
if (typeof workflow.jobs !== 'object') {
throw new Error(`workflow does not have valid "jobs" field: ${JSON.stringify(workflow)}`)
}
const workflow = yaml.load(s)
assertWorkflowDefinition(workflow)
return workflow
}

function assertWorkflowDefinition(x: unknown): asserts x is WorkflowDefinition {
assert(typeof x === 'object')
assert(x !== null)
assert('jobs' in x)
assert(typeof x.jobs === 'object')
}

export const inferRunner = (jobName: string, workflowDefinition?: WorkflowDefinition): string | undefined => {
if (workflowDefinition === undefined) {
return
Expand Down

0 comments on commit 9bcf759

Please sign in to comment.