-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.js
177 lines (150 loc) · 4.75 KB
/
migrate.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
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
const exec = require('child_process').execSync
const dotenv = require('dotenv')
const execThunk = command => _ => exec(command)
const axios = require('axios')
const links = require('./links.json')
const arg = process.argv[2];
const fs = require('fs');
class MigrateRepo {
constructor() {
dotenv.config();
this.token = process.env.token
this.orgName = process.env.org_name
this.githubURL = process.env.github_url
this.links = links;
this.getURL = `${this.githubURL}/api/v3/orgs/${this.orgName}/repos`;
this.determineAction(arg);
}
checkIfExists(links) {
// gets a list of all repos in the org
// console.log(links);
axios.get(this.getURL, {
params: {
access_token: this.token
}
})
.then(res => {
// store response for posterity
fs.writeFileSync('res.json', JSON.stringify(res.data), 'utf8');
// get github urls for each repo in the org from response object
const urlList = res.data.map(singleRepo => singleRepo.html_url)
console.log('got list of repos from github');
// check to see if github repo name exists in our links.json file
this.links.forEach(link => {
const repoTitle = this.splitName(link);
if(urlList.some(url => this.splitName(url) === repoTitle)) {
// if repo size > 0, skip
// means that it's in github and is not empty.
if(this.checkSize(repoTitle, res.data)) {
console.log(`${repoTitle} repo exists in github, skipping`)
return;
}
else {
console.log(`${repoTitle} repo exists but is empty, cloning and adding remote`)
const newRepoURL = this.matchOldRepoToNew(link, urlList)
this.cloneRepo(link);
this.addRemote(link, newRepoURL);
}
}
else {
// else create the repo which also does addRemote
console.log(`${repoTitle} doesnt exist in github`);
this.cloneRepo(link);
this.createRepo(repoTitle);
}
})
})
.catch(err => {
console.error(err)
console.log(this.getURL)
})
}
determineAction(arg) {
this.cacheCredentials();
switch(arg) {
case 'clone':
this.checkIfExists(this.links);
break;
case 'delete':
this.deleteEverything();
break;
default:
return console.error('please specify "clone" or "delete" as an argument')
}
}
deleteEverything() {
axios.get(this.getURL)
.then(res => {
res.data.forEach(url => {
let deleteURL = `${this.githubURL}/api/v3/repos/${this.orgName}/${this.splitName(url.html_url)}?access_token=${this.token}`
axios.delete(deleteURL)
.then(success => {
console.log(`deleted ${url}`)
})
.catch(err => {
console.error(err.response);
})
})
})
}
createRepo(repo) {
const postURL = `${this.githubURL}/api/v3/orgs/${this.orgName}/repos?access_token=${this.token}`
axios
.post(postURL, {"name": repo})
.then(res => {
console.log(`${res.data.url} created`)
const repoName = this.splitName(res.data.url);
this.addRemote(repo, repoName);
})
.catch(error => console.error(error.response.data))
}
checkSize(repoName, githubList) {
let exists = false;
// find index of reponame in links.json
githubList.forEach(g => {
if(g.name === repoName) {
if(g.size > 0) {
exists = true;
}
}
})
return exists;
}
remote(repo) { return `${this.githubURL}/${this.orgName}/${repo}.git` }
cloneRepo(oldRepo) {
console.log(`cloning ${oldRepo}`);
let repoName = this.splitName(oldRepo)
this.tryMe(execThunk(`git clone ${oldRepo} $(pwd)/repos/${repoName}`))
}
matchOldRepoToNew(oldRepo, newRepoList) {
let newRepo = newRepoList.find(rep => {
let old = this.splitName(oldRepo);
return rep.includes(old);
})
return newRepo;
}
addRemote(oldRepo, newRepo) {
let repoName = this.splitName(oldRepo)
console.log(`adding remote to repo: ${repoName}`);
let dirPath = `$(pwd)/repos/${oldRepo}`;
this.tryMe(execThunk(`cd ${dirPath} && git pull origin *:*`))
this.tryMe(execThunk(`cd ${dirPath} && git remote add upstream ${this.remote(newRepo)}`))
this.tryMe(execThunk(`cd ${dirPath} && git push -u upstream *:*`))
}
cacheCredentials() {
this.tryMe(execThunk(`git config --global credential.helper cache`))
}
tryMe (tryThis) {
try {
tryThis()
} catch (error) {
console.error(`☠️ PID ${error.pid} exited with status code ${error.status}`)
}
}
splitName(repo) {
let arr = repo.split('/')
const name = arr[arr.length - 1];
return name;
}
}
const makeit = new MigrateRepo()