From 7a6649b6610deca861dad6afd2e7a1623409de1e Mon Sep 17 00:00:00 2001 From: Adam Uhlir Date: Thu, 28 Feb 2019 15:05:20 -0800 Subject: [PATCH] Fixing listening for repo's events only for specific ref --- github_test.sh | 5 +++-- publish/http.py | 12 ++++++++++++ publish/publishing.py | 24 ++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/github_test.sh b/github_test.sh index d3b5a7a..e365fd0 100755 --- a/github_test.sh +++ b/github_test.sh @@ -2,9 +2,10 @@ repo_name=$1 secret=$2 -url=${3-http://localhost:8000} +ref=$3 +url=${4-http://localhost:8000} -data="{\"ref\": \"\"}" +data="{\"ref\": \"refs/heads/${ref}\"}" sig=$(echo -n "${data}" | openssl dgst -sha1 -hmac "${secret}" | awk '{print "X-Hub-Signature: sha1="$1}') diff --git a/publish/http.py b/publish/http.py index ec33b44..4da1873 100644 --- a/publish/http.py +++ b/publish/http.py @@ -114,6 +114,18 @@ async def handle_request(self, req: request) -> str: logger.warning(f'Request for GitHub repo \'{self.repo.name}\' was not result of push event!') abort(501) + if self.repo.branch: + if request.is_json: + data = await request.get_json() + else: + data = await request.form + + expected_ref = f'refs/heads/{self.repo.branch}' + if data['ref'] != expected_ref: + logger.debug(f'Received push-event for \'{self.repo.name}\', but for branch \'{data["ref"]}\' ' + f'instead of expected \'{expected_ref}\' - ignoring the event') + abort(204, 'Everything OK, but not following this branch. Build skipped.') + loop = asyncio.get_event_loop() # noinspection PyAsyncCall diff --git a/publish/publishing.py b/publish/publishing.py index 754bea7..832f6c2 100644 --- a/publish/publishing.py +++ b/publish/publishing.py @@ -162,6 +162,26 @@ def validate_branch(git_url: str, name: str) -> bool: return False +def get_default_branch(gir_url: str) -> str: + """ + Returns the default branch for Git repo + :param gir_url: + :return: + """ + result = subprocess.run(f'git -c core.askpass=\'echo\' ls-remote --symref {gir_url} HEAD', + shell=True, capture_output=True) + if result.returncode != 0: + raise exceptions.RepoException(f'Error while fetching Git\'s remote refs! {result.stderr.decode("utf-8")}') + + refs_list = result.stdout.decode("utf-8") + match = re.findall(r'refs/heads/([\w_-]*)\t', refs_list, re.MULTILINE) + + if len(match) != 1: + raise exceptions.RepoException('We can\'t determine which is the default branch, please specify it manually!') + + return match[0] + + def is_github_url(url: str) -> bool: """ Validate if passed URL is GitHub's url. @@ -535,11 +555,11 @@ def bootstrap_repo(cls, config: config_module.Config, name=None, git_repo_url=No default=get_name_from_url(git_repo_url), validate=lambda _, x: validate_name(x, config)).lower() - branch = cls.bootstrap_property('Branch name', 'text', 'Do you want to check-out specific branch?', branch, + branch = cls.bootstrap_property('Branch name', 'text', 'Which branch name should be build?', branch, default=DEFAULT_BRANCH_PLACEHOLDER, validate=lambda _, x: validate_branch(git_repo_url, x)) if branch == DEFAULT_BRANCH_PLACEHOLDER: - branch = None + branch = get_default_branch(git_repo_url) ipns_key, ipns_addr = bootstrap_ipns(config, name, ipns_key)