Skip to content

Commit

Permalink
feat!: Return error exitCode when catch some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesin11 committed Oct 25, 2020
1 parent c534252 commit 26d28b5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const main = async () => {
const yamlConfig = loadConfig(argv.c)

const runner = new CompositRunner(yamlConfig)
await runner.run()
const result = await runner.run()

if (result.isFailure()) {
console.info('Some runners return error!')
process.exitCode = 1
}
}
main()
14 changes: 10 additions & 4 deletions src/runner/circleci_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CompositExporter } from "../exporter/exporter"
import { LastRunStore } from "../last_run_store"
import { GithubRepositoryClient } from "../client/github_repository_client"
import { CustomReportCollection, createCustomReportCollection, aggregateCustomReportArtifacts } from "../custom_report_collection"
import { failure, Result, success } from "../result"

export class CircleciRunner implements Runner {
service: string = 'circleci'
Expand Down Expand Up @@ -36,8 +37,9 @@ export class CircleciRunner implements Runner {
}
}

async run () {
if (!this.config) return
async run (): Promise<Result<void, Error>> {
let result: Result<void, Error> = success()
if (!this.config) return failure(new Error('this.config must not be undefined'))
this.store = await LastRunStore.init(this.service, this.configDir, this.config.lastRunStore)

let workflowReports: WorkflowReport[] = []
Expand Down Expand Up @@ -98,8 +100,10 @@ export class CircleciRunner implements Runner {
testReports = testReports.concat(repoTestReports)
}
catch (error) {
console.error(`Some error raised in '${repo.fullname}', so it skipped.`)
const errorMessage = `Some error raised in '${repo.fullname}', so it skipped.`
console.error(errorMessage)
console.error(error)
result = failure(new Error(errorMessage))
continue
}
}
Expand All @@ -111,6 +115,8 @@ export class CircleciRunner implements Runner {
await exporter.exportCustomReports(customReportCollection)

this.store.save()
console.info(`Success: done execute '${this.service}'`)
console.info(`Done execute '${this.service}'. status: ${result.type}`)

return result
}
}
14 changes: 10 additions & 4 deletions src/runner/github_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CompositExporter } from "../exporter/exporter"
import { LastRunStore } from "../last_run_store"
import { GithubRepositoryClient } from "../client/github_repository_client"
import { CustomReportCollection, createCustomReportCollection } from "../custom_report_collection"
import { failure, Result, success } from "../result"

export class GithubRunner implements Runner {
service: string = 'github'
Expand All @@ -34,8 +35,9 @@ export class GithubRunner implements Runner {
}
}

async run () {
if (!this.config) return
async run (): Promise<Result<void, Error>> {
let result: Result<void, Error> = success()
if (!this.config) return failure(new Error('this.config must not be undefined'))
this.store = await LastRunStore.init(this.service, this.configDir, this.config.lastRunStore)

let workflowReports: WorkflowReport[] = []
Expand Down Expand Up @@ -69,8 +71,10 @@ export class GithubRunner implements Runner {
}
}
catch (error) {
console.error(`Some error raised in '${repo.fullname}', so it skipped.`)
const errorMessage = `Some error raised in '${repo.fullname}', so it skipped.`
console.error(errorMessage)
console.error(error)
result = failure(new Error(errorMessage))
continue
}
this.setRepoLastRun(repo.fullname, repoWorkflowReports)
Expand All @@ -85,6 +89,8 @@ export class GithubRunner implements Runner {
await exporter.exportCustomReports(customReportCollection)

this.store.save()
console.info(`Success: done execute '${this.service}'`)
console.info(`Done execute '${this.service}'. status: ${result.type}`)

return result
}
}
16 changes: 11 additions & 5 deletions src/runner/jenkins_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { JenkinsAnalyzer } from "../analyzer/jenkins_analyzer"
import { JenkinsConfig, parseConfig } from "../config/jenkins_config"
import { LastRunStore } from "../last_run_store"
import { CustomReportCollection, createCustomReportCollection } from "../custom_report_collection"
import { failure, Result, success } from "../result"

export class JenkinsRunner implements Runner {
service: string = 'jenkins'
Expand All @@ -34,9 +35,10 @@ export class JenkinsRunner implements Runner {
}
}

async run () {
if (!this.config) return
if (!this.client) return
async run (): Promise<Result<void, Error>> {
let result: Result<void, Error> = success()
if (!this.config) return failure(new Error('this.config must not be undefined'))
if (!this.client) return failure(new Error('this.client must not be undefined'))
this.store = await LastRunStore.init(this.service, this.configDir, this.config.lastRunStore)

const allJobs = await this.client.fetchJobs()
Expand Down Expand Up @@ -77,8 +79,10 @@ export class JenkinsRunner implements Runner {
testReports = testReports.concat(jobTestReports)
}
catch (error) {
console.error(`Some error raised in '${configJob.name}', so it skipped.`)
const errorMessage = `Some error raised in '${configJob.name}', so it skipped.`
console.error(errorMessage)
console.error(error)
result = failure(new Error(errorMessage))
continue
}
}
Expand All @@ -90,6 +94,8 @@ export class JenkinsRunner implements Runner {
await exporter.exportCustomReports(customReportCollection)

this.store.save()
console.info(`Success: done execute '${this.service}'`)
console.info(`Done execute '${this.service}'. status: ${result.type}`)

return result
}
}
15 changes: 12 additions & 3 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { YamlConfig } from "../config/config";
import { GithubRunner } from "./github_runner";
import { CircleciRunner } from "./circleci_runner";
import { JenkinsRunner } from "./jenkins_runner";
import { failure, Result, success } from "../result";

export interface Runner {
run (): Promise<void>
run (): Promise<Result<void, Error>>
}

export class CompositRunner implements Runner {
Expand All @@ -24,9 +25,17 @@ export class CompositRunner implements Runner {
}).filter((runner): runner is NonNullable<typeof runner> => runner !== undefined)
}

async run () {
await Promise.all(
async run(): Promise<Result<void, Error>> {
const results = await Promise.all(
this.runners.map((runner) => runner.run())
)

const errorResults = results.filter(result => result.isFailure())
if (errorResults.length > 0) {
console.log('End with failure')
return failure(new Error('Some runner throws error'))
}

return success()
}
}

0 comments on commit 26d28b5

Please sign in to comment.