-
Notifications
You must be signed in to change notification settings - Fork 39
/
copyAndPush.js
216 lines (192 loc) · 5.98 KB
/
copyAndPush.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
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
const fs = require( 'fs' );
const path = require( 'path' );
const child_process = require( 'child_process' );
const rimraf = require('rimraf');
const copydir = require('copy-dir');
const sourcePath = process.argv[2];
const branch = process.argv[3].replace(/^origin\//, '');
const token = process.argv[4];
const git = process.argv[5] || 'git';
const jenkins_job_name = process.env['JOB_NAME'] || ' ';
const jenkins_job_build_number = process.env['BUILD_NUMBER'] || ' ';
let branchPath = path.join(__dirname, branch);
if(!fs.existsSync(branchPath)) {
fs.mkdirSync(branchPath);
}
function getPaths(file) {
return new Promise((resolve, reject) => {
let generatedPath = path.join(sourcePath, file);
fs.stat(generatedPath, function(error, stat) {
if( error ) {
reject(`Error stating path: ${generatedPath}`, error);
return;
}
if(!stat.isDirectory()) {
reject(`Generated ${generatedPath} is not a directory`);
return;
}
let repo = 'KalturaOttGeneratedAPIClients' + file.substr(0, 1).toUpperCase() + file.substr(1);
let gitPath = path.join(__dirname, branch, repo);
fs.exists(gitPath, (exists) => {
if(exists) {
resolve({
generatedPath: generatedPath,
gitPath: gitPath
});
}
else {
gitCloneBranch(repo)
.then(() => {
resolve({
generatedPath: generatedPath,
gitPath: gitPath
});
}, (err) => {
gitClone(repo)
.then(() => {
resolve({
generatedPath: generatedPath,
gitPath: gitPath
});
}, (err) => {
reject(`Failed to clone repo ${repo}: ${err}`);
process.exit(1);
});
});
}
})
});
});
}
function execWithPomise(command, cwd, resolveData, alwaysResolve) {
if(!resolveData) {
resolveData = cwd;
}
return new Promise((resolve, reject) => {
child_process.exec(command, {
cwd: cwd,
maxBuffer: 1024 * 5000
}, (err, stdout, stderr) => {
if(!err) {
console.log(`Command [${command}] executed successfully in ${cwd}`);
}
if(err && !alwaysResolve) {
reject(`Failed to execute [${command}] in folder ${cwd}, code [${err.code}]: ${err}`);
}
else {
resolve(resolveData);
}
});
});
}
function gitClone(repo) {
console.log(`Cloning git repo ${repo}`);
return execWithPomise(`${git} clone https://${token}@github.com/kaltura/${repo}`, branchPath);
}
function gitCloneBranch(repo) {
console.log(`Cloning git repo ${repo}, branch ${branch}`);
return execWithPomise(`${git} clone -b ${branch} https://${token}@github.com/kaltura/${repo}`, branchPath);
}
function gitCheckout(generatedPath, gitPath, isNew) {
console.log(`Checking out git ${gitPath} branch ${branch}`);
return execWithPomise(git + ' checkout -B ' + branch, gitPath, {
generatedPath: generatedPath,
gitPath: gitPath
});
}
function gitPull(generatedPath, gitPath) {
console.log(`Pulling files to git ${gitPath} branch ${branch}`);
return execWithPomise(git + ' pull origin ' + branch, gitPath, {
generatedPath: generatedPath,
gitPath: gitPath
}, true);
}
function copyFiles(generatedPath, gitPath) {
return new Promise((resolve, reject) => {
console.log(`Copy from ${generatedPath} to ${gitPath}`);
rimraf(gitPath + '/*', () => {
copydir(generatedPath, gitPath, (err) => {
if(err) {
reject(`Failed to copy directory from ${generatedPath} to ${gitPath}: ` + err);
process.exit(1);
}
else {
resolve(gitPath);
}
});
});
});
}
function gitAdd(gitPath) {
console.log(`Adding files to git ${gitPath}`);
return execWithPomise(git + ' add -A', gitPath);
}
function gitCommit(gitPath) {
console.log(`Commiting files to git ${gitPath}`);
return execWithPomise(git + ` commit -m "Auto-generated by Jenkins job ${jenkins_job_name}/${jenkins_job_build_number}, branch ${branch}" || echo 'No changes to commit' `, gitPath);
}
function gitPush(gitPath) {
console.log(`Pushing files to git ${gitPath} branch ${branch}`);
return execWithPomise(git + ' tag -d ' + branch, gitPath).then((gitPath) => {
return execWithPomise(git + ' push origin ' + branch, gitPath);
}).catch((err) => {
return execWithPomise(git + ' push origin ' + branch, gitPath);
});
}
let handledFiles = 0;
function startHandlingFile() {
handledFiles++;
}
function stopHandlingFile() {
handledFiles--;
if(!handledFiles) {
process.exit(0);
}
}
function gitTagAndPush(gitPath) {
if (process.argv.length>6)
{
var tag = process.argv[6];
console.log(`Tagging repo ${gitPath} branch ${branch} tag ${tag}`);
return execWithPomise(git + ' tag -f ' + tag, gitPath).then((gitPath) => {
return execWithPomise(git + ' push -f --tags ', gitPath);
});
}
}
fs.readdir(sourcePath, (err, files) => {
if(err) {
console.error(`Could not list the directory: ${sourcePath}`, err);
process.exit(1);
}
files.forEach((file, index) => {
startHandlingFile();
getPaths(file)
.then(({generatedPath, gitPath}) => {
return gitCheckout(generatedPath, gitPath);
})
.then(({generatedPath, gitPath}) => {
return gitPull(generatedPath, gitPath);
})
.then(({generatedPath, gitPath}) => {
return copyFiles(generatedPath, gitPath);
})
.then((gitPath) => {
return gitAdd(gitPath);
})
.then((gitPath) => {
return gitCommit(gitPath);
})
.then((gitPath) => {
return gitPush(gitPath);
})
.then((gitPath) => {
return gitTagAndPush(gitPath);
})
.then((gitPath) => {
stopHandlingFile();
}, (err) => {
console.error(err);
stopHandlingFile();
});
});
});