-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathview.ts
More file actions
229 lines (210 loc) · 7.5 KB
/
view.ts
File metadata and controls
229 lines (210 loc) · 7.5 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { Args } from '@oclif/core';
import chalk from 'chalk';
import { formatGraphQLBuildArtifacts } from '../../build/utils/formatBuild';
import { getWorkflowRunUrl } from '../../build/utils/url';
import EasCommand from '../../commandUtils/EasCommand';
import { EASNonInteractiveFlag, EasJsonOnlyFlag } from '../../commandUtils/flags';
import {
WorkflowCommandSelectionStateValue,
workflowRunSelectionAction,
} from '../../commandUtils/workflow/stateMachine';
import { WorkflowTriggerType } from '../../commandUtils/workflow/types';
import { computeTriggerInfoForWorkflowRun } from '../../commandUtils/workflow/utils';
import {
BuildArtifacts,
WorkflowArtifact,
WorkflowJobType,
WorkflowRunByIdWithJobsQuery,
} from '../../graphql/generated';
import { WorkflowRunQuery } from '../../graphql/queries/WorkflowRunQuery';
import Log, { link } from '../../log';
import formatFields, { FormatFieldsItem } from '../../utils/formatFields';
import { enableJsonOutput, printJsonOnlyOutput } from '../../utils/json';
type ReducedWorkflowArtifact = Omit<
WorkflowArtifact,
'createdAt' | 'jobRun' | 'storageType' | 'updatedAt'
>;
type WorkflowJobOutput =
WorkflowRunByIdWithJobsQuery['workflowRuns']['byId']['jobs'][number]['outputs'];
type WorkflowJobResult = WorkflowRunByIdWithJobsQuery['workflowRuns']['byId']['jobs'][number] & {
artifacts?: ReducedWorkflowArtifact[] | BuildArtifacts | undefined;
output?: WorkflowJobOutput | undefined;
};
type WorkflowRunResult = WorkflowRunByIdWithJobsQuery['workflowRuns']['byId'] & {
logURL?: string | undefined;
triggerType?: WorkflowTriggerType | undefined;
trigger?: string | null;
jobs: WorkflowJobResult[];
};
const processedOutputs: (job: WorkflowJobResult) => FormatFieldsItem[] = job => {
const result: FormatFieldsItem[] = [];
const keys = job.outputs ? Object.keys(job.outputs) : [];
keys.forEach(key => {
result.push({
label: ` ${key}`,
get value(): string {
const value = job.outputs[key];
return typeof value === 'string' ? value : JSON.stringify(value, null, 2);
},
});
});
return result;
};
export default class WorkflowView extends EasCommand {
static override description =
'view details for a workflow run, including jobs. If no run ID is provided, you will be prompted to select from recent workflow runs for the current project.';
static override flags = {
...EasJsonOnlyFlag,
...EASNonInteractiveFlag,
};
static override args = {
id: Args.string({
description: 'ID of the workflow run to view',
}),
};
static override contextDefinition = {
...this.ContextOptions.ProjectId,
...this.ContextOptions.LoggedIn,
};
async runAsync(): Promise<void> {
const { args, flags } = await this.parse(WorkflowView);
const nonInteractive = flags['non-interactive'];
const {
loggedIn: { graphqlClient },
projectId,
} = await this.getContextAsync(WorkflowView, {
nonInteractive,
});
if (flags.json) {
enableJsonOutput();
}
if (nonInteractive && !args.id) {
throw new Error('If non-interactive, this command requires a workflow run ID as argument');
}
const actionResult = await workflowRunSelectionAction({
graphqlClient,
projectId,
nonInteractive,
allSteps: false,
state: WorkflowCommandSelectionStateValue.WORKFLOW_RUN_SELECTION,
runId: args.id,
});
if (actionResult.state === WorkflowCommandSelectionStateValue.ERROR) {
Log.error(actionResult.message);
return;
}
const idToQuery = actionResult.runId ?? '';
const result: WorkflowRunResult = await WorkflowRunQuery.withJobsByIdAsync(
graphqlClient,
idToQuery,
{
useCache: false,
}
);
const { triggerType, trigger } = computeTriggerInfoForWorkflowRun(result);
result.triggerType = triggerType;
result.trigger = trigger;
const processedJobs: WorkflowJobResult[] = result.jobs.map(job => {
const processedJob = job as WorkflowJobResult;
if (job.type === WorkflowJobType.Build) {
processedJob.artifacts = job.turtleBuild?.artifacts ?? undefined;
} else {
processedJob.artifacts = job.turtleJobRun?.artifacts;
}
delete processedJob.turtleJobRun;
return processedJob;
});
result.jobs = processedJobs;
result.logURL = getWorkflowRunUrl(
result.workflow.app.ownerAccount.name,
result.workflow.app.name,
result.id
);
if (flags.json) {
printJsonOnlyOutput(result);
return;
}
Log.log(
formatFields([
{ label: 'Run ID', value: result.id },
{ label: 'Workflow', value: result.workflow.fileName },
{ label: 'Trigger Type', value: result.triggerType },
{ label: 'Trigger', value: result.trigger ?? 'null' },
{
label: 'Git Commit Message',
value: result.gitCommitMessage?.split('\n')[0] ?? 'null',
},
{ label: 'Status', value: result.status },
{ label: 'Errors', value: result.errors.map(error => error.title).join('\n') },
{ label: 'Created At', value: result.createdAt },
{ label: 'Updated At', value: result.updatedAt },
{ label: 'Log URL', value: link(result.logURL) },
])
);
Log.addNewLineIfNone();
result.jobs.forEach((job: WorkflowJobResult) => {
Log.log(
formatFields([
{ label: 'Job ID', value: job.id },
{ label: ' Key', value: job.key },
{ label: ' Name', value: job.name },
{ label: ' Status', value: job.status },
{ label: ' Type', value: job.type },
{ label: ' Created At', value: job.createdAt },
{ label: ' Updated At', value: job.updatedAt },
])
);
if (job.errors.length > 0) {
Log.gray(chalk.dim(' Errors:'));
job.errors.forEach(error => {
Log.log(formatFields([{ label: ` ${error.title}`, value: `${error.message}` }]));
});
}
if (job.outputs) {
const outputs = processedOutputs(job);
if (outputs.length > 0) {
Log.gray(chalk.dim(' Outputs:'));
Log.log(formatFields(outputs));
}
}
if (job.type === WorkflowJobType.Build) {
if (job.turtleBuild?.artifacts) {
Log.gray(chalk.dim(' Artifacts:'));
Log.log(
formatFields(
formatGraphQLBuildArtifacts(job.turtleBuild).map(item => {
item.label = ` ${item.label}`;
return item;
})
)
);
}
} else {
const jobArtifacts = job.artifacts as ReducedWorkflowArtifact[];
if (jobArtifacts?.length) {
Log.gray(chalk.dim(' Artifacts:'));
jobArtifacts.forEach(artifact => {
Log.log(
formatFields([
{ label: ' ID', value: artifact.id },
{ label: ' Name', value: artifact.name },
{ label: ' Content Type', value: artifact?.contentType ?? 'null' },
{
label: ' File Size Bytes',
value: artifact?.fileSizeBytes ? `${artifact.fileSizeBytes}` : 'null',
},
{ label: ' Filename', value: artifact.filename },
{
label: ' Download URL',
value: artifact?.downloadUrl ? link(artifact.downloadUrl) : 'null',
},
])
);
Log.addNewLineIfNone();
});
}
}
Log.addNewLineIfNone();
});
}
}