-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_releases.js
More file actions
133 lines (127 loc) · 3.78 KB
/
sync_releases.js
File metadata and controls
133 lines (127 loc) · 3.78 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
const {Octokit} = require('octokit');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
let octokit = new Octokit();
let input_octokit = new Octokit();
const optionDefinitions = [
{
name: 'owner',
description: 'The owner of the github repository.',
alias: 'o',
type: String,
typeLabel: '{underline owner}'
},
{
name: 'repo',
description: 'The name of the github repository.',
alias: 'r',
type: String,
typeLabel: '{underline repo}'
},
{
name: 'auth',
description: 'Token for authorization at the github repository.',
alias: 'a',
type: String,
typeLabel: '{underline token}'
},
{
name: 'input-owner',
description: 'The owner of the input github repository.',
type: String,
typeLabel: '{underline owner}'
},
{
name: 'input-repo',
description: 'The name of the input github repository.',
type: String,
typeLabel: '{underline repo}'
},
{
name: 'input-auth',
description: 'Token for authorization at the input github repository.',
type: String,
typeLabel: '{underline token}'
},
{
name: 'help',
description: 'Display this usage guide.',
alias: 'h',
type: Boolean
}
];
const sections = [
{
header: 'BO4E Java Generator',
content: 'Synchronise releases of two GitHub repositories'
},
{
header: 'Synopsis',
content: [
'node sync_releases.js [{underline options}] {bold -o} {underline owner} {bold -r} {underline repo} {bold --input-owner} {underline owner} {bold --input-repo} {underline repo}',
'node sync_releases.js {bold --help}'
]
},
{
header: 'Options',
optionList: optionDefinitions
},
{
content: 'Project home: {underline https://github.com/TimoMolls/BO4E-Java-Generator}'
}
];
/**
* get the latest release of the given GitHub repository
* @param http {Octokit}
* @param owner {string}
* @param repo {string}
* @returns {Promise<any[]>}
*/
async function getReleases(http, owner, repo) {
const response = await http.request('GET /repos/{owner}/{repo}/releases', {
owner: owner,
repo: repo
});
if (response.status === 200) {
return response.data;
}
else {
throw new Error('Request failed: ' + response.status);
}
}
async function main() {
const options = commandLineArgs(optionDefinitions);
if (options['help']) {
console.log(commandLineUsage(sections));
process.exit();
}
const owner = options['owner'];
const repo = options['repo'];
if (options['auth']) {
octokit = new Octokit({auth: options['auth']});
}
const input_owner = options['input-owner'];
const input_repo = options['input-repo'];
if (options['input-auth']) {
input_octokit = new Octokit({auth: options['input-auth']});
}
const releases = await getReleases(octokit, owner, repo);
const input_releases = await getReleases(input_octokit, input_owner, input_repo);
const date = new Date();
const tags = [];
for (const input_release of input_releases) {
if (input_release['tag_name'].startsWith('v' + date.getFullYear()) || input_release['tag_name'].startsWith('v' + (date.getFullYear() - 1))) {
const index = releases.findIndex(release => release['tag_name'] === input_release['tag_name']);
if (index < 0) {
tags.push(input_release['tag_name']);
}
}
}
if (tags.length > 0) {
console.log(tags[tags.length - 1]);
}
else {
console.log('none');
}
}
main().then();