Skip to content

Commit

Permalink
Added check for project in commit validation
Browse files Browse the repository at this point in the history
fixes #6525
  • Loading branch information
David Davis authored and daviddavis committed Apr 24, 2020
1 parent 24dc0c7 commit 2c43cc8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -168,6 +168,10 @@ The following settings are stored in `template_config.yml`.
stable_branch A string that points to the latest stable branch (e.g. "3.0"). This is used
for features like the cherry pick automation.

redmine_project A string that corresponds to the redmine identifier for the repo's project.
This is used during commit validation to make sure the commit is attached to
an issue in the correct project.
```
# Bootstrap a new Pulp plugin
Expand Down
1 change: 1 addition & 0 deletions plugin-template
Expand Up @@ -44,6 +44,7 @@ DEFAULT_SETTINGS = {
'pulpcore_branch': 'master',
'pulpcore_pip_version_specifier': None,
'pypi_username': None,
'redmine_project': None,
'stable_branch': None,
'travis_addtl_services': [],
'travis_notifications': None,
Expand Down
30 changes: 19 additions & 11 deletions templates/travis/.travis/validate_commit_message.py.j2
Expand Up @@ -9,13 +9,15 @@ import sys
KEYWORDS = ["fixes", "closes", "re", "ref"]
NO_ISSUE = "[noissue]"
STATUSES = ["NEW", "ASSIGNED", "POST", "MODIFIED"]
REDMINE_URL = "https://pulp.plan.io"

sha = sys.argv[1]
project = "{{ redmine_project if redmine_project }}"
message = subprocess.check_output(["git", "log", "--format=%B", "-n 1", sha]).decode("utf-8")


def __check_status(issue):
response = requests.get("https://pulp.plan.io/issues/{}.json".format(issue))
response = requests.get(f"{REDMINE_URL}/issues/{issue}.json")
response.raise_for_status()
bug_json = response.json()
status = bug_json["issue"]["status"]["name"]
Expand All @@ -25,6 +27,12 @@ def __check_status(issue):
"{statuses}.".format(issue=issue, status=status, statuses=", ".join(STATUSES))
)

if project:
project_id = bug_json["issue"]["project"]["id"]
project_json = requests.get(f"{REDMINE_URL}/projects/{project_id}.json").json()
if project_json["project"]["identifier"] != project:
sys.exit(f"Error: issue {issue} is not in the {project} project.")


def __check_changelog(issue):
if len(glob.glob(f"CHANGES/**/{issue}.*", recursive=True)) < 1:
Expand All @@ -34,18 +42,18 @@ def __check_changelog(issue):
print("Checking commit message for {sha}.".format(sha=sha[0:7]))

# validate the issue attached to the commit
if NO_ISSUE in message:
print("Commit {sha} has no issue attached. Skipping issue check".format(sha=sha[0:7]))
else:
regex = r"(?:{keywords})[\s:]+#(\d+)".format(keywords=("|").join(KEYWORDS))
pattern = re.compile(regex)
regex = r"(?:{keywords})[\s:]+#(\d+)".format(keywords=("|").join(KEYWORDS))
pattern = re.compile(regex)

issues = pattern.findall(message)
issues = pattern.findall(message)

if issues:
for issue in pattern.findall(message):
__check_status(issue)
__check_changelog(issue)
if issues:
for issue in pattern.findall(message):
__check_status(issue)
__check_changelog(issue)
else:
if NO_ISSUE in message:
print("Commit {sha} has no issues but is tagged {tag}.".format(sha=sha[0:7], tag=NO_ISSUE))
else:
sys.exit(
"Error: no attached issues found for {sha}. If this was intentional, add "
Expand Down

0 comments on commit 2c43cc8

Please sign in to comment.