Skip to content

Commit

Permalink
fix: (JenkinsClient) Filter runs that after in_progress or not_execut…
Browse files Browse the repository at this point in the history
…ed run
  • Loading branch information
Kesin11 committed May 18, 2020
1 parent 49ec0aa commit f16d333
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
61 changes: 60 additions & 1 deletion __tests__/client/jenkins_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { JenkinsClient } from '../../src/client/jenkins_client'

describe('JenkinsClient', () => {
const baseUrl = 'http://localhost:8080'
describe('new', () => {
const baseUrl = 'http://localhost:8080'
it('should not throw error when both user and token are undefined', async () => {
const client = new JenkinsClient(baseUrl)

Expand All @@ -27,4 +27,63 @@ describe('JenkinsClient', () => {
}).toThrow()
})
})

describe('filterJobRuns', () => {
const allCompletedRuns = [
{ id: '2', status: 'SUCCESS' },
{ id: '3', status: 'FAILED' },
{ id: '4', status: 'ABORTED' },
{ id: '5', status: 'SUCCESS' },
{ id: '6', status: 'SUCCESS' },
] as any

const hasInprogressRuns = [
{ id: '2', status: 'SUCCESS' },
{ id: '3', status: 'FAILED' },
{ id: '4', status: 'ABORTED' },
{ id: '5', status: 'IN_PROGRESS' },
{ id: '6', status: 'SUCCESS' },
] as any

const hasNotexecutedRuns = [
{ id: '2', status: 'SUCCESS' },
{ id: '3', status: 'FAILED' },
{ id: '4', status: 'ABORTED' },
{ id: '5', status: 'NOT_EXECUTED' },
{ id: '6', status: 'SUCCESS' },
] as any

let client: JenkinsClient
beforeEach(() => {
client = new JenkinsClient(baseUrl)
})

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

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

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

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

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

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

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

expect(actual.map((run) => run.id)).toEqual(['3','4'])
})
})
})
16 changes: 15 additions & 1 deletion src/client/jenkins_client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, { AxiosInstance } from 'axios'
import { axiosRequestLogger } from './client'
import { maxBy } from 'lodash'

// ref: https://github.com/jenkinsci/pipeline-stage-view-plugin/blob/master/rest-api/src/main/java/com/cloudbees/workflow/rest/external/StatusExt.java
export type JenkinsStatus = 'SUCCESS' | 'FAILED' | 'ABORTED' | 'NOT_EXECUTED' | 'IN_PROGRESS' | 'PAUSED_PENDING_INPUT' | 'UNSTABLE'
Expand Down Expand Up @@ -174,9 +175,22 @@ export class JenkinsClient {
})

const runs = res.data as WfapiRunResponse[]
return (lastRunId)
return this.filterJobRuns(runs, lastRunId)
}

filterJobRuns (runs: WfapiRunResponse[], lastRunId?: number): WfapiRunResponse[] {
const lastInprogress = maxBy(
runs.filter((run) => run.status === 'IN_PROGRESS' || run.status === 'NOT_EXECUTED' ),
(run) => Number(run.id)
)
// Filter to: lastRunId < Id < lastInprogressId
runs = (lastRunId)
? runs.filter((run) => Number(run.id) > lastRunId)
: runs
runs = (lastInprogress)
? runs.filter((run) => Number(run.id) < Number(lastInprogress.id))
: runs
return runs
}

async fetchJobRun(job: JobResponse, runId: number) {
Expand Down

0 comments on commit f16d333

Please sign in to comment.