-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunUpdateGithubRepos.js
165 lines (142 loc) · 4.98 KB
/
runUpdateGithubRepos.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
const fs = require("fs")
const path = require("path")
let ENVIRONMENT = require('../Environment')
const simpleGit = require("simple-git")
const {Octokit} = require("@octokit/rest");
const {resolve} = require("path");
let originUserName
const checkDirectory = (directory) => {
return fs.existsSync(directory)
}
async function checkRepository(remote, git) {
const origin = remote.find(remote => remote.name === 'origin')
if (!origin) {
console.log('[ERROR] There are no remotes configured for this repository. Please add a remote to this repository and try again.');
return false
}
originUserName = origin.refs.fetch.split('/')[3];
const upstream = remote.find(remote => remote.name === 'upstream')
let upstreamUsername
//if there is no upstream, then we are adding remote
if (!upstream) {
const repo = origin.refs.fetch.split('/')[4];
await git.addRemote('upstream', `https://github.com/Superalgos/${repo}.git`);
upstreamUsername = 'Superalgos';
} else {
upstreamUsername = upstream.refs.fetch.split('/')[3];
}
if (upstreamUsername !== 'Superalgos') {
console.log('[ERROR] Upstream repository username is wrong!: ' + upstreamUsername)
return false;
}
return true;
}
function getCred() {
let secretsDiv = global.env.PATH_TO_SECRETS
if (fs.existsSync(secretsDiv)) {
let rawFile = fs.readFileSync(secretsDiv + '/githubCredentials.json')
return JSON.parse(rawFile)
}
}
function setSafeDirectory(git, cloneDir) {
git.getConfig('safe.directory', 'global', (err, config) => {
if (err) {
console.log(err)
throw err
} else {
if (config !== undefined) {
for (const scope of config.scopes) {
if (scope[1] !== undefined) {
if (!scope[1].includes(cloneDir.replaceAll('\\', '/'))) {
git.addConfig('safe.directory', cloneDir.replaceAll('\\', '/'), true, 'global')
console.log('[INFO] Add global config for safe.directory ' + cloneDir.replaceAll('\\', '/'))
}
} else {
git.addConfig('safe.directory', cloneDir.replaceAll('\\', '/'), true, 'global')
console.log('[INFO] Add global config for safe.directory ' + cloneDir.replaceAll('\\', '/'))
}
}
}
}
})
}
function updateRepo(cloneDir, repo) {
const options = {
baseDir: cloneDir,
binary: 'git',
maxConcurrentProcesses: 6,
};
const git = simpleGit(options)
setSafeDirectory(git, cloneDir);
git.getRemotes(true).then(remote => {
checkRepository(remote, git).then(r => {
if (!r) {
return false
}
})
}).catch((err => {
console.log(err)
}));
git.status((err) => {
if (!err) {
const branch = 'develop'
git.fetch('upstream', branch)
const token = getCred().githubToken
const octokit = new Octokit({
auth: token,
userAgent: 'Superalgos'
})
octokit.git.getRef({
owner: 'Superalgos',
repo: repo,
ref: `heads/${branch}`
}).catch((err) => {
console.log(err)
}).then(value => {
const sha = value.data.object.sha
octokit.git.updateRef({
owner: originUserName,
repo: repo,
ref: `heads/${branch}`,
sha: sha,
force: true
}).catch((err) => {
console.log(err)
}).then(() => {
git.pull('origin', branch)
})
})
} else {
console.log(err)
}
})
console.log(' ')
console.log('[INFO] Updated (Pull) plugin repo from upstream Directory: ' + cloneDir)
}
const updateFromUpstreamPromise = async () => {
let dir = process.cwd()
new Promise(() => {
// update plugin repos
for (const propertyName in global.env.PROJECT_PLUGIN_MAP) {
let cloneDir = path.join(global.env.PATH_TO_PLUGINS, global.env.PROJECT_PLUGIN_MAP[propertyName].dir)
let repo = global.env.PROJECT_PLUGIN_MAP[propertyName].repo
if (!checkDirectory(cloneDir)) {
console.log(`[INFO] Please run node setupPlugins <username> <github_token> to setup the plugins.`)
return 'No Plugins'
}
updateRepo(cloneDir, repo);
}
//update main repo
updateRepo(dir, 'Superalgos');
console.log('')
console.log('[INFO] All repositories are updated.')
resolve()
})
}
const run = () => {
global.env = ENVIRONMENT.newEnvironment()
updateFromUpstreamPromise()
}
module.exports = {
run
}