-
Notifications
You must be signed in to change notification settings - Fork 1.6k
{CI} Fix merge-base diff in linter, style, and scan jobs #9725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
naga-nandyala
wants to merge
1
commit into
Azure:main
Choose a base branch
from
naga-nandyala:naga/fix-ci-diff-merge-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| """ | ||
| This script is used to run azdev scan on modified extensions in PR pipelines. | ||
|
|
||
| It reuses find_modified_files_against_master_branch() from util.py to get an | ||
| accurate list of files changed in the PR (via merge-base), then runs | ||
| azdev scan on each file. | ||
| """ | ||
| import json | ||
| import logging | ||
| import sys | ||
| from subprocess import CalledProcessError, check_output | ||
|
|
||
| from util import find_modified_files_against_master_branch | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| logger.setLevel(logging.DEBUG) | ||
| ch = logging.StreamHandler() | ||
| ch.setLevel(logging.DEBUG) | ||
| logger.addHandler(ch) | ||
|
|
||
|
|
||
| def run_scan(modified_files, confidence_level=None): | ||
| """Run azdev scan on each modified file and report secrets.""" | ||
| confidence_flag = [] | ||
| confidence_msg = '' | ||
| if confidence_level: | ||
| confidence_flag = ['--confidence-level', confidence_level] | ||
| confidence_msg = ' --confidence-level {}'.format(confidence_level) | ||
|
|
||
| secret_files = [] | ||
| failed_files = [] | ||
| for f in modified_files: | ||
| cmd = ['azdev', 'scan', '-f', f, '--continue-on-failure'] + confidence_flag | ||
| logger.info('Scanning: %s', f) | ||
| try: | ||
| output = check_output(cmd).decode('utf-8', errors='replace') | ||
| result = json.loads(output) | ||
| if result.get('secrets_detected') is True: | ||
| logger.error( | ||
| '\033[0;31mSecrets detected from %s, Please remove or replace it. ' | ||
| 'You can run \'azdev scan%s\'/\'azdev mask%s\' locally to fix.\033[0m', | ||
| f, confidence_msg, confidence_msg | ||
| ) | ||
| secret_files.append(f) | ||
| except CalledProcessError as e: | ||
| logger.error('azdev scan failed for %s: %s', f, e) | ||
| failed_files.append(f) | ||
| except (json.JSONDecodeError, KeyError) as e: | ||
| logger.error('Failed to parse azdev scan output for %s: %s', f, e) | ||
| failed_files.append(f) | ||
|
|
||
| has_errors = False | ||
| if secret_files: | ||
| logger.error('Secrets detected in %d file(s): %s', len(secret_files), secret_files) | ||
| has_errors = True | ||
| if failed_files: | ||
| logger.error('Scan failed for %d file(s): %s', len(failed_files), failed_files) | ||
| has_errors = True | ||
| if has_errors: | ||
| sys.exit(1) | ||
naga-nandyala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| logger.info('-' * 100) | ||
| logger.info('No secrets detected in any modified files.') | ||
| logger.info('-' * 100) | ||
|
|
||
|
|
||
| def main(): | ||
| import argparse | ||
| parser = argparse.ArgumentParser(description='azdev scan on modified extensions') | ||
| parser.add_argument('--confidence-level', | ||
| type=str, | ||
| default=None, | ||
| help='Confidence level for azdev scan (e.g., MEDIUM). ' | ||
| 'Default: HIGH (azdev scan default).') | ||
| args = parser.parse_args() | ||
|
|
||
| modified_files = find_modified_files_against_master_branch() | ||
| if not modified_files: | ||
| logger.info('No modified files found, skipping scan.') | ||
| return | ||
|
|
||
| run_scan(modified_files, confidence_level=args.confidence_level) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.