forked from SonarSource/sonarlint-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
236 lines (220 loc) · 7.24 KB
/
gulpfile.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* --------------------------------------------------------------------------------------------
* SonarLint for VisualStudio Code
* Copyright (C) 2017-2019 SonarSource SA
* sonarlint@sonarsource.com
* Licensed under the LGPLv3 License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
const gulp = require('gulp');
const artifactoryUpload = require('gulp-artifactory-upload');
const del = require('del');
const vsce = require('vsce');
const gutil = require('gulp-util');
const fs = require('fs');
const crypto = require('crypto');
const through = require('through2');
const request = require('request');
const bump = require('gulp-bump');
const dateformat = require('dateformat');
const sonarqubeScanner = require('sonarqube-scanner');
//...
gulp.task('clean', () => {
del.sync('*.vsix');
del.sync('server');
});
gulp.task('update-version', function() {
const buildNumber = process.env.TRAVIS_BUILD_NUMBER;
const packageJSON = getPackageJSON();
const version = packageJSON.version;
if (version.endsWith('-SNAPSHOT') && buildNumber) {
return gulp
.src('./package.json')
.pipe(bump({ version: version.replace('-SNAPSHOT', '-build.' + buildNumber) }))
.pipe(gulp.dest('./'));
} else {
gutil.log(`Not modifying version ${version} with build number ${buildNumber}`);
return Promise.resolve();
}
});
gulp.task('package', ['update-version'], vsce.createVSIX);
function getPackageJSON() {
return JSON.parse(fs.readFileSync('package.json'));
}
const hashes = {
sha1: '',
md5: ''
};
gulp.task('compute-hashes', ['package'], function() {
return gulp.src('*.vsix').pipe(hashsum());
});
gulp.task('deploy-vsix', ['package', 'compute-hashes'], function() {
if (process.env.TRAVIS_BRANCH !== 'master') {
gutil.log('Not on master, skip deploy-vsix');
return Promise.resolve();
}
const {
ARTIFACTORY_URL,
ARTIFACTORY_DEPLOY_REPO,
ARTIFACTORY_DEPLOY_USERNAME,
ARTIFACTORY_DEPLOY_PASSWORD,
TRAVIS_COMMIT,
TRAVIS_BRANCH,
TRAVIS_BUILD_NUMBER
} = process.env;
const packageJSON = getPackageJSON();
const { version, name } = packageJSON;
const artifactoryTargetUrl =
`${ARTIFACTORY_URL}/${ARTIFACTORY_DEPLOY_REPO}/org/sonarsource/sonarlint/vscode/${name}/${version}`;
return gulp
.src('*.vsix')
.pipe(
artifactoryUpload({
url: artifactoryTargetUrl,
username: ARTIFACTORY_DEPLOY_USERNAME,
password: ARTIFACTORY_DEPLOY_PASSWORD,
properties: {
'vcs.revision': TRAVIS_COMMIT,
'vcs.branch': TRAVIS_BRANCH,
'build.name': name,
'build.number': TRAVIS_BUILD_NUMBER
},
request: {
headers: {
'X-Checksum-MD5': hashes.md5,
'X-Checksum-Sha1': hashes.sha1
}
}
})
)
.on('error', gutil.log);
});
gulp.task('deploy-buildinfo', ['compute-hashes'], function() {
if (process.env.TRAVIS_BRANCH !== 'master') {
gutil.log('Not on master, skip deploy-buildinfo');
return Promise.resolve();
}
const packageJSON = getPackageJSON();
const { version, name } = packageJSON;
const buildNumber = process.env.TRAVIS_BUILD_NUMBER;
return request
.put(
{
url: process.env.ARTIFACTORY_URL + '/api/build',
json: buildInfo(name, version, buildNumber, hashes)
},
function(error, response, body) {
if (error) {
gutil.log('error:', error);
}
}
)
.auth(process.env.ARTIFACTORY_DEPLOY_USERNAME, process.env.ARTIFACTORY_DEPLOY_PASSWORD, true);
});
gulp.task('deploy', ['deploy-buildinfo', 'deploy-vsix'], function() {});
function snapshotVersion() {
const buildNumber = process.env.TRAVIS_BUILD_NUMBER;
const packageJSON = getPackageJSON();
const version = packageJSON.version;
const buildIdx = version.lastIndexOf('-');
if (buildIdx >= 0 && buildNumber) {
return version.substr(0, buildIdx) + '-SNAPSHOT';
}
return version;
}
gulp.task('sonarqube', callback => {
if (process.env.TRAVIS_BRANCH === 'master' && process.env.TRAVIS_PULL_REQUEST === 'false') {
runSonnarQubeScanner(callback, {
'sonar.analysis.sha1': process.env.TRAVIS_COMMIT
});
} else if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
runSonnarQubeScanner(callback, {
'sonar.pullrequest.branch': process.env.TRAVIS_PULL_REQUEST_BRANCH,
'sonar.pullrequest.base': process.env.TRAVIS_BRANCH,
'sonar.pullrequest.key': process.env.TRAVIS_PULL_REQUEST,
'sonar.pullrequest.provider': 'github',
'sonar.pullrequest.github.repository': process.env.TRAVIS_REPO_SLUG,
'sonar.analysis.prNumber': process.env.TRAVIS_PULL_REQUEST,
'sonar.analysis.sha1': process.env.TRAVIS_PULL_REQUEST_SHA
});
}
});
function runSonnarQubeScanner(callback, options = {}) {
const commonOptions = {
'sonar.projectKey': 'org.sonarsource.sonarlint.vscode:sonarlint-vscode',
'sonar.projectName': 'SonarLint for VSCode',
'sonar.projectVersion': snapshotVersion(),
'sonar.exclusions': 'build/**, out/**, coverage/**, node_modules/**, **/node_modules/**',
'sonar.typescript.lcov.reportPaths': 'coverage/lcov.info',
'sonar.coverage.exclusions': 'gulpfile.js, build/**, config/**, coverage/**, scripts/**',
'sonar.analysis.buildNumber': process.env.TRAVIS_BUILD_NUMBER,
'sonar.analysis.pipeline': process.env.TRAVIS_BUILD_NUMBER,
'sonar.analysis.repository': process.env.TRAVIS_REPO_SLUG
};
sonarqubeScanner(
{
serverUrl: process.env.SONAR_HOST_URL,
token: process.env.SONAR_TOKEN,
options: {
...commonOptions,
...options
}
},
callback
);
}
function buildInfo(name, version, buildNumber, hashes) {
return {
version: '1.0.1',
name,
number: buildNumber,
started: dateformat(new Date(), "yyyy-mm-dd'T'HH:MM:ss.lo"),
url: process.env.CI_BUILD_URL,
vcsRevision: process.env.TRAVIS_COMMIT,
vcsUrl: `https://github.com/${process.env.TRAVIS_REPO_SLUG}.git`,
modules: [
{
id: `org.sonarsource.sonarlint.vscode:${name}:${version}`,
properties: {
artifactsToDownload: `org.sonarsource.sonarlint.vscode:${name}:vsix`
},
artifacts: [
{
type: 'vsix',
sha1: hashes.sha1,
md5: hashes.md5,
name: `${name}-${version}.vsix`
}
]
}
],
properties: {
'java.specification.version': '1.8', // Workaround for https://jira.sonarsource.com/browse/RA-115
'buildInfo.env.PROJECT_VERSION': version,
'buildInfo.env.ARTIFACTORY_DEPLOY_REPO': 'sonarsource-public-qa',
'buildInfo.env.TRAVIS_COMMIT': process.env.TRAVIS_COMMIT
}
};
}
function hashsum() {
function processFile(file, encoding, callback) {
if (file.isNull()) {
return;
}
if (file.isStream()) {
gutil.log('Streams not supported');
return;
}
for (let algo in hashes) {
if (hashes.hasOwnProperty(algo)) {
hashes[algo] = crypto
.createHash(algo)
.update(file.contents, 'binary')
.digest('hex');
gutil.log(`Computed ${algo}: ${hashes[algo]}`);
}
}
this.push(file);
callback();
}
return through.obj(processFile);
}