-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
62 lines (53 loc) · 1.6 KB
/
run.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
const GithubAPI = require('./api');
const getConfig = require('./config');
/**
* Rename master branch to main for your owned repositories
* @param {string} source
* @param {string} dest
* @param {object} options
*/
async function run(source, dest, options) {
try {
const config = getConfig();
const api = new GithubAPI(config.auth);
const action = async (repo) => {
const branchs = await api.listOfBranchs(repo.name, config.owner);
const sourceBranch = branchs.find((b) => b.name === source);
if (sourceBranch) {
if (branchs.indexOf((b) => b.name === dest) === -1) {
// create main branch
await api.createBranch(
repo.name,
config.owner,
dest,
sourceBranch.commit.sha
);
}
// set default branch
if (setDefault) {
await api.setDefaultBranch(dest, repo.name, config.owner);
}
if (setDefault || repo.default_branch !== source) {
// delete source branch
await api.deleteBranch(repo.name, config.owner, source);
}
}
};
if (options.setAll) {
// get repositories
const repositories = await api.getUserRepos();
// ignore fork
const repos = repositories.filter((r) => r.fork === false);
repos.forEach(action);
} else {
const repo = await api.getUserRepo(options.repo, config.owner);
if (repo.fork) {
throw new Error('Currently ignore fork repository');
}
await action(repo);
}
} catch (error) {
console.log(error.message);
}
}
module.exports = run;