-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetWorkflow.ts
More file actions
42 lines (38 loc) · 1.27 KB
/
Copy pathgetWorkflow.ts
File metadata and controls
42 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { CircleAPIWorkflow, Pipeline, Workflow } from "./types";
import { getEmoji } from "./statusEmoji";
import { fetchWorkflow } from "./fetch";
/**
* Returns the workflow for the given pipeline based on
* the configured WORKFLOW_NAME variable set in the env.
* @throws NoWorkflowError
*/
export async function getWorkflow(pipeline: Pipeline): Promise<Workflow> {
const data = await fetchWorkflow(pipeline);
let foundWorkflow = data.items
.map((workflow: CircleAPIWorkflow) => {
return {
...workflow,
id: workflow.id,
name: workflow.name,
status: getEmoji(workflow.status),
commit: pipeline.commit,
pipelineNumber: pipeline.number,
};
})
.find((workflow) => workflow.name === process.env.WORKFLOW_NAME);
if (!foundWorkflow) {
throw new NoWorkflowError(pipeline);
}
return foundWorkflow;
}
/**
* Typically this just means we are loading a pipeline and
* it hasn't started the workflow yet, so it's undefined.
*/
export class NoWorkflowError extends Error {
constructor(pipeline: Pipeline) {
super(
`No workflow could be found for pipeline: (${pipeline.id}) ${pipeline.commit}`
);
}
}