is_default_branch global expression

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2022-04-25 13:41:39 +02:00
parent 535130561a
commit e1a45f6e54
7 changed files with 179 additions and 40 deletions

View File

@ -30,6 +30,7 @@ export class Meta {
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
// Needs to override Git reference with pr ref instead of upstream branch ref
// for pull_request_target event
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
if (/pull_request_target/.test(context.eventName)) {
context.ref = `refs/pull/${context.payload.number}/merge`;
}
@ -51,7 +52,11 @@ export class Meta {
};
for (const tag of this.tags) {
if (!/true/i.test(tag.attrs['enable'])) {
const enabled = this.setGlobalExp(tag.attrs['enable']);
if (!['true', 'false'].includes(enabled)) {
throw new Error(`Invalid value for enable attribute: ${enabled}`);
}
if (!/true/i.test(enabled)) {
continue;
}
switch (tag.type) {
@ -369,6 +374,27 @@ export class Meta {
}
return '';
},
is_default_branch: function () {
let branch = ctx.ref.replace(/^refs\/heads\//g, '');
if (/^refs\/tags\//.test(ctx.ref) && ctx.payload?.base_ref != undefined) {
branch = ctx.payload.base_ref.replace(/^refs\/heads\//g, '');
}
if (/^refs\/pull\//.test(ctx.ref) && ctx.payload?.pull_request?.base?.ref != undefined) {
branch = ctx.payload.pull_request.base.ref;
}
if (branch == undefined || branch.length == 0) {
return 'false';
}
if (ctx.payload?.repository?.default_branch == branch) {
return 'true';
}
// following events always trigger for last commit on default branch
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
if (/create/.test(ctx.eventName) || /discussion/.test(ctx.eventName) || /issues/.test(ctx.eventName) || /schedule/.test(ctx.eventName)) {
return 'true';
}
return 'false';
},
date: function (format) {
return moment(currentDate).utc().format(format);
}

View File

@ -206,9 +206,6 @@ export function Parse(s: string): Tag {
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'priority')) {
tag.attrs['priority'] = DefaultPriorities[tag.type];
}
if (!['true', 'false'].includes(tag.attrs['enable'])) {
throw new Error(`Invalid value for enable attribute: ${tag.attrs['enable']}`);
}
return tag;
}