/
tag.ts
214 lines (197 loc) · 4.89 KB
/
tag.ts
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
import csvparse from 'csv-parse/lib/sync';
import * as core from '@actions/core';
export enum Type {
Schedule = 'schedule',
Semver = 'semver',
Pep440 = 'pep440',
Match = 'match',
Edge = 'edge',
Ref = 'ref',
Raw = 'raw',
Sha = 'sha'
}
export enum RefEvent {
Branch = 'branch',
Tag = 'tag',
PR = 'pr'
}
export enum ShaFormat {
Short = 'short',
Long = 'long'
}
export class Tag {
public type?: Type;
public attrs: Record<string, string>;
constructor() {
this.attrs = {};
}
public toString(): string {
const out: string[] = [`type=${this.type}`];
for (let attr in this.attrs) {
out.push(`${attr}=${this.attrs[attr]}`);
}
return out.join(',');
}
}
export const DefaultPriorities: Record<Type, string> = {
[Type.Schedule]: '1000',
[Type.Semver]: '900',
[Type.Pep440]: '900',
[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}`
];
}
for (const input of inputs) {
tags.push(Parse(input));
}
const sorted = tags.sort((tag1, tag2) => {
if (Number(tag1.attrs['priority']) < Number(tag2.attrs['priority'])) {
return 1;
}
if (Number(tag1.attrs['priority']) > Number(tag2.attrs['priority'])) {
return -1;
}
return 0;
});
core.startGroup(`Processing tags input`);
for (const tag of sorted) {
core.info(tag.toString());
}
core.endGroup();
return sorted;
}
export function Parse(s: string): Tag {
const fields = csvparse(s, {
relaxColumnCount: true,
skipLinesWithEmptyValues: true
})[0];
const tag = new Tag();
for (const field of fields) {
const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) {
tag.attrs['value'] = parts[0];
} else {
const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'type': {
if (!Object.values(Type).includes(value)) {
throw new Error(`Unknown tag type attribute: ${value}`);
}
tag.type = value;
break;
}
default: {
tag.attrs[key] = value;
break;
}
}
}
}
if (tag.type == undefined) {
tag.type = Type.Raw;
}
switch (tag.type) {
case Type.Schedule: {
if (!tag.attrs.hasOwnProperty('pattern')) {
tag.attrs['pattern'] = 'nightly';
}
break;
}
case Type.Semver:
case Type.Pep440: {
if (!tag.attrs.hasOwnProperty('pattern')) {
throw new Error(`Missing pattern attribute for ${s}`);
}
if (!tag.attrs.hasOwnProperty('value')) {
tag.attrs['value'] = '';
}
break;
}
case Type.Match: {
if (!tag.attrs.hasOwnProperty('pattern')) {
throw new Error(`Missing pattern attribute for ${s}`);
}
if (!tag.attrs.hasOwnProperty('group')) {
tag.attrs['group'] = '0';
}
if (isNaN(+tag.attrs['group'])) {
throw new Error(`Invalid match group for ${s}`);
}
if (!tag.attrs.hasOwnProperty('value')) {
tag.attrs['value'] = '';
}
break;
}
case Type.Edge: {
if (!tag.attrs.hasOwnProperty('branch')) {
tag.attrs['branch'] = '';
}
break;
}
case Type.Ref: {
if (!tag.attrs.hasOwnProperty('event')) {
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}`);
}
if (tag.attrs['event'] == RefEvent.PR && !tag.attrs.hasOwnProperty('prefix')) {
tag.attrs['prefix'] = 'pr-';
}
break;
}
case Type.Raw: {
if (!tag.attrs.hasOwnProperty('value')) {
throw new Error(`Missing value attribute for ${s}`);
}
break;
}
case Type.Sha: {
if (!tag.attrs.hasOwnProperty('prefix')) {
tag.attrs['prefix'] = 'sha-';
}
if (!tag.attrs.hasOwnProperty('format')) {
tag.attrs['format'] = ShaFormat.Short;
}
if (
!Object.keys(ShaFormat)
.map(k => ShaFormat[k])
.includes(tag.attrs['format'])
) {
throw new Error(`Invalid format for ${s}`);
}
break;
}
}
if (!tag.attrs.hasOwnProperty('enable')) {
tag.attrs['enable'] = 'true';
}
if (!tag.attrs.hasOwnProperty('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;
}