Skip to content

Instantly share code, notes, and snippets.

@flaksp
Last active September 6, 2024 12:46
Show Gist options
  • Save flaksp/1fa4ff0c1e664cf3c6949a42b1fa6811 to your computer and use it in GitHub Desktop.
Save flaksp/1fa4ff0c1e664cf3c6949a42b1fa6811 to your computer and use it in GitHub Desktop.
This script converts CODEOWNERS file to IDEA Scopes.

CODEOWNERS to IDEA Scope converter

This script converts CODEOWNERS file to IDEA Scopes.

How to use:

  1. Download the script

  2. Place it in the same directory as CODEOWNERS file (or edit the CODEOWNERS_FILE_PATH variable in the script)

  3. Specify your teams or users in the OWNERS_TO_SEARCH_FOR variable (without @)

  4. (Optionally) Edit the PROJECT_NAME variable to specify project name

  5. Run the script:

    python3 convert.py
  6. Create a scope in your IDE and paste an output from the previous step to the "Pattern" field. See "Scopes" for details.

import re
CODEOWNERS_FILE_PATH = "CODEOWNERS"
PROJECT_NAME = "my-project-name"
OWNERS_TO_SEARCH_FOR = {
"my-codeowners-team-name": True,
"my-another-codeowners-team-name": True,
}
scope_lines = []
def codeowners_path_to_scope_line(file_path):
# Remove leading /
file_path = re.sub(r'^/', '', file_path)
if file_path.endswith('/'):
file_path += '/**'
return 'file[' + PROJECT_NAME + ']:' + file_path
with open("CODEOWNERS") as file:
for line in file:
if not line.startswith('/'):
continue
print(line)
path_and_team = re.split(r'[ \t]+@', line, 2)
path = path_and_team[0].strip()
owners = re.split(r'[ \t]+@', path_and_team[1].strip())
for owner in owners:
if owner in OWNERS_TO_SEARCH_FOR:
scope_lines.append(codeowners_path_to_scope_line(path))
break
else:
continue
print("\n".join(scope_lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment