Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
refactor: move more logic to the python wrapper
Browse files Browse the repository at this point in the history
This commit will:
- use commisery as subprocess call
- handle annotations (incl. message format) from python
  • Loading branch information
KevinDeJong-TomTom committed Sep 23, 2020
1 parent 3fa405d commit a1192b2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
111 changes: 75 additions & 36 deletions commisery_action.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,75 @@
#!/usr/bin/env python3

# Copyright (C) 2020-2020, TomTom (http://tomtom.com).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click

from github import Github
from commisery import checking


@click.command()
@click.option('-t', '--token', required=True, help='GitHub Token')
@click.option('-r', '--repository', required=True, help='GitHub repository')
@click.option('-p', '--pull-request-id', required=True, help='Pull Request identifier')
def main(token: str, repository: str, pull_request_id: int) -> int:
repo = Github(token).get_repo(repository)
commits = repo.get_pull(int(pull_request_id)).get_commits()

for commit in commits:
checking.main(argv=[1, commit.sha])


if __name__ == '__main__':
main()
#!/usr/bin/env python3

# Copyright (C) 2020-2020, TomTom (http://tomtom.com).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click
import re
import subprocess

from github import Github


def convert_to_multiline(text: str) -> str:
return text.replace('\n', '%0A')


def strip_ansicolors(text: str) -> str:
return re.sub('\x1b\\[(K|.*?m)', '', text)


def error_message(message: str, options: dict = {}):
error = '::error '

for key, value in options.items():
error += f'{key}={value},'

error = error[:-1]
message = strip_ansicolors(convert_to_multiline(message))
error += f'::{message}'

print(error)


@click.command()
@click.option('-t', '--token',
required=True, help='GitHub Token')
@click.option('-r', '--repository',
required=True, help='GitHub repository')
@click.option('-p', '--pull-request-id',
required=True, help='Pull Request identifier')
def main(token: str, repository: str, pull_request_id: int) -> int:
errors = 0

repo = Github(token).get_repo(repository)
pr = repo.get_pull(int(pull_request_id))
commits = pr.get_commits()

for commit in commits:
proc = subprocess.Popen(
["commisery-verify-msg", commit.sha],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = proc.communicate()

if proc.returncode > 0:
error_message(stderr.decode("utf-8"))
errors += 1

exit(1 if errors else 0)


if __name__ == '__main__':
main()
13 changes: 2 additions & 11 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,5 @@

echo "Running Commisery for ${GITHUB_REPOSITORY}, Pull Request #${INPUT_PULL_REQUEST}"

OUTPUT="$(python3 /commisery_action.py --token=${INPUT_TOKEN} --repository=${GITHUB_REPOSITORY} --pull-request-id=${INPUT_PULL_REQUEST} 2>&1)"
echo "$OUTPUT"

if [ -n "$OUTPUT" ]
then
# 1. Remove ANSI Color codes
# 2. Replace \n with %0A to allow new lines in annotations
ANNOTATION=`echo "$OUTPUT" | sed 's/\x1b\[[0-9;]*m//g' | sed ':a;N;$!ba;s/\n/%0A/g'`
echo "::error ::$ANNOTATION"
exit 1
fi
python /commisery_action.py --token=${INPUT_TOKEN} --repository=${GITHUB_REPOSITORY} --pull-request-id=${INPUT_PULL_REQUEST}
exit $?

0 comments on commit a1192b2

Please sign in to comment.