-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub-actions.js
More file actions
242 lines (232 loc) · 5.19 KB
/
github-actions.js
File metadata and controls
242 lines (232 loc) · 5.19 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
async function execa(command, params) {
const { spawn } = require('child_process');
const child = spawn(command, params);
let data = '';
for await (const chunk of child.stdout) {
//console.log('stdout chunk: ' + chunk);
data += chunk;
}
let error = '';
for await (const chunk of child.stderr) {
//console.error('stderr chunk: ' + chunk);
error += chunk;
}
const exitCode = await new Promise((resolve, reject) => {
child.on('close', resolve);
});
if (exitCode) {
throw new Error(`subprocess error exit ${exitCode}, ${error}`);
}
return data;
}
function getDependences(scope) {
const dependencies = {
xml: {
xml: true,
},
'only-complementos':{
complementos: true,
},
complementos:{
complementos: true,
xml: true
},
utils: {
pdf: true,
utils: true,
},
csd: {
csd: true,
xml: true,
},
csf: {
csf: true,
},
openssl: {
openssl: true,
csd: true,
xml: true,
},
saxon: {
saxon: true,
xml: true,
},
catalogs: {
catalogs: true,
xml: true,
},
curp: {
curp: true,
},
pdf: {
pdf: true,
},
rfc: {
rfc: true,
},
xsd: {
xsd: true,
xml: true,
complementos: true,
},
transform: {
transform: true,
xml: true
},
types: {
types: true,
xml: true,
complementos: true
},
elements: {
elements: true,
xml: true,
complementos: true,
transform: true
},
expresiones: {
expresiones: true
},
'2json': {
'2json': true
},
designs: {
designs: true
},
sat: {
sat: true
},
estado: {
estado: true
},
validador: {
validador: true
},
cleaner: {
cleaner: true
},
auth: {
auth: true
},
descarga: {
descarga: true
},
cancelacion: {
cancelacion: true
},
recursos: {
recursos: true
},
scraper: {
scraper: true
},
opinion: {
opinion: true
},
contabilidad: {
contabilidad: true
},
captcha: {
captcha: true
},
retenciones: {
retenciones: true
},
pacs: {
pacs: true
},
banxico: {
banxico: true
},
diot: {
diot: true
}
};
return dependencies[scope] || {};
}
function getScopes(commits = []) {
const list = ['catalogs','csd','csf','curp','pdf','rfc','utils','xml','complementos','openssl','saxon','xsd','2json','designs','sat','estado','validador','cleaner','auth','transform','expresiones','elements','types','descarga','cancelacion','recursos','scraper','opinion','contabilidad','captcha','retenciones','pacs','banxico','diot']
const onlys = {
'only-complementos': 'complementos'
}
let scopes = {};
for (var i = 0; i < commits.length; i++) {
const commit = commits[i];
const message = commit.message;
const [type, msg] = message.split(':');
const findScope = type.match(/\(([^)]+)\)/g);
if (findScope) {
const scope = findScope.pop().replace(/[{()}]/g, '');
const onlyScope = onlys[scope] || scope
if (list.includes(onlyScope)) {
scopes = {
...scopes,
...getDependences(scope)
}
}
}
}
return Object.keys(scopes);
}
async function getCommitsPR(url) {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-type': 'application/json',
},
});
if (!response.ok) {
throw new Error('Error get list prices');
}
const data = await response.json();
return data;
} catch (error) {
console.error(`ERROR => ${url}`, error);
return [];
}
}
module.exports = async ({ github, context, core }) => {
// For pull_request events, use the target branch (base_ref), not the PR ref
const branch = context.payload.pull_request
? context.payload.pull_request.base.ref
: context.ref.split('/').slice(2).join('/');
console.log("branch:", branch);
console.log("eventName:", context.eventName);
const branchs = ['next','beta', 'alpha','dev']
const eventName = context.eventName
let commits = context.payload.commits || [];
if(eventName==='pull_request'){
const { owner, repo } = context.repo;
const pull_number = context.payload.pull_request.number;
console.log(`Fetching commits for PR #${pull_number}...`);
const { data: commits_local } = await github.rest.pulls.listCommits({
owner,
repo,
pull_number,
per_page: 100,
});
console.log(`Found ${commits_local.length} commits in PR`);
commits = commits_local.map(({commit})=>commit)
}
const scopes = getScopes(commits);
console.log("commits", scopes);
for (var i = 0; i < scopes.length; i++) {
const scope = scopes[i];
const comands = [
'version',
'--version-policy',
scope,
'--bump',
]
if (branchs.includes(branch)) {
comands.push('--override-bump');
comands.push('prerelease');
comands.push('--override-prerelease-id');
comands.push(branch);
}
console.log(comands);
const data = await execa('rush', comands);
console.log(data);
}
};