Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/JanssenProject/jans into ja…
Browse files Browse the repository at this point in the history
…ns-config-api-418
  • Loading branch information
pujavs committed Apr 8, 2022
2 parents cb7d36c + dfa19f0 commit 84eadf6
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/label_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
name: Label PR and Schema
on:
pull_request:
types:
- opened
- edited
- synchronize
issues:
types:
- opened
Expand Down
125 changes: 86 additions & 39 deletions automation/github-labels/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ def exec_cmd(cmd, output_file=None, silent=False):


def load_labels_schema():
"""Loads the label json schema file
:return: json schema
"""
json_schema = {}
with open(label_schema_json, "r") as schema_file:
json_schema = json.load(schema_file)
return json_schema


def create_labels():
"""Creates missing labels from json schema
"""
json_schema = load_labels_schema()
stdout, stderr, retcode = exec_cmd("gh label list", silent=True)
existing_labels = str(stdout, "utf-8")
Expand All @@ -56,50 +61,24 @@ def create_labels():
print(f"Couldn't create label {k} because {e}")


def auto_label(operation="pr", issue_or_pr_number=None, paths=None, branch=None, title=None):
def auto_label(operation="pr", issue_or_pr_number=None, pr_modified_file_paths=None, pr_branch=None, title=None):
"""Label issues and PRs based on the schema provided from title prefixes, paths and branches
"""
labels = []
json_schema = load_labels_schema()
# Loop over all labels
for k, v in json_schema.items():
# Check title label. Label based on conventional commit title
if title:
for title_prefix in v["auto-label"]["title-prefixes"]:
try:
if title_prefix in title.split(':')[0]:
print(f"Detected label from title. Adding label {k}")
labels.append(k)
except Exception as e:
print(e)
labels = labels + check_title(title, k, v["auto-label"]["title-prefixes"], v["auto-label"]["paths"])

# Check branch label
if branch:
if branch == v["auto-label"]["branch"]:
print(f"Detected label from branch. Adding label {k}")
labels.append(k)

if paths:
# Check if comp-*, area-* labels need to be added
allpaths = paths.split(",")
for path in allpaths:
# Check file path for direct hit
if path in v["auto-label"]["paths"]:
print(f"Detected label from exact path. Adding label {k}")
labels.append(k)
else:
# Label all .md with area-documentation
if k == "area-documentation" and ".md" in path:
print(f"Detected a markdown file in the path. Adding label {k}")
labels.append(k)
else:
# Check if main directories need exist in paths
try:
for i in range(len(path.split("/"))):
# Check main directories i.e docs .github .github/workflows
if path.split("/")[i] in v["auto-label"]["paths"]:
print(f"Detected label from folder. Adding label {k}")
labels.append(k)
except IndexError:
print("Got an index issue!")
if pr_branch:
labels = labels + check_branch(pr_branch, k, v["auto-label"]["branch"])

# Check modified file paths
if pr_modified_file_paths:
labels = labels + check_paths_in_pr(pr_modified_file_paths, k, v["auto-label"]["paths"])

# removes duplicate labels and joins them by comma
string_of_labels = ",".join(list(dict.fromkeys(labels)))
Expand All @@ -110,19 +89,87 @@ def auto_label(operation="pr", issue_or_pr_number=None, paths=None, branch=None,
print(f"Couldn't add the label to the PR {issue_or_pr_number} because {e}")


def check_title(title, label, title_prefixes, label_paths):
"""Detects necessary labels based on the title structure
:return: list of labels
"""
labels = []
for title_prefix in title_prefixes:
try:
if title_prefix == "Snyk" and title_prefix in title:
labels.append(label)
# Detect title prefix i.e feat, fix, refactor..etc
if title_prefix in title.split(':')[0]:
print(f"Detected label from title prefix. Adding label {label}")
labels.append(label)
except Exception as e:
print(e)
# Detect comp-* label from component name in title
component = title[title.find("(") + 1: title.find(")")]
for path in label_paths:
try:
if component == path:
print(f"Detected label from title component {path}. Adding label {label}")
labels.append(label)
except Exception as e:
print(e)
return labels


def check_branch(pr_branch, label, label_branch):
"""Detects necessary labels based on the branch
:return: list of labels
"""
labels = []
if pr_branch == label_branch:
print(f"Detected label from branch. Adding label {label}")
labels.append(label)
return labels


def check_paths_in_pr(pr_modified_file_paths, label, label_paths):
"""Detects necessary labels based on files modified in the paths of the PR branch
:return: list of labels
"""
labels = []
# Check if comp-*, area-* labels need to be added
allpaths = pr_modified_file_paths.split(",")
for path in allpaths:
# Check file path for direct hit
if path in label_paths:
print(f"Detected label from exact path. Adding label {label}")
labels.append(label)
else:
# Label all .md with area-documentation
if label == "area-documentation" and ".md" in path:
print(f"Detected a markdown file in the path. Adding label {label}")
labels.append(label)
else:
# Check if main directories exist in paths
try:
for i in range(len(path.split("/"))):
# Check main directories i.e docs .github .github/workflows
if path.split("/")[i] in label_paths:
print(f"Detected label from folder. Adding label {label}")
labels.append(label)
except IndexError:
print("Got an index issue!")
return labels


def main():
issue_or_pr_number = sys.argv[1]
changed_files = None if sys.argv[2] == "NONE" else sys.argv[2]
branch = None if sys.argv[3] == "NONE" else sys.argv[3]
pr_branch = None if sys.argv[3] == "NONE" else sys.argv[3]
operation = sys.argv[4]
title = sys.argv[5]

print(f"Starting to add labels for {operation} {issue_or_pr_number}")
print(f"Detected the following changed files {changed_files}")
print(f"Detected the following head branch {branch}")
print(f"Detected the following head branch {pr_branch}")
print(f"Detected the following title {title}")
create_labels()
auto_label(operation, issue_or_pr_number, changed_files, branch, title)
auto_label(operation, issue_or_pr_number, changed_files, pr_branch, title)


if __name__ == "__main__":
Expand Down
9 changes: 9 additions & 0 deletions automation/github-labels/labels-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@
"title-prefixes": []
}
},
"kind-dependencies": {
"color": "800080",
"description": "This issue or pull request already exists",
"auto-label": {
"branch": "",
"paths": [],
"title-prefixes": ["build", "chore", "Snyk"]
}
},
"kind-enhancement": {
"color": "800080",
"description": "Issue or PR is an enhancement to an existing functionality",
Expand Down
5 changes: 4 additions & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ git commit -S -s -m 'message that follows conventional commit style'
```

### Branches
Branch name should have compotitlesnent name as prefix, eg `jans-core-mybranch`
Branch name should have component name as prefix, eg `jans-core-mybranch`

### PRs
- PR titles should also follow [Conventional Commits v1.0.0](https://www.conventionalcommits.org/en/v1.0.0/). This will help in keeping merge commit messages inline with commit message standards
Expand All @@ -136,6 +136,9 @@ Branch name should have compotitlesnent name as prefix, eg `jans-core-mybranch`
- PR should include relevent documentaton changes
- PR should include unit and integration tests

### Issues
- Issue titles should follow [Conventional Commits v1.0.0](https://www.conventionalcommits.org/en/v1.0.0/)

# Contribution Workflow

## Find something to work on
Expand Down
Loading

0 comments on commit 84eadf6

Please sign in to comment.