diff options
| author | Micaël Bergeron <mbergeron@gitlab.com> | 2018-03-22 09:06:10 -0400 |
|---|---|---|
| committer | Micaël Bergeron <mbergeron@gitlab.com> | 2018-03-22 09:06:10 -0400 |
| commit | 9c6663ea079128bb730ec2a168b43961cd9462ec (patch) | |
| tree | 4f62c2e745c7f3e8571ee8c023abcce316c68275 /scripts | |
| parent | 6801a93e5e7447199b091e44f33c96d22a1a1960 (diff) | |
| parent | c01697539c3da4e72b1812662ff35d1f709d1dcc (diff) | |
| download | gitlab-ce-9c6663ea079128bb730ec2a168b43961cd9462ec.tar.gz | |
Merge remote-tracking branch 'origin/master' into 40781-os-to-ce
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/frontend/frontend_script_utils.js | 30 | ||||
| -rw-r--r-- | scripts/frontend/prettier.js | 103 | ||||
| -rwxr-xr-x | scripts/trigger-build-docs | 19 |
3 files changed, 147 insertions, 5 deletions
diff --git a/scripts/frontend/frontend_script_utils.js b/scripts/frontend/frontend_script_utils.js new file mode 100644 index 00000000000..2c06747255c --- /dev/null +++ b/scripts/frontend/frontend_script_utils.js @@ -0,0 +1,30 @@ +/* eslint import/no-commonjs: "off" */ +const execFileSync = require('child_process').execFileSync; + +const exec = (command, args) => { + const options = { + cwd: process.cwd(), + env: process.env, + encoding: 'utf-8', + }; + return execFileSync(command, args, options); +}; + +const execGitCmd = args => + exec('git', args) + .trim() + .toString() + .split('\n'); + +module.exports = { + getStagedFiles: fileExtensionFilter => { + const gitOptions = [ + 'diff', + '--name-only', + '--cached', + '--diff-filter=ACMRTUB', + ]; + if (fileExtensionFilter) gitOptions.push(...fileExtensionFilter); + return execGitCmd(gitOptions); + }, +}; diff --git a/scripts/frontend/prettier.js b/scripts/frontend/prettier.js new file mode 100644 index 00000000000..863572bf64d --- /dev/null +++ b/scripts/frontend/prettier.js @@ -0,0 +1,103 @@ +/* eslint import/no-commonjs: "off", import/no-extraneous-dependencies: "off", no-console: "off" */ +const glob = require('glob'); +const prettier = require('prettier'); +const fs = require('fs'); + +const getStagedFiles = require('./frontend_script_utils').getStagedFiles; + +const mode = process.argv[2] || 'check'; +const shouldSave = mode === 'save' || mode === 'save-all'; +const allFiles = mode === 'check-all' || mode === 'save-all'; + +const config = { + patterns: ['**/*.js', '**/*.vue', '**/*.scss'], + ignore: ['**/node_modules/**', '**/vendor/**', '**/public/**'], + parsers: { + js: 'babylon', + vue: 'vue', + scss: 'css', + }, +}; +const availableExtensions = Object.keys(config.parsers); + +console.log(`Loading ${allFiles ? 'All' : 'Staged'} Files ...`); + +const stagedFiles = allFiles + ? null + : getStagedFiles(availableExtensions.map(ext => `*.${ext}`)); + +if (stagedFiles) { + if (!stagedFiles.length || (stagedFiles.length === 1 && !stagedFiles[0])) { + console.log('No matching staged files.'); + return; + } + console.log(`Matching staged Files : ${stagedFiles.length}`); +} + +let didWarn = false; +let didError = false; + +let files; +if (allFiles) { + const ignore = config.ignore; + const patterns = config.patterns; + const globPattern = + patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`; + files = glob + .sync(globPattern, { ignore }) + .filter(f => allFiles || stagedFiles.includes(f)); +} else { + files = stagedFiles.filter(f => + availableExtensions.includes(f.split('.').pop()), + ); +} + +if (!files.length) { + console.log('No Files found to process with Prettier'); + return; +} + +console.log(`${shouldSave ? 'Updating' : 'Checking'} ${files.length} file(s)`); + +prettier + .resolveConfig('.') + .then(options => { + console.log('Found options : ', options); + files.forEach(file => { + try { + const fileExtension = file.split('.').pop(); + Object.assign(options, { + parser: config.parsers[fileExtension], + }); + + const input = fs.readFileSync(file, 'utf8'); + + if (shouldSave) { + const output = prettier.format(input, options); + if (output !== input) { + fs.writeFileSync(file, output, 'utf8'); + console.log(`Prettified : ${file}`); + } + } else if (!prettier.check(input, options)) { + if (!didWarn) { + console.log( + '\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed below or run "yarn prettier-staged-save"\n===============================\n', + ); + didWarn = true; + } + console.log(`Prettify Manually : ${file}`); + } + } catch (error) { + didError = true; + console.log(`\n\nError with ${file}: ${error.message}`); + } + }); + + if (didWarn || didError) { + process.exit(1); + } + }) + .catch(e => { + console.log(`Error on loading the Config File: ${e.message}`); + process.exit(1); + }); diff --git a/scripts/trigger-build-docs b/scripts/trigger-build-docs index ae8cac0cf02..c9aaba91aa0 100755 --- a/scripts/trigger-build-docs +++ b/scripts/trigger-build-docs @@ -24,8 +24,8 @@ def docs_branch # The maximum string length a file can have on a filesystem (ext4) # is 63 characters. Let's use something smaller to be 100% sure. max = 42 - # Prefix the remote branch with 'preview-' in order to avoid - # name conflicts in the rare case the branch name already + # Prefix the remote branch with the slug of the project in order + # to avoid name conflicts in the rare case the branch name already # exists in the docs repo and truncate to max length. "#{slug}-#{ENV["CI_COMMIT_REF_SLUG"]}"[0...max] end @@ -41,12 +41,21 @@ def create_remote_branch Gitlab.create_branch(GITLAB_DOCS_REPO, docs_branch, 'master') puts "=> Remote branch '#{docs_branch}' created" - # Get the latest pipeline ID which is also the first - pipeline_id = Gitlab.pipelines(GITLAB_DOCS_REPO, { ref: docs_branch }).last.id + pipelines = nil + + # Wait until the pipeline is started + loop do + sleep 1 + puts "=> Waiting for pipeline to start..." + pipelines = Gitlab.pipelines(GITLAB_DOCS_REPO, { ref: docs_branch }) + break if pipelines.any? + end + + # Get the first pipeline ID which should be the only one for the branch + pipeline_id = pipelines.first.id # Cancel the pipeline Gitlab.cancel_pipeline(GITLAB_DOCS_REPO, pipeline_id) - puts "=> Canceled uneeded pipeline #{pipeline_id} for '#{docs_branch}'" rescue Gitlab::Error::BadRequest puts "=> Remote branch '#{docs_branch}' already exists" end |
