-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathparse.ts
More file actions
315 lines (258 loc) · 7.47 KB
/
parse.ts
File metadata and controls
315 lines (258 loc) · 7.47 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import is from '@sindresorhus/is';
import { regEx } from '../../../util/regex.ts';
function splitFirstFrom(
str: string,
sep: string,
start: number,
): [string, string] | null {
const idx = str.indexOf(sep, start);
if (idx === -1) {
return null;
}
return [str.slice(0, idx), str.slice(idx + sep.length)];
}
function splitFirst(str: string, sep: string): [string, string] | null {
return splitFirstFrom(str, sep, 0);
}
interface QuotedValue {
value: string;
quote: string;
}
export function parseQuote(input: string): QuotedValue {
const trimmed = input.trim();
const first = trimmed[0];
const last = trimmed[trimmed.length - 1];
if (
trimmed.length >= 2 &&
first === last &&
(first === '"' || first === "'")
) {
return { value: trimmed.slice(1, -1), quote: first };
}
return { value: trimmed, quote: '' };
}
/**
* Docker container:
* - `docker://image:tag`
* - `docker://image@digest`
* - `docker://image:tag@digest`
*/
export interface DockerReference {
kind: 'docker';
image: string;
tag?: string;
digest?: string;
originalRef: string;
}
/**
* Local file or directory:
* - `./path/to/action`
* - `./.github/workflows/main.yml`
*/
export interface LocalReference {
kind: 'local';
path: string;
}
/**
* Repository:
* - `owner/repo[/path]@ref`
* - `https://host/owner/repo[/path]@ref`
*/
export interface RepositoryReference {
kind: 'repository';
hostname: string;
isExplicitHostname: boolean;
owner: string;
repo: string;
path?: string;
ref: string;
}
export type ActionReference =
| DockerReference
| LocalReference
| RepositoryReference;
export interface ParsedUsesLine {
/** The whitespace before "uses:" */
indentation: string;
/** The `uses:` (and optional `-`) part */
usesPrefix: string;
/** The raw value part, potentially quoted (e.g. `actions/checkout@v2`) */
replaceString: string;
/** Whitespace between value and `#` */
commentPrecedingWhitespace: string;
/** The full comment including `#` */
commentString: string;
actionRef: ActionReference | null;
commentData: CommentData;
/** The quote char used (' or " or empty) */
quote: string;
}
const shaRe = regEx(/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/);
const shaShortRe = regEx(/^[a-f0-9]{6,7}$/);
export function isSha(str: string): boolean {
return shaRe.test(str);
}
export function isShortSha(str: string): boolean {
return shaShortRe.test(str);
}
const DOCKER_PREFIX = 'docker://';
function parseDockerReference(input: string): DockerReference | null {
const originalRef = input.slice(DOCKER_PREFIX.length);
if (!originalRef) {
return null;
}
const digestParts = splitFirst(originalRef, '@');
if (digestParts) {
const [image, digest] = digestParts;
return { kind: 'docker', image, digest, originalRef };
}
// Find tag: look for first colon after last slash (to avoid matching port in registry)
const lastSlashIndex = originalRef.lastIndexOf('/');
const searchStart = lastSlashIndex === -1 ? 0 : lastSlashIndex + 1;
const tagParts = splitFirstFrom(originalRef, ':', searchStart);
if (tagParts) {
const [image, tag] = tagParts;
return { kind: 'docker', image, tag, originalRef };
}
return { kind: 'docker', image: originalRef, originalRef };
}
const repositoryActionRegex = regEx(
/^(?:https:\/\/(?<hostname>[^/]+)\/)?(?<owner>[^/]+)\/(?<repo>[^/]+)(?:\/(?<path>.+?))?@(?<ref>.+)$/,
);
function parseRepositoryReference(input: string): RepositoryReference | null {
const match = repositoryActionRegex.exec(input);
if (!match?.groups) {
return null;
}
const { owner, repo, path, ref } = match.groups;
let { hostname } = match.groups;
let isExplicitHostname = true;
if (is.undefined(hostname)) {
hostname = 'github.com';
isExplicitHostname = false;
}
return {
kind: 'repository',
hostname,
isExplicitHostname,
owner,
repo,
path,
ref,
};
}
export function parseActionReference(uses: string): ActionReference | null {
if (!uses) {
return null;
}
if (uses.startsWith(DOCKER_PREFIX)) {
return parseDockerReference(uses);
}
if (uses.startsWith('./') || uses.startsWith('../')) {
return { kind: 'local', path: uses };
}
return parseRepositoryReference(uses);
}
export interface CommentData {
pinnedVersion?: string;
ref?: string;
ratchetExclude?: boolean;
matchedString?: string;
index?: number;
}
const pinTokenRe = regEx(
/^\s*(?:(?:renovate\s*:\s*)?(?:pin\s+|tag\s*=\s*)?|(?:ratchet:[\w-]+\/[.\w-]+))?@?(?<version>([\w-]*[-/])?v?\d+(?:\.\d+(?:\.\d+)?)?)/,
);
export const versionLikeRe = regEx(/^v?\d+/);
const bareTokenRe = regEx(/^\s*(?<token>\S+)\s*$/);
export function parseComment(commentBody: string): CommentData {
const trimmed = commentBody.trim();
if (trimmed === 'ratchet:exclude') {
return { ratchetExclude: true };
}
// We use commentBody (with leading spaces) to get the correct index relative to the comment start
const match = pinTokenRe.exec(commentBody);
if (match?.groups?.version) {
return {
pinnedVersion: match.groups.version,
matchedString: match[0],
index: match.index,
};
}
const bareMatch = bareTokenRe.exec(commentBody);
if (bareMatch?.groups?.token) {
return {
ref: bareMatch.groups.token,
matchedString: bareMatch[0],
index: bareMatch.index,
};
}
return {};
}
const usesLineRegex = regEx(
/^(?<prefix>\s+(?:-\s+)?uses\s*:\s*)(?<remainder>.+)$/,
);
/**
* Parses a GitHub Actions `uses:` line into its components.
*
* Expected line format:
* ```
* <indentation>[- ]uses: [quote]<action-reference>[quote][ # <comment>]
* ```
*
* Examples:
* - ` uses: actions/checkout@v4`
* - ` - uses: "owner/repo@abc123" # v1.0.0`
* - ` uses: docker://alpine:3.18`
*
* @returns Parsed components or `null` if the line doesn't match `uses:` pattern
*/
export function parseUsesLine(line: string): ParsedUsesLine | null {
const match = usesLineRegex.exec(line);
if (!match?.groups) {
return null;
}
const { prefix, remainder } = match.groups;
if (remainder.startsWith('#')) {
return null;
}
const indentation = prefix.slice(0, prefix.indexOf('uses'));
// We look for ' #' to determine where the comment starts.
// This is a safe heuristic for valid "uses" values which cannot contain spaces.
const commentIndex = remainder.indexOf(' #');
// No comment case
if (commentIndex === -1) {
const { value, quote } = parseQuote(remainder);
return {
indentation,
usesPrefix: prefix,
replaceString: remainder.trim(),
commentPrecedingWhitespace: '',
commentString: '',
actionRef: parseActionReference(value),
commentData: {},
quote,
};
}
// Has comment: split value and comment
const rawValuePart = remainder.slice(0, commentIndex);
const commentPart = remainder.slice(commentIndex + 1); // starts at #
// Calculate whitespace between value and #
const partBeforeHash = remainder.slice(0, commentIndex + 1);
const commentPrecedingWhitespace = partBeforeHash.slice(
partBeforeHash.trimEnd().length,
);
const { value, quote } = parseQuote(rawValuePart);
// commentPart always starts with '#' (see commentIndex search above)
const cleanCommentBody = commentPart.slice(1);
return {
indentation,
usesPrefix: prefix,
replaceString: rawValuePart.trim(),
commentPrecedingWhitespace,
commentString: commentPart,
actionRef: parseActionReference(value),
commentData: parseComment(cleanCommentBody),
quote,
};
}