-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
index.js
185 lines (161 loc) · 5.1 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
#!/usr/bin/env node
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var execSync = require('child_process').execSync;
function escape(value) {
return '\'' + value.replace(/'/g, "'\\''") + '\'';
}
var cwd = null;
function exec(command) {
console.error('>', command.replace(process.env.GITHUB_TOKEN, '************'));
var options = {
stdio: [null, null, null],
};
if (cwd) {
options.cwd = cwd;
}
return execSync(command, options).toString();
}
var isInsideOfTravis = !!process.env.TRAVIS_REPO_SLUG;
if (isInsideOfTravis) {
if (process.env.TRAVIS_BRANCH !== 'master') {
console.error('facts-tracker: Branch is not master, exiting...');
process.exit(0);
}
if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
console.error('facts-tracker: This is a pull request, exiting...');
process.exit(0);
}
if (!process.env.GITHUB_USER || !process.env.GITHUB_TOKEN) {
console.error(
'In order to use facts-tracker, you need to configure a ' +
'few environment variables in order to be able to commit to the ' +
'repository. Follow those steps to get you setup:\n' +
'\n' +
'Go to https://github.com/settings/tokens/new\n' +
' - Fill "Token description" with "facts-tracker for ' +
process.env.TRAVIS_REPO_SLUG + '"\n' +
' - Check "public_repo"\n' +
' - Press "Generate Token"\n' +
'\n' +
'In a different tab, go to https://travis-ci.org/' +
process.env.TRAVIS_REPO_SLUG + '/settings\n' +
' - Make sure "Build only if .travis.yml is present" is ON\n' +
' - Fill "Name" with "GITHUB_TOKEN" and "Value" with the token you ' +
'just generated. Press "Add"\n' +
' - Fill "Name" with "GITHUB_USER" and "Value" with the name of the ' +
'account you generated the token with. Press "Add"\n' +
'\n' +
'Once this is done, commit anything to the repository to restart ' +
'Travis and it should work :)'
);
process.exit(1);
}
exec(
'echo "machine github.com login $GITHUB_USER password $GITHUB_TOKEN"' +
'> ~/.netrc'
);
exec(
'git config --global user.name ' +
escape(process.env.GITHUB_USER_NAME || 'facts-tracker')
);
exec(
'git config --global user.email ' +
escape(process.env.GITHUB_USER_EMAIL || 'facts-tracker@no-reply.github.com')
);
}
if (process.argv.length <= 2) {
console.error('Usage: facts-tracker <name1> <value1> <name2> <value2>...');
process.exit(1);
}
function getRepoSlug() {
if (isInsideOfTravis) {
return process.env.TRAVIS_REPO_SLUG;
}
var remotes = exec('git remote -v').split('\n');
for (var i = 0; i < remotes.length; ++i) {
var match = remotes[i].match(/^origin\t[^:]+:([^\.]+).+\(fetch\)/);
if (match) {
return match[1];
}
}
console.error('Cannot find repository slug, sorry.');
process.exit(1);
}
var repoSlug = getRepoSlug();
var currentCommitHash = exec('git rev-parse HEAD').trim();
var currentTimestamp = new Date().toISOString()
.replace('T', ' ')
.replace(/\..+/, '');
function checkoutFactsFolder() {
var factsFolder = '../' + repoSlug.split('/')[1] + '-facts';
if (!fs.existsSync(factsFolder)) {
var escapedRepoURL;
if (isInsideOfTravis) {
escapedRepoURL = 'https://$GITHUB_TOKEN@github.com/' + escape(repoSlug) + '.git';
} else {
escapedRepoURL = escape('git@github.com:' + repoSlug + '.git');
}
exec(
'git clone ' +
'--branch facts ' +
'--depth=5 ' +
escapedRepoURL + ' ' +
escape(factsFolder)
);
}
cwd = path.resolve(factsFolder);
exec('git fetch');
if (exec('git status --porcelain')) {
console.error('facts-tracker: `git status` is not clean, aborting.');
process.exit(1);
}
exec('git rebase origin/facts');
}
checkoutFactsFolder();
for (var i = 2; i < process.argv.length; i += 2) {
var name = process.argv[i].trim();
var value = process.argv[i + 1];
if (value.indexOf('\n') !== -1) {
console.error(
'facts-tracker: skipping', name,
'as the value contains new lines:', value
);
continue;
}
var filename = name + '.txt';
try {
var lastLine = exec('tail -n 1 ' + escape(filename));
} catch (e) {
// ignore error
}
var lastValue = lastLine && lastLine
.replace(/^[^\t]+\t[^\t]+\t/, '') // commit hash \t timestamp \t
.slice(0, -1); // trailing \n
if (value !== lastValue) {
fs.appendFileSync(
path.resolve(cwd, filename),
currentCommitHash + '\t' + currentTimestamp + '\t' + value + '\n'
);
}
console.log(name);
console.log(lastValue);
console.log(value);
}
if (exec('git status --porcelain')) {
exec('git add --all');
exec('git commit -m ' + escape('Adding facts for ' + currentCommitHash));
exec('git push origin facts');
} else {
console.error('facts-tracker: nothing to update');
}
cwd = null;