Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / build-scripts / gulpfile-linter.js
blob5c16fdfc84619a4e5459184407d487d903dac832
1 'use strict';
3 var gulp = require('gulp');
4 var argv = require('yargs').argv;
5 var gutil = require('gulp-util');
6 var eslint = require('gulp-eslint');
7 var jsonlint = require('gulp-jsonlint');
8 var gulpIf = require('gulp-if');
9 var fs = require('fs-extra');
10 var mkdirp = require('mkdirp');
11 var github = require('gulp-github');
12 var eslintFilter = require('./eslint-filter');
13 var del = require('del');
15 const JS_GLOBS = [
16 '**/*.js',
17 '**/*.vue',
18 '!node_modules/**',
19 '!node_modules_linux/**',
20 '!public/repo/**'
23 function guessBaseBranch() {
24 var branch = process.env.CI_COMMIT_REF_NAME || process.env.GIT_BRANCH;
25 if (!branch) return 'develop';
27 if (branch.match(/\bfeature\//)) return 'origin/develop';
29 return 'origin/master';
32 gulp.task('linter:validate:config', function() {
33 return gulp
34 .src(['config/*.json'])
35 .pipe(jsonlint())
36 .pipe(jsonlint.reporter())
37 .pipe(jsonlint.failOnError());
38 });
40 // Full eslint
41 gulp.task('linter:validate:eslint', function() {
42 mkdirp.sync('output/eslint/');
43 return gulp
44 .src(JS_GLOBS)
45 .pipe(
46 eslint({
47 quiet: argv.quiet,
48 fix: argv.fix,
49 extensions: ['.js', '.vue']
52 .pipe(eslint.format('unix'))
53 .pipe(
54 eslint.format('checkstyle', function(checkstyleData) {
55 fs.writeFileSync('output/eslint/checkstyle.xml', checkstyleData);
58 .pipe(gulpIf(file => file.eslint != null && file.eslint.fixed, gulp.dest('./')))
59 .pipe(eslint.failAfterError());
60 });
62 // eslint of the diff
63 gulp.task('linter:validate:eslint-diff', function() {
64 var baseBranch = process.env.BASE_BRANCH || guessBaseBranch();
65 gutil.log('Performing eslint comparison to', baseBranch);
67 var eslintPipe = gulp
68 .src(JS_GLOBS, { read: false })
69 .pipe(eslintFilter.filterFiles(baseBranch))
70 .pipe(
71 eslint({
72 quiet: argv.quiet
75 .pipe(eslintFilter.filterMessages())
76 .pipe(eslint.format('unix'));
78 if (
79 process.env.SONARQUBE_GITHUB_ACCESS_TOKEN &&
80 process.env.ghprbPullId &&
81 process.env.GIT_COMMIT
82 ) {
83 eslintPipe = eslintPipe.pipe(
84 github({
85 git_token: process.env.SONARQUBE_GITHUB_ACCESS_TOKEN,
86 git_repo: 'troupe/gitter-webapp',
87 git_prid: process.env.ghprbPullId,
88 git_sha: process.env.GIT_COMMIT
93 return eslintPipe;
94 });
96 /**
97 * Hook into the validate phase
99 gulp.task('linter:validate', [
100 'linter:validate:config',
101 'linter:validate:eslint'
102 // 'linter:validate:eslint-diff',
105 gulp.task('linter:clean', function(cb) {
106 del(['output/'], cb);