webhooks: Add support for a GitHub push event deleting a branch.

Fixes: #4742.
This commit is contained in:
Eeshan Garg 2017-05-16 00:08:25 -02:30
parent 0b46be2fed
commit 7227f488c8
5 changed files with 152 additions and 7 deletions

View File

@ -20,6 +20,7 @@ PUSH_COMMITS_MESSAGE_TEMPLATE_WITHOUT_COMMITTERS = u"""{user_name} {pushed_text}
{commits_data}
"""
PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE = u"{user_name} [deleted]({compare_url}) the branch {branch_name}."
PUSH_COMMITS_MESSAGE_EXTENSION = u"Commits by {}"
PUSH_COMMITTERS_LIMIT_INFO = 3
@ -43,8 +44,15 @@ TAG_WITH_URL_TEMPLATE = u"[{tag_name}]({tag_url})"
TAG_WITHOUT_URL_TEMPLATE = u"{tag_name}"
def get_push_commits_event_message(user_name, compare_url, branch_name, commits_data, is_truncated=False):
# type: (Text, Optional[Text], Text, List[Dict[str, Any]], Optional[bool]) -> Text
def get_push_commits_event_message(user_name, compare_url, branch_name, commits_data, is_truncated=False, deleted=False):
# type: (Text, Optional[Text], Text, List[Dict[str, Any]], Optional[bool], Optional[bool]) -> Text
if not commits_data and deleted:
return PUSH_DELETE_BRANCH_MESSAGE_TEMPLATE.format(
user_name=user_name,
compare_url=compare_url,
branch_name=branch_name
)
pushed_message_template = PUSH_PUSHED_TEXT_WITH_URL if compare_url else PUSH_PUSHED_TEXT_WITHOUT_URL
pushed_text_message = pushed_message_template.format(

View File

@ -172,7 +172,8 @@ def api_github_v2(user_profile, event, payload, branches, default_stream,
payload['compare'],
payload['pusher']['name'],
forced=payload['forced'],
created=payload['created'])
created=payload['created'],
deleted=payload['deleted'])
elif event == 'commit_comment':
subject = topic_focus
@ -279,8 +280,8 @@ def api_github_landing(request, user_profile, event=REQ(),
forged=False, subject_name=subject,
message_content=content)
def build_message_from_gitlog(user_profile, name, ref, commits, before, after, url, pusher, forced=None, created=None):
# type: (UserProfile, Text, Text, List[Dict[str, str]], Text, Text, Text, Text, Optional[Text], Optional[Text]) -> Tuple[Text, Text]
def build_message_from_gitlog(user_profile, name, ref, commits, before, after, url, pusher, forced=None, created=None, deleted=False):
# type: (UserProfile, Text, Text, List[Dict[str, str]], Text, Text, Text, Text, Optional[Text], Optional[Text], Optional[bool]) -> Tuple[Text, Text]
short_ref = re.sub(r'^refs/heads/', '', ref)
subject = SUBJECT_WITH_BRANCH_TEMPLATE.format(repo=name, branch=short_ref)
@ -291,7 +292,7 @@ def build_message_from_gitlog(user_profile, name, ref, commits, before, after, u
content = get_force_push_commits_event_message(pusher, url, short_ref, after[:7])
else:
commits = _transform_commits_list_to_common_format(commits)
content = get_push_commits_event_message(pusher, url, short_ref, commits)
content = get_push_commits_event_message(pusher, url, short_ref, commits, deleted=deleted)
return subject, content

View File

@ -0,0 +1,130 @@
{
"ref":"refs/heads/feature",
"before":"2e8cf535fb38a3dab2476cdf856efda904ad4c94",
"after":"0000000000000000000000000000000000000000",
"created":false,
"deleted":true,
"forced":false,
"base_ref":null,
"compare":"https://github.com/eeshangarg/public-repo/compare/2e8cf535fb38...000000000000",
"commits":[
],
"head_commit":null,
"repository":{
"id":89799239,
"name":"public-repo",
"full_name":"eeshangarg/public-repo",
"owner":{
"name":"eeshangarg",
"email":"jerryguitarist@gmail.com",
"login":"eeshangarg",
"id":7251823,
"avatar_url":"https://avatars3.githubusercontent.com/u/7251823?v=3",
"gravatar_id":"",
"url":"https://api.github.com/users/eeshangarg",
"html_url":"https://github.com/eeshangarg",
"followers_url":"https://api.github.com/users/eeshangarg/followers",
"following_url":"https://api.github.com/users/eeshangarg/following{/other_user}",
"gists_url":"https://api.github.com/users/eeshangarg/gists{/gist_id}",
"starred_url":"https://api.github.com/users/eeshangarg/starred{/owner}{/repo}",
"subscriptions_url":"https://api.github.com/users/eeshangarg/subscriptions",
"organizations_url":"https://api.github.com/users/eeshangarg/orgs",
"repos_url":"https://api.github.com/users/eeshangarg/repos",
"events_url":"https://api.github.com/users/eeshangarg/events{/privacy}",
"received_events_url":"https://api.github.com/users/eeshangarg/received_events",
"type":"User",
"site_admin":false
},
"private":false,
"html_url":"https://github.com/eeshangarg/public-repo",
"description":null,
"fork":false,
"url":"https://github.com/eeshangarg/public-repo",
"forks_url":"https://api.github.com/repos/eeshangarg/public-repo/forks",
"keys_url":"https://api.github.com/repos/eeshangarg/public-repo/keys{/key_id}",
"collaborators_url":"https://api.github.com/repos/eeshangarg/public-repo/collaborators{/collaborator}",
"teams_url":"https://api.github.com/repos/eeshangarg/public-repo/teams",
"hooks_url":"https://api.github.com/repos/eeshangarg/public-repo/hooks",
"issue_events_url":"https://api.github.com/repos/eeshangarg/public-repo/issues/events{/number}",
"events_url":"https://api.github.com/repos/eeshangarg/public-repo/events",
"assignees_url":"https://api.github.com/repos/eeshangarg/public-repo/assignees{/user}",
"branches_url":"https://api.github.com/repos/eeshangarg/public-repo/branches{/branch}",
"tags_url":"https://api.github.com/repos/eeshangarg/public-repo/tags",
"blobs_url":"https://api.github.com/repos/eeshangarg/public-repo/git/blobs{/sha}",
"git_tags_url":"https://api.github.com/repos/eeshangarg/public-repo/git/tags{/sha}",
"git_refs_url":"https://api.github.com/repos/eeshangarg/public-repo/git/refs{/sha}",
"trees_url":"https://api.github.com/repos/eeshangarg/public-repo/git/trees{/sha}",
"statuses_url":"https://api.github.com/repos/eeshangarg/public-repo/statuses/{sha}",
"languages_url":"https://api.github.com/repos/eeshangarg/public-repo/languages",
"stargazers_url":"https://api.github.com/repos/eeshangarg/public-repo/stargazers",
"contributors_url":"https://api.github.com/repos/eeshangarg/public-repo/contributors",
"subscribers_url":"https://api.github.com/repos/eeshangarg/public-repo/subscribers",
"subscription_url":"https://api.github.com/repos/eeshangarg/public-repo/subscription",
"commits_url":"https://api.github.com/repos/eeshangarg/public-repo/commits{/sha}",
"git_commits_url":"https://api.github.com/repos/eeshangarg/public-repo/git/commits{/sha}",
"comments_url":"https://api.github.com/repos/eeshangarg/public-repo/comments{/number}",
"issue_comment_url":"https://api.github.com/repos/eeshangarg/public-repo/issues/comments{/number}",
"contents_url":"https://api.github.com/repos/eeshangarg/public-repo/contents/{+path}",
"compare_url":"https://api.github.com/repos/eeshangarg/public-repo/compare/{base}...{head}",
"merges_url":"https://api.github.com/repos/eeshangarg/public-repo/merges",
"archive_url":"https://api.github.com/repos/eeshangarg/public-repo/{archive_format}{/ref}",
"downloads_url":"https://api.github.com/repos/eeshangarg/public-repo/downloads",
"issues_url":"https://api.github.com/repos/eeshangarg/public-repo/issues{/number}",
"pulls_url":"https://api.github.com/repos/eeshangarg/public-repo/pulls{/number}",
"milestones_url":"https://api.github.com/repos/eeshangarg/public-repo/milestones{/number}",
"notifications_url":"https://api.github.com/repos/eeshangarg/public-repo/notifications{?since,all,participating}",
"labels_url":"https://api.github.com/repos/eeshangarg/public-repo/labels{/name}",
"releases_url":"https://api.github.com/repos/eeshangarg/public-repo/releases{/id}",
"deployments_url":"https://api.github.com/repos/eeshangarg/public-repo/deployments",
"created_at":1493484776,
"updated_at":"2017-04-29T16:52:56Z",
"pushed_at":1494897868,
"git_url":"git://github.com/eeshangarg/public-repo.git",
"ssh_url":"git@github.com:eeshangarg/public-repo.git",
"clone_url":"https://github.com/eeshangarg/public-repo.git",
"svn_url":"https://github.com/eeshangarg/public-repo",
"homepage":null,
"size":0,
"stargazers_count":0,
"watchers_count":0,
"language":null,
"has_issues":true,
"has_projects":true,
"has_downloads":true,
"has_wiki":true,
"has_pages":false,
"forks_count":0,
"mirror_url":null,
"open_issues_count":0,
"forks":0,
"open_issues":0,
"watchers":0,
"default_branch":"changes",
"stargazers":0,
"master_branch":"changes"
},
"pusher":{
"name":"eeshangarg",
"email":"jerryguitarist@gmail.com"
},
"sender":{
"login":"eeshangarg",
"id":7251823,
"avatar_url":"https://avatars3.githubusercontent.com/u/7251823?v=3",
"gravatar_id":"",
"url":"https://api.github.com/users/eeshangarg",
"html_url":"https://github.com/eeshangarg",
"followers_url":"https://api.github.com/users/eeshangarg/followers",
"following_url":"https://api.github.com/users/eeshangarg/following{/other_user}",
"gists_url":"https://api.github.com/users/eeshangarg/gists{/gist_id}",
"starred_url":"https://api.github.com/users/eeshangarg/starred{/owner}{/repo}",
"subscriptions_url":"https://api.github.com/users/eeshangarg/subscriptions",
"organizations_url":"https://api.github.com/users/eeshangarg/orgs",
"repos_url":"https://api.github.com/users/eeshangarg/repos",
"events_url":"https://api.github.com/users/eeshangarg/events{/privacy}",
"received_events_url":"https://api.github.com/users/eeshangarg/received_events",
"type":"User",
"site_admin":false
}
}

View File

@ -23,6 +23,11 @@ class GithubWebhookTest(WebhookTestCase):
expected_message = u"GitHub webhook has been successfully configured by TomaszKolek"
self.send_and_test_stream_message('ping', self.EXPECTED_SUBJECT_REPO_EVENTS, expected_message, HTTP_X_GITHUB_EVENT='ping')
def test_push_delete_branch(self):
# type: () -> None
expected_message = u"eeshangarg [deleted](https://github.com/eeshangarg/public-repo/compare/2e8cf535fb38...000000000000) the branch feature."
self.send_and_test_stream_message('push_delete_branch', u"public-repo / feature", expected_message, HTTP_X_GITHUB_EVENT='push')
def test_push_1_commit(self):
# type: () -> None
expected_message = u"baxterthehacker [pushed](https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f) 1 commit to branch changes.\n\n* Update README.md ([0d1a26e](https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c))"

View File

@ -190,7 +190,8 @@ def get_push_commits_body(payload):
get_sender_name(payload),
payload['compare'],
get_branch_name_from_ref(payload['ref']),
commits_data
commits_data,
deleted=payload['deleted']
)
def get_public_body(payload):