Skip to content

Commit

Permalink
Add support for upstream presubmit logic on npm run lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Jun 20, 2022
1 parent 7ab5ffe commit ffd10a5
Show file tree
Hide file tree
Showing 16 changed files with 389 additions and 62 deletions.
17 changes: 17 additions & 0 deletions PRESUBMIT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2022 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/. */

USE_PYTHON3 = True
PRESUBMIT_VERSION = '2.0.0'


def CheckChangeLintsClean(input_api, output_api):
return input_api.canned_checks.CheckChangeLintsClean(input_api, output_api)


def CheckPylint(input_api, output_api):
return input_api.canned_checks.RunPylint(input_api,
output_api,
version='2.7')
30 changes: 21 additions & 9 deletions build/commands/lib/pylint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const path = require('path')
const fs = require('fs')
const os = require('os')
const config = require('../lib/config')
const util = require('../lib/util')
const {EOL} = require('os');
Expand Down Expand Up @@ -65,14 +66,14 @@ const getAllFiles = (check_folders) => {
return files.split('\n')
}

const getChangedFiles = (base_branch, check_folders) => {
if (!base_branch) {
const getChangedFiles = (options, check_folders) => {
if (options.all) {
return getAllFiles(check_folders)
}

const merge_base = runGit(['merge-base', 'origin/' + base_branch, 'HEAD'])
const merge_base = runGit(['merge-base', options.base, 'HEAD'])
if (!merge_base) {
console.error('Could not determine merge-base for branch ' + base_branch)
console.error('Could not determine merge-base for branch ' + options.base)
process.exit(1)
}
const changed_files = runGit(['diff', '--name-only', '--diff-filter', 'drt',
Expand Down Expand Up @@ -114,26 +115,31 @@ const runPylintLoop = (options, args, paths, report_file) => {
result &= doPylint(loop_args)
currentLen = 0
loop_args = [...args]
}
}
loop_args.push(pyPath)
currentLen += pyPath.length
}

if (currentLen > 0) {
result &= doPylint(loop_args)
result &= doPylint(loop_args)
}

return result
}

const pylint = (options = {}) => {
if (!options.base) {
options.base = 'origin/master'
} else if (!options.base.startsWith('origin/')) {
options.base = 'origin/' + options.base
}
const check_folders = ['build', 'components', 'installer', 'script', 'tools']
const report_file = 'pylint-report.txt'

const description = getDescription(options.base, check_folders)

// Get changed or all python files
const paths = getChangedFiles(options.base, check_folders)
const paths = getChangedFiles(options, check_folders)
if (!paths.length) {
console.log('No ' + description)
if (options.report) {
Expand All @@ -142,8 +148,14 @@ const pylint = (options = {}) => {
return
}

// Prepare pylint args
let args = ['-j0', '-rn']
var j_value = 0
if (process.platform === 'win32') {
// Windows can't handle unrestricted -j0 value, limit it to sane value.
j_value = Math.min(32, os.cpus().length)
}

// Prepare pylint args.
let args = [ '-j' + j_value.toString(), '-rn']
if (options.report) {
args.push('-fparseable')
// Clean previous report as we will be appending to it
Expand Down
35 changes: 33 additions & 2 deletions build/commands/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ const util = {
util.run('goma_ctl', ['showflags'], options)
util.run('goma_ctl', ['stat'], options)
}

// Setting `AUTONINJA_BUILD_ID` allows tracing Goma remote execution which helps with
// debugging issues (e.g., slowness or remote-failures).
options.env.AUTONINJA_BUILD_ID = buildId
Expand Down Expand Up @@ -627,11 +627,42 @@ const util = {
'--base_branch=' + options.base], cmd_options)
},

presubmit: (options = {}) => {
if (!options.base) {
options.base = 'origin/master'
}
// Temporary cleanup call, should be removed when everyone will remove
// 'gerrit.host' from their brave checkout.
util.runGit(
config.braveCoreDir, ['config', '--unset-all', 'gerrit.host'], true)
let cmd_options = config.defaultOptions
cmd_options.cwd = config.braveCoreDir
cmd_options = mergeWithDefault(cmd_options)
cmd = 'git'
args = ['cl', 'presubmit', options.base, '--force']
if (options.all)
args.append('--all')
util.run(cmd, args, cmd_options)
},

format: (options = {}) => {
if (!options.base) {
options.base = 'origin/master'
}
let cmd_options = config.defaultOptions
cmd_options.cwd = config.braveCoreDir
cmd_options = mergeWithDefault(cmd_options)
util.run('vpython', [path.join(config.braveCoreDir, 'build', 'commands', 'scripts', 'format.py'), options.full ? '--full' : ''], cmd_options)
cmd = 'git'
args = ['cl', 'format', '--upstream=' + options.base]
if (options.full)
args.append('--full')
if (options.js)
args.append('--js')
if (options.rust)
args.append('--rust-fmt')
if (options.swift)
args.append('--swift-format')
util.run(cmd, args, cmd_options)
},


Expand Down
12 changes: 12 additions & 0 deletions build/commands/scripts/commands.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,27 @@ program
.option('--base <base branch>', 'set the destination branch for the PR')
.action(util.lint)

program
.command('presubmit')
.option('--base <base branch>', 'set the destination branch for the PR')
.option('--all', 'run presubmit on all files')
.action(util.presubmit)

program
.command('pylint')
.option('--base <base_branch>', 'only analyse files changed relative to base_branch')
.option('--all', 'run pylint on all files')
.option('--report', 'produce a parseable report file')
.action(pylint)

program
.command('format')
.option('--base <base branch>', 'set the destination branch for the PR')
.option('--full', 'format all lines in changed files instead of only the changed lines')
.option('--js', 'format javascript code with clang-format')
.option('--rust', 'enables formatting of Rust file types using rustfmt')
.option('--swift',
'enables formatting of Swift file types using swift-format')
.action(util.format)

program
Expand Down
45 changes: 0 additions & 45 deletions build/commands/scripts/format.py

This file was deleted.

5 changes: 5 additions & 0 deletions chromium_src/chrome/browser/prefs/browser_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

// This method should be periodically pruned of year+ old migrations.
void MigrateObsoleteProfilePrefs(Profile* profile) {
// BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS
#if !BUILDFLAG(USE_GCM_FROM_PLATFORM)
// Added 02/2020.
// Must be called before ChromiumImpl because it's migrating a Chromium pref
Expand Down Expand Up @@ -98,10 +99,12 @@ void MigrateObsoleteProfilePrefs(Profile* profile) {
#if BUILDFLAG(ENABLE_BRAVE_TRANSLATE_GO)
translate::MigrateBraveProfilePrefs(profile->GetPrefs());
#endif
// END_MIGRATE_OBSOLETE_PROFILE_PREFS
}

// This method should be periodically pruned of year+ old migrations.
void MigrateObsoleteLocalStatePrefs(PrefService* local_state) {
// BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS
MigrateObsoleteLocalStatePrefs_ChromiumImpl(local_state);

#if BUILDFLAG(ENABLE_WIDEVINE)
Expand All @@ -115,4 +118,6 @@ void MigrateObsoleteLocalStatePrefs(PrefService* local_state) {
#endif

decentralized_dns::MigrateObsoleteLocalStatePrefs(local_state);

// END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS
}
6 changes: 5 additions & 1 deletion codereview.settings
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This file is used by git-cl to get repository specific information.
GERRIT_HOST: True

# Don't add GERRIT_HOST, it will make PRESUBMIT checks do unnecessary stuff.
# CODE_REVIEW_SERVER is a mock value to not generate errors during PRESUBMIT.
CODE_REVIEW_SERVER: github.com

LINT_IGNORE_REGEX: win_build_output/midl/google_update/.*
5 changes: 5 additions & 0 deletions components/brave_private_cdn/private_cdn_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

namespace brave {

// static
PrivateCdnHelper* PrivateCdnHelper::GetInstance() {
return base::Singleton<PrivateCdnHelper>::get();
}

bool PrivateCdnHelper::RemovePadding(base::StringPiece* padded_string) const {
if (!padded_string) {
return false;
Expand Down
6 changes: 1 addition & 5 deletions components/brave_private_cdn/private_cdn_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#ifndef BRAVE_COMPONENTS_BRAVE_PRIVATE_CDN_PRIVATE_CDN_HELPER_H_
#define BRAVE_COMPONENTS_BRAVE_PRIVATE_CDN_PRIVATE_CDN_HELPER_H_

#include <string>

#include "base/memory/singleton.h"
#include "base/strings/string_piece.h"

Expand All @@ -18,9 +16,7 @@ class PrivateCdnHelper final {
PrivateCdnHelper(const PrivateCdnHelper&) = delete;
PrivateCdnHelper& operator=(const PrivateCdnHelper&) = delete;

static PrivateCdnHelper* GetInstance() {
return base::Singleton<PrivateCdnHelper>::get();
}
static PrivateCdnHelper* GetInstance();

bool RemovePadding(base::StringPiece* padded_string) const;

Expand Down
2 changes: 2 additions & 0 deletions inherit-review-settings-ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This file allows src/PRESUBMIT.py to be executed on brave/* files.
See presubmit_support.py for details.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"pull_l10n": "node ./build/commands/scripts/commands.js pull_l10n",
"chromium_rebase_l10n": "node ./build/commands/scripts/commands.js chromium_rebase_l10n",
"lint": "node ./build/commands/scripts/commands.js lint",
"presubmit": "node ./build/commands/scripts/commands.js presubmit",
"format": "node ./build/commands/scripts/commands.js format",
"test": "node ./build/commands/scripts/commands.js test",
"test:scripts": "jest build/commands/lib build/commands/scripts",
Expand Down
9 changes: 9 additions & 0 deletions patches/PRESUBMIT.py.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 81b1b62c3ef84dedaba01ef067bafc66d9ebfbb4..7e4fb471f68b60b23b03603902f33fb0e90ca204 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -5845,3 +5845,4 @@ def CheckPythonShebang(input_api, output_api):
"Please use '#!/usr/bin/env python/2/3' as the shebang of %s" %
file))
return result
+if not globals().get('__name__'): from lib.import_inline import inline_module; inline_module('chromium_presubmit_overrides', globals(), locals())
Loading

0 comments on commit ffd10a5

Please sign in to comment.