forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree-api.ts
More file actions
68 lines (62 loc) · 1.94 KB
/
tree-api.ts
File metadata and controls
68 lines (62 loc) · 1.94 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useEffect, useState } from "react";
import startPollingPipelineStatus from "../pipeline-graph-view/pipeline-graph/main/support/startPollingPipelineStatus.ts";
import { getRunStatusFromPath, RunStatus } from "./RestClient.tsx";
import { mergeStageInfos } from "./utils/stage-merge.ts";
const onPollingError = (err: Error) =>
console.log("There was an error when polling the pipeline status", err);
/**
* Polls a run, stopping once the run has completed
* Optionally retrieves data from the prior run and overlays the new run on top
*/
export default function useRunPoller({
currentRunPath,
previousRunPath,
}: RunPollerProps) {
const [run, setRun] = useState<RunStatus>();
const [loading, setLoading] = useState(true);
useEffect(() => {
let onPipelineDataReceived: (data: RunStatus) => void;
if (previousRunPath) {
let previousRun: RunStatus | null = null;
onPipelineDataReceived = async (current: RunStatus) => {
setLoading(false);
if (current.complete) {
setRun(current);
} else {
if (previousRun == null) {
// only set the previous run if it is not yet set
previousRun = await getRunStatusFromPath(previousRunPath);
}
// error getting previous run
if (previousRun == null) {
setRun(current);
} else {
setRun({
stages: mergeStageInfos(previousRun!.stages, current.stages),
complete: false,
});
}
}
};
} else {
onPipelineDataReceived = (data: RunStatus) => {
setLoading(false);
setRun(data);
};
}
startPollingPipelineStatus(
onPipelineDataReceived,
onPollingError,
() => setLoading(false),
currentRunPath,
);
}, [currentRunPath, previousRunPath]);
return {
run,
loading,
};
}
interface RunPollerProps {
currentRunPath: string;
previousRunPath?: string;
}