-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
200 lines (180 loc) · 5.43 KB
/
index.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
var bluebird = require('bluebird');
var execSync = require('child_process').execSync;
var fs = require('fs');
var githubApi = require('node-github');
var path = require('path');
function GitHubCreator(user) {
var github = new githubApi({
debug: true,
protocol: "https",
host: "api.github.com",
pathPrefix: "/api/v3", // for some GHEs; none for GitHub
version: '3.0.0',
headers: {
"user-agent": "nodejs" // GitHub require it
},
Promise: bluebird,
followRedirects: false, // default: true;
// there's currently an issue with non-get redirects, so allow ability to disable follow-redirects
timeout: 5000
});
if (user.token) {
github.authenticate({
type: 'oauth',
token: user.token
});
} else if (user.name && user.passwd) {
github.authenticate({
ype: 'basic',
username: user.name,
password: user.passwd
});
} else {
throw ('No authenticate!');
}
return github;
}
function getRepoUrl(repo, user) {
if (!repo || !repo.owner || !repo.name) {
throw ('Missing repo option');
}
return 'https://' + user.name + ':' + (user.token || user.passwd) + '@github.com/' + repo.owner + '/' + repo.name;
}
function getFolderName(repo) {
return '.' + repo.name;
}
// clone the repo and direct to it.
function chdirToRepo(repo, user) {
var folder = getFolderName(repo);
if (!fs.existsSync(folder)) {
// clone the repo, require user.name here
execSync('git clone ' + getRepoUrl(repo, user) + ' ' + folder);
}
process.chdir(folder);
}
function pushChange(repo, branchName, user) {
execSync('git add --all');
execSync('git commit -m"Auto modify files"');
execSync('git push ' + getRepoUrl(repo, user) + ' ' + branchName + ':' + branchName);
}
function branchNameGenerator(branch, number) {
return branch.prefix + '-' + (branch.startNumber + number);
}
function branchSwitch(defaultBranch, branch) {
execSync('git checkout .');
execSync('git checkout ' + defaultBranch);
execSync('git pull origin ' + defaultBranch);
try {
execSync('git branch ' + branch);
} catch (err) {
console.error(err.message || err);
}
try {
execSync('git checkout ' + branch);
execSync('git pull origin ' + branch);
} catch (err) {
console.error(err.message || err);
}
}
function modifyContent(modifyCount, modifyDir, modifyFilePattern) {
// modify file
for (var i = 0; i < modifyCount; i++) {
try {
var filename = getFileRandom(modifyDir);
modifyRandom(filename);
} catch (err) {
console.error(err.message || err);
}
}
function getFileRandom(dir) {
var files = fs.readdirSync(dir);
var item = files[Math.floor(Math.random() * files.length)];
var filename = dir + '/' + item;
// ignore .git
if (filename.startsWith('./.git')) {
return;
}
var state = fs.lstatSync(filename);
if (state.isFile()) { /* file */
if (filename.indexOf(modifyFilePattern) >= 0) {
return filename;
} else {
return;
}
} else if (state.isDirectory()) { /* dir */
return getFileRandom(filename);
}
}
function modifyRandom(filename) {
filename = filename || 'tempfile.txt';
console.log('modify ' + filename);
execSync('echo I m a robot! I do nothing! >> ' + filename);
}
}
(function entry(config) {
var options;
try {
options = require(config);
} catch (err) {
console.error(err.message || err);
console.log('Run `npm install` to seup up your config');
}
var github = GitHubCreator(options.user);
var repo = options.repo;
var user = options.user;
chdirToRepo(repo, user);
// will spell branch name with branch.prefix + (branch.startNumber + count)
// check the value is already set
var branch = options.branch;
if (!branch || !branch.prefix) {
throw ('Missing branch option');
}
branch.startNumber = branch.startNumber || 0;
var base = options.base;
if (!base || !base.branch) {
throw ('Missing base option');
}
if (!base.owner) {
console.log('base owner not specific, use ' + repo.owner);
base.owner = repo.owner;
}
var pr = options.pr;
if (!pr || !pr.title) {
throw ('Missing pr option');
}
var branches = [];
// github, branchName, modifyCount, repo, title, base
for (var i = 0; i < options.count; i++) {
var branchName = branchNameGenerator(branch, i);
branches.push(branchName);
}
var promise = bluebird.resolve();
branches.forEach((branchName) => {
promise = promise.delay(options.period).then(() => {
return new bluebird((resolve /*, reject*/ ) => {
// switch branch
branchSwitch(base.branch, branchName);
// modify file
modifyContent(options.modifyCount, options.modifyDir, options.modifyFilePattern);
// commit and push
pushChange(repo, branchName, user);
// creat pr
github.pullRequests.create({
user: base.owner,
repo: repo.name,
title: pr.title,
body: pr.body,
head: repo.owner + ':' + branchName,
base: base.branch,
}, (err, res) => {
if (err) {
console.error(err);
} else {
console.log(res);
}
resolve();
});
});
});
});
})('./pr-config.json');