Skip to content

Commit bb8cd83

Browse files
committed
Allow to use components.reference or components.sha
1 parent 06b3fdb commit bb8cd83

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

src/parser-includes.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class ParserIncludes {
7373
// cache the parsed component, because parseIncludeComponent is expensive and we would call it twice otherwise
7474
const componentParseCache = new Map<number, ParsedComponent>();
7575

76-
const include = this.expandInclude(gitlabData?.include, opts.variables);
76+
const include = this.expandInclude(gitlabData?.include, null, opts.variables);
7777

7878
this.normalizeTriggerInclude(gitlabData, opts);
7979
// Find files to fetch from remote and place in .gitlab-ci-local/includes
@@ -190,7 +190,7 @@ export class ParserIncludes {
190190
return includeDatas;
191191
}
192192

193-
static expandInclude (i: any, variables: {[key: string]: string}): any[] {
193+
static expandInclude (i: any, ref: string | null, variables: {[key: string]: string}): any[] {
194194
let include = i || [];
195195
if (include && include.length == null) {
196196
include = [ i ];
@@ -213,16 +213,32 @@ export class ParserIncludes {
213213
for (const entry of include) {
214214
for (const [key, value] of Object.entries(entry)) {
215215
if (Array.isArray(value)) {
216-
entry[key] = value.map((v) => Utils.expandText(v, variables));
216+
entry[key] = value.map((v) => Utils.expandText(this.expandWithRef(v, ref), variables));
217217
} else {
218-
entry[key] = Utils.expandText(value, variables);
218+
entry[key] = Utils.expandText(this.expandWithRef(value, ref), variables);
219219
}
220220
}
221221
}
222222

223223
return include;
224224
}
225225

226+
static expandWithRef (text: any, ref: string | null): string {
227+
if (typeof text !== "string" || ref === null) {
228+
return text;
229+
}
230+
return text.replace(
231+
/\$\[\[ *component.([a-z]+) *\]\]/g,
232+
(_match: string, name: any) => {
233+
if (name === "version" || name === "reference" || name === "sha") {
234+
return ref;
235+
} else {
236+
return "";
237+
}
238+
},
239+
);
240+
}
241+
226242
static covertTemplateToProjectFile (template: string): {project: string; ref: string; file: string; domain: string} {
227243
return {
228244
domain: "gitlab.com",
@@ -279,7 +295,7 @@ export class ParserIncludes {
279295
// Expand local includes inside to a "project"-like include
280296
static expandInnerLocalIncludes (fileIncludes: any, projectPath: string, ref: string, opts: ParserIncludesInitOptions) {
281297
const {argv} = opts;
282-
const updatedIncludes = this.expandInclude(fileIncludes, opts.variables);
298+
const updatedIncludes = this.expandInclude(fileIncludes, ref, opts.variables);
283299
updatedIncludes.forEach((inner: any, i: number) => {
284300
if (!inner["local"]) return;
285301
if (inner["rules"]) {

0 commit comments

Comments
 (0)