-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathworkflow.ts
More file actions
84 lines (68 loc) · 2.05 KB
/
workflow.ts
File metadata and controls
84 lines (68 loc) · 2.05 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
import * as vscode from "vscode";
import {
convertWorkflowTemplate,
NoOperationTraceWriter,
parseWorkflow,
WorkflowTemplate
} from "@actions/workflow-parser";
import {ErrorPolicy} from "@actions/workflow-parser/model/convert";
import {basename} from "path";
import {GitHubRepoContext} from "../git/repository";
export async function getContextStringForWorkflow(workflowUri: vscode.Uri): Promise<string> {
try {
const content = await vscode.workspace.fs.readFile(workflowUri);
const file = new TextDecoder().decode(content);
const fileName = "";
const result = parseWorkflow(
{
name: fileName,
content: file
},
new NoOperationTraceWriter()
);
if (result.value) {
const template = await convertWorkflowTemplate(result.context, result.value, undefined, {
errorPolicy: ErrorPolicy.TryConversion
});
const context: string[] = [];
if (template.events["repository_dispatch"]) {
context.push("rdispatch");
}
if (template.events["workflow_dispatch"]) {
context.push("wdispatch");
}
return context.join("");
}
} catch (e) {
// Ignore
}
return "";
}
/**
* Try to get Uri to workflow in currently open workspace folders
*
* @param path Path for workflow. E.g., `.github/workflows/somebuild.yaml`
*/
export function getWorkflowUri(gitHubRepoContext: GitHubRepoContext, path: string): vscode.Uri {
return vscode.Uri.joinPath(gitHubRepoContext.workspaceUri, path);
}
export async function parseWorkflowFile(uri: vscode.Uri): Promise<WorkflowTemplate | undefined> {
try {
const b = await vscode.workspace.fs.readFile(uri);
const workflowInput = new TextDecoder().decode(b);
const fileName = basename(uri.fsPath);
const result = parseWorkflow(
{
name: fileName,
content: workflowInput
},
new NoOperationTraceWriter()
);
if (result.value) {
return await convertWorkflowTemplate(result.context, result.value);
}
} catch {
// Ignore error here
}
return undefined;
}