generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
117 lines (94 loc) · 3.31 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const yaml = require('js-yaml');
/**
* Run the action.
* @param _local True to test locally with `node`. Requires a valid GITHUB_TOKEN
* environment variable with minimal permissions.
* @param _lib Library to test locally, such as "gz-math"
* @param _branch Branch to test locally, such as "gz-math7"
*/
async function run(_local, _lib, _branch) {
try {
if (!_local && github.context.payload.pull_request === undefined) {
core.debug('Labeler action must be run for pull requests.');
return;
}
let library = _local ? _lib : github.context.payload.repository.name;
const target = _local ? _branch : github.context.payload.pull_request.base.ref;
const token = _local ? process.env.GITHUB_TOKEN :
core.getInput('github-token', { required: true });
if (!token) {
core.debug('Failed to get token');
return;
}
const gh = new github.GitHub(token);
const owner = 'ignition-tooling';
const repo = 'gazebodistro';
let labels = [];
const collections = [
{name: 'citadel', label: '🏰 citadel'},
{name: 'fortress', label: '🏯 fortress'},
{name: 'garden', label: '🌱 garden'},
{name: 'harmonic', label: '🎵 harmonic'},
{name: 'ionic', label: '🏛️ ionic'},
{name: 'jetty', label: '🪵 jetty'}
];
for (const collection of collections) {
const path = 'collection-' + collection.name + '.yaml';
const collectionRes = await gh.repos.getContents({owner, repo, path});
const collectionContent = Buffer.from(collectionRes.data.content, 'base64').toString();
const collectionYaml = yaml.safeLoad(collectionContent);
let lib = collectionYaml.repositories[library];
if (lib == undefined)
{
// TODO(chapulina) Remove this after gz rename is over
library = library.replace('gz', 'ign');
lib = collectionYaml.repositories[library];
if (lib == undefined)
{
continue;
}
}
if (lib.version == target) {
labels.push(collection.label);
}
}
const classicVersions = [
{name: 'gazebo9', label: 'Gazebo 9️'},
{name: 'gazebo11', label: 'Gazebo 1️1️'},
];
for (const version of classicVersions) {
const path = version.name + '.yaml';
const versionRes = await gh.repos.getContents({owner, repo, path});
const versionContent = Buffer.from(versionRes.data.content, 'base64').toString();
const versionYaml = yaml.safeLoad(versionContent);
let lib = versionYaml.repositories[library];
if (lib == undefined)
{
continue;
}
if (lib.version == target) {
labels.push(version.label);
}
}
if (_local) {
labels.forEach((_label) => {
console.log(_label);
});
}
if (!_local && labels.length > 0) {
const prNumber = github.context.payload.pull_request.number;
core.debug(`Adding labels: [${labels}] to PR [${prNumber}]`);
gh.issues.addLabels(
Object.assign({issue_number: prNumber, labels: labels },
github.context.repo));
}
}
catch (error) {
core.setFailed(error.message);
}
}
run()
// Uncomment and change input to test locally
// run(true, "gz-launch", "main")