import { Step , StepPrioritizer , StepProcessor } from "./looper/api" ; import { JobId , JobState } from "./index" ; import { RepositoryData } from "../models/subscription" ; import { PullRequestProcessor } from "./pull-request-processor" ; import { BranchProcessor } from "./branch-processor" ; import { CommitProcessor } from "./commit-processor" ; export class Prioritizer implements StepPrioritizer < JobId , JobState > { private readonly commitsSkipCount ; private readonly pullrequestSkipCount ; constructor ( commitsSkipCount ? , pullrequestSkipCount ? ) { this . commitsSkipCount = commitsSkipCount || 10 ; this . pullrequestSkipCount = pullrequestSkipCount || 10 ; } getStepProcessor ( _ , jobState ) { if ( Prioritizer . hasWaitingPullrequests ( jobState . repository ) ) { return new PullRequestProcessor ( ) ; } else if ( Prioritizer . hasWaitingBranches ( jobState . repository ) ) { return new BranchProcessor ( ) ; } else if ( Prioritizer . hasWaitingCommits ( jobState . repository ) ) { return new CommitProcessor ( ) ; } else { return undefined ; } } skip ( _ , jobState ) { const repo = jobState . repository ; if ( Prioritizer . hasWaitingPullrequests ( jobState . repository ) ) { if ( ! repo . lastPullCursor ) { repo . lastPullCursor = 0 ; } repo . lastPullCursor += this . pullrequestSkipCount ; } else if ( Prioritizer . hasWaitingBranches ( repo ) ) { repo . branchStatus = "failed" ; } else { if ( ! repo . lastCommitCursor ) { repo . commitStatus = "failed" ; } else { const commitCursorRegex = /^([^ ]+) ([0-9]+)$/ ; const cursorParts = repo . lastCommitCursor . match ( commitCursorRegex ) ; if ( ! cursorParts || cursorParts . length != 3 ) { repo . commitStatus = "failed" ; } else { const cursorId = cursorParts [ 1 ] ; const index = cursorParts [ 2 ] ; repo . lastCommitCursor = `${ cursorId } ${ + index + this . commitsSkipCount }` ; } } } return jobState ; } private static hasWaitingPullrequests ( repo ) { return repo . pullStatus === "pending" || repo . pullStatus == undefined ; } private static hasWaitingBranches ( repo ) { return repo . branchStatus === "pending" || repo . branchStatus == undefined ; } private static hasWaitingCommits ( repo ) { return repo . commitStatus === "pending" || repo . commitStatus == undefined ; } }