Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…awa-admin into action-items
  • Loading branch information
meetulr committed Feb 18, 2024
2 parents 31a299d + 25d3fe1 commit e402627
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 182 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/authorized-changes-detection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
##############################################################################
##############################################################################
#
# NOTE!
#
# Please read the README.md file in this directory that defines what should
# be placed in this file
#
##############################################################################
##############################################################################

name: Checking workflow files
on:
pull_request:
paths:
- '.github/**'
- 'env.example'
- '.node-version'
- '.husky/**'
- 'scripts/**'
- 'package.json'
- 'tsconfig.json'
- '.gitignore'
- '.eslintrc.json'
- '.eslintignore '
- 'vite.config.ts'
- 'docker-compose.yaml'
- 'Dockerfile'
- 'CODEOWNERS'
- 'LICENSE'
- 'setup.ts'

jobs:
Checking-for-unauthorized-file-changes:
name: Checking for unauthorized file changes
runs-on: ubuntu-latest
steps:
- name: Unauthorized file modification in PR
run: exit 1
162 changes: 0 additions & 162 deletions .github/workflows/check_files.py

This file was deleted.

132 changes: 132 additions & 0 deletions .github/workflows/count_changed_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""Script to limit number of file changes in single PR.
Methodology:
Analyses the Pull request to find if the count of file changed in a pr
exceeds a pre-defined nummber 20
This scripts encourages contributors to align with project practices,
reducing the likelihood of unintentional merges into incorrect branches.
NOTE:
This script complies with our python3 coding and documentation standards.
It complies with:
1) Pylint
2) Pydocstyle
3) Pycodestyle
4) Flake8
"""

import sys
import argparse
import subprocess


def _count_changed_files(base_branch, pr_branch):
"""
Count the number of changed files between two branches.
Args:
base_branch (str): The base branch.
pr_branch (str): The PR branch.
Returns:
int: The number of changed files.
Raises:
SystemExit: If an error occurs during execution.
"""
base_branch = f"origin/{base_branch}"
pr_branch = f"origin/{pr_branch}"

command = f"git diff --name-only {base_branch}...{pr_branch} | wc -l"

try:
# Run git command to get the list of changed files
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
output, error = process.communicate()
except Exception as e:
print(f"Error: {e}")
sys.exit(1)

file_count = int(output.strip())
return file_count

def _arg_parser_resolver():
"""Resolve the CLI arguments provided by the user.
Args:
None
Returns:
result: Parsed argument object
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--base_branch",
type=str,
required=True,
help="Base branch where pull request should be made."
),
parser.add_argument(
"--pr_branch",
type=str,
required=True,
help="PR branch from where the pull request is made.",
),
parser.add_argument(
"--file_count",
type=int,
default=20,
help="Number of files changes allowed in a single commit")
return parser.parse_args()


def main():
"""
Execute the script's main functionality.
This function serves as the entry point for the script. It performs
the following tasks:
1. Validates and retrieves the base branch and PR commit from
command line arguments.
2. Counts the number of changed files between the specified branches.
3. Checks if the count of changed files exceeds the acceptable
limit (20).
4. Provides informative messages based on the analysis.
Raises:
SystemExit: If an error occurs during execution.
"""

args = _arg_parser_resolver()

base_branch = args.base_branch
pr_branch = args.pr_branch

print(f"You are trying to merge on branch: {base_branch}")
print(f"You are making commit from your branch: {pr_branch}")

# Count changed files
file_count = _count_changed_files(base_branch, pr_branch)
print(f"Number of changed files: {file_count}")

# Check if the count exceeds 20
if file_count > args.file_count:
print("Error: Too many files (greater than 20) changed in the pull request.")
print("Possible issues:")
print("- Contributor may be merging into an incorrect branch.")
print("- Source branch may be incorrect please use develop as source branch.")
sys.exit(1)


if __name__ == "__main__":
main()
8 changes: 4 additions & 4 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ jobs:

- name: Run linting check
if: steps.changed-files.outputs.only_changed != 'true'

run: npm run lint:check

- name: Check for localStorage Usage
Expand All @@ -64,8 +63,9 @@ jobs:
Check-Changed-Files:
name: File count, sensitive files and branch check
runs-on: ubuntu-latest
needs: Code-Quality-Checks
# needs: Code-Quality-Checks
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -76,8 +76,8 @@ jobs:
python-version: 3.9

- name: Run Python script
run:
python .github/workflows/check_files.py --base_branch "${{ github.base_ref }}" --pr_branch "${{ github.head_ref }}" --sensitive_files .github/workflows/sensitive_files.txt
run: |
python .github/workflows/count_changed_files.py --base_branch "${{ github.base_ref }}" --pr_branch "${{ github.head_ref }}"
Test-Application:
name: Test Application
Expand Down
16 changes: 0 additions & 16 deletions .github/workflows/sensitive_files.txt

This file was deleted.

0 comments on commit e402627

Please sign in to comment.