2022-04-25 13:35:08 +02:00
|
|
|
import {parse} from 'csv-parse/sync';
|
2021-04-03 18:15:27 +02:00
|
|
|
import * as core from '@actions/core';
|
2021-03-29 13:04:53 +02:00
|
|
|
|
|
|
|
export enum Type {
|
|
|
|
Schedule = 'schedule',
|
|
|
|
Semver = 'semver',
|
2021-07-06 13:56:48 +02:00
|
|
|
Pep440 = 'pep440',
|
2021-03-29 13:04:53 +02:00
|
|
|
Match = 'match',
|
|
|
|
Edge = 'edge',
|
|
|
|
Ref = 'ref',
|
|
|
|
Raw = 'raw',
|
|
|
|
Sha = 'sha'
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum RefEvent {
|
|
|
|
Branch = 'branch',
|
|
|
|
Tag = 'tag',
|
|
|
|
PR = 'pr'
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:14:23 +02:00
|
|
|
export enum ShaFormat {
|
|
|
|
Short = 'short',
|
|
|
|
Long = 'long'
|
|
|
|
}
|
|
|
|
|
2021-04-03 18:15:27 +02:00
|
|
|
export class Tag {
|
|
|
|
public type?: Type;
|
|
|
|
public attrs: Record<string, string>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.attrs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
public toString(): string {
|
|
|
|
const out: string[] = [`type=${this.type}`];
|
2022-03-22 21:09:00 +01:00
|
|
|
for (const attr in this.attrs) {
|
2021-04-03 18:15:27 +02:00
|
|
|
out.push(`${attr}=${this.attrs[attr]}`);
|
|
|
|
}
|
|
|
|
return out.join(',');
|
|
|
|
}
|
2021-03-29 13:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const DefaultPriorities: Record<Type, string> = {
|
|
|
|
[Type.Schedule]: '1000',
|
|
|
|
[Type.Semver]: '900',
|
2021-07-06 13:56:48 +02:00
|
|
|
[Type.Pep440]: '900',
|
2021-03-29 13:04:53 +02:00
|
|
|
[Type.Match]: '800',
|
|
|
|
[Type.Edge]: '700',
|
|
|
|
[Type.Ref]: '600',
|
|
|
|
[Type.Raw]: '200',
|
|
|
|
[Type.Sha]: '100'
|
|
|
|
};
|
|
|
|
|
|
|
|
export function Transform(inputs: string[]): Tag[] {
|
|
|
|
const tags: Tag[] = [];
|
|
|
|
if (inputs.length == 0) {
|
|
|
|
// prettier-ignore
|
|
|
|
inputs = [
|
|
|
|
`type=schedule`,
|
|
|
|
`type=ref,event=${RefEvent.Branch}`,
|
|
|
|
`type=ref,event=${RefEvent.Tag}`,
|
|
|
|
`type=ref,event=${RefEvent.PR}`
|
|
|
|
];
|
|
|
|
}
|
2021-04-03 18:15:27 +02:00
|
|
|
|
2021-03-29 13:04:53 +02:00
|
|
|
for (const input of inputs) {
|
|
|
|
tags.push(Parse(input));
|
|
|
|
}
|
2021-04-03 18:15:27 +02:00
|
|
|
const sorted = tags.sort((tag1, tag2) => {
|
2021-03-29 13:04:53 +02:00
|
|
|
if (Number(tag1.attrs['priority']) < Number(tag2.attrs['priority'])) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (Number(tag1.attrs['priority']) > Number(tag2.attrs['priority'])) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2021-04-03 18:15:27 +02:00
|
|
|
|
|
|
|
core.startGroup(`Processing tags input`);
|
|
|
|
for (const tag of sorted) {
|
|
|
|
core.info(tag.toString());
|
|
|
|
}
|
|
|
|
core.endGroup();
|
|
|
|
|
|
|
|
return sorted;
|
2021-03-29 13:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function Parse(s: string): Tag {
|
2022-04-25 13:35:08 +02:00
|
|
|
const fields = parse(s, {
|
2021-03-29 13:04:53 +02:00
|
|
|
relaxColumnCount: true,
|
2022-04-25 13:35:08 +02:00
|
|
|
skipEmptyLines: true
|
2021-03-29 13:04:53 +02:00
|
|
|
})[0];
|
|
|
|
|
2021-04-03 18:15:27 +02:00
|
|
|
const tag = new Tag();
|
2021-03-29 13:04:53 +02:00
|
|
|
for (const field of fields) {
|
2021-10-22 13:50:28 +02:00
|
|
|
const parts = field
|
|
|
|
.toString()
|
|
|
|
.split('=')
|
|
|
|
.map(item => item.trim());
|
2021-03-29 13:04:53 +02:00
|
|
|
if (parts.length == 1) {
|
2021-10-22 13:50:28 +02:00
|
|
|
tag.attrs['value'] = parts[0];
|
2021-03-29 13:04:53 +02:00
|
|
|
} else {
|
2021-10-22 13:50:28 +02:00
|
|
|
const key = parts[0].toLowerCase();
|
|
|
|
const value = parts[1];
|
2021-03-29 13:04:53 +02:00
|
|
|
switch (key) {
|
|
|
|
case 'type': {
|
|
|
|
if (!Object.values(Type).includes(value)) {
|
2021-07-05 20:37:02 +02:00
|
|
|
throw new Error(`Unknown tag type attribute: ${value}`);
|
2021-03-29 13:04:53 +02:00
|
|
|
}
|
|
|
|
tag.type = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
tag.attrs[key] = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag.type == undefined) {
|
|
|
|
tag.type = Type.Raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tag.type) {
|
|
|
|
case Type.Schedule: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'pattern')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['pattern'] = 'nightly';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-07-06 13:56:48 +02:00
|
|
|
case Type.Semver:
|
|
|
|
case Type.Pep440: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'pattern')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
throw new Error(`Missing pattern attribute for ${s}`);
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'value')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['value'] = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type.Match: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'pattern')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
throw new Error(`Missing pattern attribute for ${s}`);
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'group')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['group'] = '0';
|
|
|
|
}
|
|
|
|
if (isNaN(+tag.attrs['group'])) {
|
|
|
|
throw new Error(`Invalid match group for ${s}`);
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'value')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['value'] = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type.Edge: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'branch')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['branch'] = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type.Ref: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'event')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
throw new Error(`Missing event attribute for ${s}`);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!Object.keys(RefEvent)
|
|
|
|
.map(k => RefEvent[k])
|
|
|
|
.includes(tag.attrs['event'])
|
|
|
|
) {
|
|
|
|
throw new Error(`Invalid event for ${s}`);
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (tag.attrs['event'] == RefEvent.PR && !Object.prototype.hasOwnProperty.call(tag.attrs, 'prefix')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['prefix'] = 'pr-';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type.Raw: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'value')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
throw new Error(`Missing value attribute for ${s}`);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type.Sha: {
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'prefix')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['prefix'] = 'sha-';
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'format')) {
|
2021-05-11 20:14:23 +02:00
|
|
|
tag.attrs['format'] = ShaFormat.Short;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!Object.keys(ShaFormat)
|
|
|
|
.map(k => ShaFormat[k])
|
|
|
|
.includes(tag.attrs['format'])
|
|
|
|
) {
|
|
|
|
throw new Error(`Invalid format for ${s}`);
|
|
|
|
}
|
2021-03-29 13:04:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'enable')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['enable'] = 'true';
|
|
|
|
}
|
2022-03-22 21:09:00 +01:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'priority')) {
|
2021-03-29 13:04:53 +02:00
|
|
|
tag.attrs['priority'] = DefaultPriorities[tag.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|