Skip to content

Commit

Permalink
Validating commits
Browse files Browse the repository at this point in the history
  • Loading branch information
fao89 committed Dec 20, 2019
1 parent 454b03f commit a994c85
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,10 @@ language: python
services:
- docker
before_install:
- git clone https://github.com/pulp/pulp_file.git
.travis/before_install.sh
# Created template_config.yml with plugin_template,
# and after that I removed manually travis files not related to validating commit
# also removed some python related stuff like flake8, black and pip installing doc/test requirements
install:
- pip install jmespath
# Need /usr/bin/http script, so use sudo
Expand Down
76 changes: 76 additions & 0 deletions .travis/before_install.sh
@@ -0,0 +1,76 @@
#!/usr/bin/env bash

# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by bootstrap.py. Please use
# bootstrap.py to update this file.
#
# For more info visit https://github.com/pulp/plugin_template

set -mveuo pipefail

export PRE_BEFORE_INSTALL=$TRAVIS_BUILD_DIR/.travis/pre_before_install.sh
export POST_BEFORE_INSTALL=$TRAVIS_BUILD_DIR/.travis/post_before_install.sh

COMMIT_MSG=$(git log --format=%B --no-merges -1)
export COMMIT_MSG

if [ -f $PRE_BEFORE_INSTALL ]; then
$PRE_BEFORE_INSTALL
fi

if [[ -n $(echo -e $COMMIT_MSG | grep -P "Required PR:.*" | grep -v "https") ]]; then
echo "Invalid Required PR link detected in commit message. Please use the full https url."
exit 1
fi

if [ "$TRAVIS_PULL_REQUEST" != "false" ] || [ -z "$TRAVIS_TAG" -a "$TRAVIS_BRANCH" != "master"]
then
export PULP_PR_NUMBER=$(echo $COMMIT_MSG | grep -oP 'Required\ PR:\ https\:\/\/github\.com\/pulp\/pulpcore\/pull\/(\d+)' | awk -F'/' '{print $7}')
export PULP_ROLES_PR_NUMBER=$(echo $COMMIT_MSG | grep -oP 'Required\ PR:\ https\:\/\/github\.com\/pulp\/ansible-pulp\/pull\/(\d+)' | awk -F'/' '{print $7}')
export PULP_FILE_PR_NUMBER=$(echo $COMMIT_MSG | grep -oP 'Required\ PR:\ https\:\/\/github\.com\/pulp\/pulp_file\/pull\/(\d+)' | awk -F'/' '{print $7}')
else
export PULP_PR_NUMBER=
export PULP_ROLES_PR_NUMBER=
export PULP_FILE_PR_NUMBER=
fi


# check the commit message
./.travis/check_commit.sh


git clone --depth=1 https://github.com/pulp/pulp_file.git
if [ -n "$PULP_FILE_PR_NUMBER" ]; then
cd pulp_file
git fetch --depth=1 origin +refs/pull/$PULP_FILE_PR_NUMBER/merge
git checkout FETCH_HEAD
cd ..
fi


cd ..
git clone --depth=1 https://github.com/pulp/ansible-pulp.git
if [ -n "$PULP_ROLES_PR_NUMBER" ]; then
cd ansible-pulp
git fetch --depth=1 origin +refs/pull/$PULP_ROLES_PR_NUMBER/merge
git checkout FETCH_HEAD
cd ..
fi


git clone --depth=1 https://github.com/pulp/pulpcore.git --branch master

if [ -n "$PULP_PR_NUMBER" ]; then
cd pulpcore
git fetch --depth=1 origin +refs/pull/$PULP_PR_NUMBER/merge
git checkout FETCH_HEAD
cd ..
fi


cd pulp-operator

if [ -f $POST_BEFORE_INSTALL ]; then
$POST_BEFORE_INSTALL
fi
39 changes: 39 additions & 0 deletions .travis/check_commit.sh
@@ -0,0 +1,39 @@
#!/bin/bash

# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by bootstrap.py. Please use
# bootstrap.py to update this file.
#
# For more info visit https://github.com/pulp/plugin_template

set -euv

# skip this check for everything but PRs
if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then
exit 0
fi

if [ "$TRAVIS_COMMIT_RANGE" != "" ]; then
RANGE=$TRAVIS_COMMIT_RANGE
elif [ "$TRAVIS_COMMIT" != "" ]; then
RANGE=$TRAVIS_COMMIT
fi

# Travis sends the ranges with 3 dots. Git only wants two.
if [[ "$RANGE" == *...* ]]; then
RANGE=`echo $TRAVIS_COMMIT_RANGE | sed 's/\.\.\./../'`
else
RANGE="$RANGE~..$RANGE"
fi

for sha in `git log --format=oneline --no-merges "$RANGE" | cut '-d ' -f1`
do
pip install requests
python .travis/validate_commit_message.py $sha
VALUE=$?

if [ "$VALUE" -gt 0 ]; then
exit $VALUE
fi
done
53 changes: 53 additions & 0 deletions .travis/validate_commit_message.py
@@ -0,0 +1,53 @@
# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by bootstrap.py. Please use
# bootstrap.py to update this file.
#
# For more info visit https://github.com/pulp/plugin_template

import re
import requests
import subprocess
import sys

KEYWORDS = ["fixes", "closes", "re", "ref"]
NO_ISSUE = "[noissue]"
STATUSES = ["NEW", "ASSIGNED", "POST", "MODIFIED"]

sha = sys.argv[1]
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.raise_for_status()
bug_json = response.json()
status = bug_json["issue"]["status"]["name"]
if status not in STATUSES:
sys.exit(
"Error: issue #{issue} has invalid status of {status}. Status must be one of "
"{statuses}.".format(issue=issue, status=status, statuses=", ".join(STATUSES))
)


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)

issues = pattern.findall(message)

if issues:
for issue in pattern.findall(message):
__check_status(issue)
else:
sys.exit(
"Error: no attached issues found for {sha}. If this was intentional, add "
" '{tag}' to the commit message.".format(sha=sha[0:7], tag=NO_ISSUE)
)

print("Commit message for {sha} passed.".format(sha=sha[0:7]))
31 changes: 31 additions & 0 deletions template_config.yml
@@ -0,0 +1,31 @@
# This config represents the latest values used when running the plugin-template. Any settings that
# were not present before running plugin-template have been added with their default values.

additional_plugins: []
black: true
check_commit_message: true
cherry_pick_automation: false
coverage: false
deploy_client_to_pypi: false
deploy_client_to_rubygems: false
deploy_daily_client_to_pypi: false
deploy_daily_client_to_rubygems: false
deploy_to_pypi: false
docs_test: false
plugin_app_label: operator
plugin_camel: Pulp-Operator
plugin_camel_short: Operator
plugin_caps: PULP-OPERATOR
plugin_caps_short: OPERATOR
plugin_dash: pulp-operator
plugin_dash_short: operator
plugin_name: pulp-operator
plugin_snake: pulp_operator
pulp_settings: null
pulpcore_branch: master
pydocstyle: false
pypi_username: null
stable_branch: null
test_bindings: false
test_performance: false
travis_notifications: null

0 comments on commit a994c85

Please sign in to comment.