Skip to content

Commit

Permalink
fix: (GithubClient) Filter runs that after in_progress run
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesin11 committed May 18, 2020
1 parent a06e008 commit 38b1751
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
54 changes: 54 additions & 0 deletions __tests__/client/github_client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { GithubClient } from '../../src/client/github_client'

const allCompletedRuns = [
{ run_number: 2, status: 'completed' },
{ run_number: 3, status: 'completed' },
{ run_number: 4, status: 'completed' },
{ run_number: 5, status: 'completed' },
{ run_number: 6, status: 'completed' },
] as any

const hasInprogressRuns = [
{ run_number: 2, status: 'completed' },
{ run_number: 3, status: 'completed' },
{ run_number: 4, status: 'completed' },
{ run_number: 5, status: 'in_progress' },
{ run_number: 6, status: 'completed' },
] as any

describe('GithubClient', () => {
describe('filterWorkflowRuns', () => {
let client: GithubClient
beforeEach(() => {
client = new GithubClient('DUMMY_TOKEN')
})

it('when lastRunId is undef and has not in_pregress runs', async () => {
const fromId = undefined
const actual = client.filterWorkflowRuns(allCompletedRuns, fromId)

expect(actual.map((run) => run.run_number)).toEqual([2,3,4,5,6])
})

it('when defined lastRunId and has not in_pregress runs', async () => {
const fromId = 2
const actual = client.filterWorkflowRuns(allCompletedRuns, fromId)

expect(actual.map((run) => run.run_number)).toEqual([3,4,5,6])
})

it('when lastRunId is undef and has in_pregress runs', async () => {
const fromId = undefined
const actual = client.filterWorkflowRuns(hasInprogressRuns, fromId)

expect(actual.map((run) => run.run_number)).toEqual([2,3,4])
})

it('when defined lastRunId and has in_pregress runs', async () => {
const fromId = 2
const actual = client.filterWorkflowRuns(hasInprogressRuns, fromId)

expect(actual.map((run) => run.run_number)).toEqual([3,4])
})
})
})
31 changes: 25 additions & 6 deletions src/client/github_client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Octokit } from "@octokit/rest";
import { Octokit, RestEndpointMethodTypes } from "@octokit/rest";
import { maxBy } from "lodash";

// Oktokit document: https://octokit.github.io/rest.js/v17#actions

const DEBUG_PER_PAGE = 10

type WorkflowRunsItem = RestEndpointMethodTypes['actions']['listRepoWorkflowRuns']['response']['data']['workflow_runs'][0]
// see: https://developer.github.com/v3/checks/runs/#create-a-check-run
type RunStatus = 'queued' | 'in_progress' | 'completed'

export class GithubClient {
private octokit: Octokit
constructor(token: string, baseUrl?: string) {
Expand All @@ -22,23 +29,35 @@ export class GithubClient {
const runs = await this.octokit.actions.listRepoWorkflowRuns({
owner,
repo,
status: "completed",
per_page: (process.env['CI_ANALYZER_DEBUG']) ? DEBUG_PER_PAGE : 100, // API default is 100
// page: 1, // order desc
})

const filterdWorkflowRuns = this.filterWorkflowRuns(runs.data.workflow_runs, fromRunId)

// Attach workflow name
const workflowRuns = runs.data.workflow_runs.map((run) => {
return filterdWorkflowRuns.map((run) => {
const workflowId = run.workflow_url.split('/').pop()! // parse workflow_url
return {
name: workflowIdMap.get(workflowId)!,
run: run
}
})
}

return (fromRunId)
? workflowRuns.filter((workflowRun) => workflowRun.run.run_number > fromRunId)
: workflowRuns
filterWorkflowRuns (runs: WorkflowRunsItem[], fromRunId?: number): WorkflowRunsItem[] {
const lastInprogress = maxBy(
runs.filter((run) => run.status as RunStatus === 'in_progress'),
(run) => run.run_number
)
// Filter to: fromRunId < Id < lastInprogressId
runs = (fromRunId)
? runs.filter((run) => run.run_number > fromRunId)
: runs
runs = (lastInprogress)
? runs.filter((run) => run.run_number < lastInprogress.run_number)
: runs
return runs
}

// see: https://developer.github.com/v3/actions/workflows/#list-repository-workflows
Expand Down

0 comments on commit 38b1751

Please sign in to comment.