Skip to content

Commit

Permalink
Fixing listening for repo's events only for specific ref
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Feb 28, 2019
1 parent 8bc6eb1 commit 7a6649b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
5 changes: 3 additions & 2 deletions github_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Expand Down
12 changes: 12 additions & 0 deletions publish/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions publish/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 7a6649b

Please sign in to comment.