This repository was archived by the owner on Mar 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreateRepo.js
More file actions
220 lines (166 loc) · 5.57 KB
/
Copy pathcreateRepo.js
File metadata and controls
220 lines (166 loc) · 5.57 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
const config = require("../../config");
const debug = require('debug')('init:createRepo');
const Octokit = require('@octokit/rest');
const path = require('path');
const fs = require('mz/fs');
const run = require('../run');
const updateLocalRepo = require('../updateLocalRepo');
const _ = require('lodash');
const octokit = new Octokit({
auth: `token ${config.secret.github.token}`,
//log: console,
previews: ['hellcat-preview', 'mercy-preview'], // enables nested teams API
});
// if original local repo doesn't exist => create it
// if translated local repo exists => we're done
// otherwise
// if translated remote repo exists => clone it, set upstream
// otherwise create translated local and remote repos
module.exports = async function(langInfo) {
let originalPath = path.join(config.repoRoot, `${config.langMain}.${config.repoSuffix}`);
let existsOriginal = await fs.exists(originalPath);
debug("Local original repo exists: " + existsOriginal);
if (!existsOriginal) {
debug("Cloning local original repo");
await run(`git clone git@github.com:${config.org}/${config.langMain}.${config.repoSuffix}`, {
cwd: config.repoRoot
});
debug("Created local original repo");
}
const repo = `${langInfo.code}.${config.repoSuffix}`;
debug("Checking if remote repo exists");
const {data} = await octokit.search.repos({
q: `org:${config.org} "${repo}"`,
});
// when we search for "br.javascript.info", it also finds "pt-br.javascript.info", so check for exact match
let remoteRepoExists = data.items.some(item => item.full_name === `${config.org}/${repo}`);
if (remoteRepoExists) {
debug("Translated remote repo exists");
await setupRepo(langInfo);
await updateLocalRepo(langInfo.code);
debug("Translated local repo ready");
return;
}
/*
let localRepoPath = path.join(config.repoRoot, `${langInfo.code}.${config.repoSuffix}`);
let localRepoExists = await fs.exists(localRepoPath);
debug("Local translated repo exists: " + localRepoExists);
if (localRepoExists) {
debug("When local repo exists, we assume it's up to date");
return;
}
*/
debug("No translated remote and local repo: cloning, initializing");
await octokit.repos.createInOrg({
org: config.org,
name: repo,
description: `Modern JavaScript Tutorial in ${langInfo.name}`,
});
debug("Translated remote repo created");
// clear initial labels
let {data: labels} = await octokit.issues.listLabelsForRepo({
owner,
repo
});
// Allow repo to have own labels, just ensure it has standard labels too
for(let label of labels) {
await octokit.issues.deleteLabel({
owner: config.org,
repo,
name: label.name
});
}
debug("Labels cleared");
await setupRepo(langInfo);
debug("Cloning local translated repo");
await run(`git clone --no-local ${config.langMain}.${config.repoSuffix} ${langInfo.code}.${config.repoSuffix}`, {
cwd: config.repoRoot
});
debug("Created local translated repo");
await run(`git remote set-url origin git@github.com:${config.org}/${langInfo.code}.${config.repoSuffix}`, {
cwd: localRepoPath
});
await run(`git remote add upstream git@github.com:${config.org}/${config.langMain}.${config.repoSuffix}`, {
cwd: localRepoPath
});
debug("Pushing to remote");
await run(`git push -u origin master`, {
cwd: localRepoPath
});
debug("Translated remote repo is initialized");
};
async function setupRepo(langInfo) {
debug("setupRepo");
const repo = `${langInfo.code}.${config.repoSuffix}`;
const owner = config.org;
// Set topics
let langTopic = langInfo.name
.toLowerCase()
.replace(/[() ]/g, ' ')
.trim()
.replace(/ +/, '-');
await octokit.repos.replaceTopics({
owner,
repo,
names: ["javascript", "tutorial", langTopic]
});
// General repo settings
await octokit.repos.update({
owner,
repo,
name: repo,
has_projects: false,
has_wiki: false,
homepage: typeof langInfo.published === 'string' ? langInfo.published :
langInfo.published ? `https://${langInfo}.javascript.info` : `https://javascript.info`,
allow_merge_commit: true,
allow_squash_merge: false,
allow_rebase_merge: false
});
// Hooks
let {data: hooks} = await octokit.repos.listHooks({
owner,
repo
});
for (let hook of hooks) {
debug("Deleting old hook", hook);
await octokit.repos.deleteHook({
owner,
repo,
hook_id: hook.id
});
}
if (repo === 'test.javascript.info' || repo === 'ru.javascript.info') {
debug("Create new hook");
let {data: result} = await octokit.repos.createHook({
owner,
repo,
// by default only push
// events: ['pull_request', 'pull_request_review', 'pull_request_review_comment'],
events: ['*'],
config: {
url: repo === 'text.javascript.info' ? 'http://testhook.javascript.ru/hook' : 'https://translate.javascript.info/hook',
content_type: 'json',
secret: config.secret.github.hook
}
});
debug("Created hook", result);
// Labels
let {data: labels} = await octokit.issues.listLabelsForRepo({
owner,
repo
});
labels = _.keyBy(labels, 'name');
// Allow repo to have own labels, just ensure it has standard labels too
for(let label of Object.values(config.labels)) {
if (!labels[label.name]) {
debug("No such label, creating", label);
octokit.issues.createLabel({
owner,
repo,
...label
});
}
}
}
}