forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestClient.tsx
More file actions
158 lines (146 loc) · 4.06 KB
/
RestClient.tsx
File metadata and controls
158 lines (146 loc) · 4.06 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
Result,
StageInfo,
} from "../pipeline-graph-view/pipeline-graph/main/PipelineGraphModel.tsx";
import { ResourceBundle } from "./i18n/index.ts";
export interface RunStatus {
stages: StageInfo[];
complete: boolean;
raw?: string;
}
export interface InputStep {
message: string;
cancel: string;
id: string;
ok: string;
parameters: boolean;
}
export interface AllStepsData {
steps: StepInfo[];
runIsComplete: boolean;
raw?: string;
}
/**
* StageInfo is the input, in the form of an Array<StageInfo> of the top-level stages of a pipeline
*/
export interface StepInfo {
name: string;
title: string;
state: Result;
inputStep?: InputStep;
id: string;
type: string;
stageId: string;
pauseDurationMillis: number;
startTimeMillis: number;
totalDurationMillis: number;
flags?: Record<string, unknown>;
}
// Internal representation of console log.
export interface StepLogBufferInfo {
lines: string[];
startByte: number;
endByte: number;
pending?: Promise<void>;
consoleAnnotator?: string;
hasTrailingNewLine?: boolean;
lastFetched?: number;
stopTailing?: boolean;
exceptionText?: string[];
pendingExceptionText?: Promise<void>;
}
// Returned from API, gets converted to 'StepLogBufferInfo'.
export interface ConsoleLogData {
text: string;
startByte: number;
endByte: number;
nodeIsActive: boolean;
consoleAnnotator: string;
}
export async function getRunStatusFromPath(url: string): Promise<RunStatus> {
const response = await fetch(url + "stages/tree");
if (!response.ok) {
throw response.statusText;
}
const text = await response.text();
const json = JSON.parse(text);
json.data.raw = text;
return json.data;
}
export async function getRunSteps(): Promise<AllStepsData> {
const response = await fetch("allSteps");
if (!response.ok) throw response.statusText;
const text = await response.text();
const json = JSON.parse(text);
json.data.raw = text;
return json.data;
}
export async function getConsoleTextOffset(
stepId: string,
startByte: number,
consoleAnnotator: string,
): Promise<ConsoleLogData | null> {
const headers = new Headers({ Accept: "multipart/form-data" });
if (consoleAnnotator) headers.set("X-ConsoleAnnotator", consoleAnnotator);
try {
const response = await fetch(
`../execution/node/${stepId}/log/logText/progressiveHtml?start=${startByte.toString()}`,
{ headers },
);
if (!response.ok) throw response.statusText;
const data = await response.formData();
const text = data.get("text") as string;
const meta = JSON.parse(data.get("meta") as string);
return {
text,
startByte: meta.start,
endByte: meta.end,
nodeIsActive: !meta.completed,
consoleAnnotator: meta.consoleAnnotator,
};
} catch (e) {
console.error(`Caught error when fetching console: '${e}'`);
return null;
}
}
export async function getExceptionText(stepId: string): Promise<string[]> {
try {
const response = await fetch(`exceptionText?nodeId=${stepId}`);
if (!response.ok) throw response.statusText;
const text = await response.text();
if (!text) return [];
return text.split("\n");
} catch (e) {
console.error(`Caught error when fetching console: '${e}'`);
return [];
}
}
export async function getConsoleBuildOutput(): Promise<string | null> {
try {
const response = await fetch(`consoleBuildOutput`);
if (!response.ok) throw response.statusText;
return await response.text();
} catch (e) {
console.error(`Caught error when fetching console: '${e}'`);
return null;
}
}
export async function getResourceBundle(
resource: string,
): Promise<ResourceBundle | undefined> {
try {
const baseUrl: string = document.head.dataset.rooturl ?? "";
const response = await fetch(
`${baseUrl}/i18n/resourceBundle?baseName=${resource}`,
);
if (!response.ok) {
throw response.statusText;
}
return (await response.json()).data;
} catch (e) {
console.error(
`Caught error when fetching resource bundle ${resource}: '${e}'`,
);
return undefined;
}
}