Skip to content

Commit

Permalink
github: create/preview: only include existing labels
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 8, 2018
1 parent f50cbef commit 158e2c9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
29 changes: 23 additions & 6 deletions granary/github.py
Expand Up @@ -41,6 +41,13 @@
}
}
"""
GRAPHQL_REPO = """
query {
repository(owner: "%(owner)s", name: "%(repo)s") {
id
}
}
"""
GRAPHQL_REPO_ISSUES = """
query {
viewer {
Expand All @@ -60,10 +67,14 @@
}
}
"""
GRAPHQL_REPO = """
GRAPHQL_REPO_LABELS = """
query {
repository(owner: "%(owner)s", name: "%(repo)s") {
id
labels(first:100) {
nodes {
name
}
}
}
}
"""
Expand Down Expand Up @@ -392,8 +403,9 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
ignore_formatting=False):
"""Creates a new issue or comment.
Tags are converted to labels. If a tag doesn't atch an existing label, a new
label will be created for it. You must be a collaborator on the repo
When creating a new issue, if the authenticated user is a collaborator on
the repo, tags that match existing labels are converted to those labels and
included.
https://developer.github.com/v4/guides/forming-calls/#about-mutations
https://developer.github.com/v4/mutation/addcomment/
Expand Down Expand Up @@ -464,8 +476,13 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
else: # new issue
title = util.ellipsize(obj.get('displayName') or obj.get('title') or
orig_content)
labels = list(util.trim_nulls(
labels_resp = self.graphql(GRAPHQL_REPO_LABELS, locals())
existing_labels = set(node['name'] for node in
labels_resp['repository']['labels']['nodes'])
labels = set(util.trim_nulls(
tag.get('displayName', '').strip() for tag in obj.get('tags', [])))
labels &= existing_labels

if preview:
preview_content = '<b>%s</b><hr>%s' % (
title, self.render_markdown(content, owner, repo))
Expand All @@ -480,7 +497,7 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
resp = self.rest(REST_API_CREATE_ISSUE % (owner, repo), {
'title': title,
'body': content,
'labels': labels,
'labels': list(labels),
}).json()
resp['url'] = resp.pop('html_url')
return source.creation_result(resp)
Expand Down
27 changes: 24 additions & 3 deletions granary/test/test_github.py
Expand Up @@ -167,6 +167,8 @@ def tag_uri(name):
}, {
'displayName': 'label 2\t\n',
'objectType': 'hashtag',
}, {
'displayName': 'label 3',
}],
})
REPO_REST = {
Expand Down Expand Up @@ -373,6 +375,20 @@ def expect_graphql_issue(self):
},
})

def expect_graphql_get_labels(self, labels):
self.expect_graphql(json={
'query': github.GRAPHQL_REPO_LABELS % {
'owner': 'foo',
'repo': 'bar',
},
}, response={
'repository': {
'labels': {
'nodes': [{'name': l} for l in labels],
},
},
})

def expect_graphql_add_reaction(self):
self.expect_graphql(json={
'query': github.GRAPHQL_ADD_REACTION % {
Expand Down Expand Up @@ -663,10 +679,11 @@ def test_create_issue_issues_url(self):
self._test_create_issue('https://github.com/foo/bar/issues')

def _test_create_issue(self, in_reply_to):
self.expect_graphql_get_labels([])
self.expect_requests_post(github.REST_API_CREATE_ISSUE % ('foo', 'bar'), json={
'title': 'an issue title',
'body': ISSUE_OBJ['content'].strip(),
'labels': ['new silo'],
'labels': [],
}, response={
'id': '789999',
'number': '123',
Expand All @@ -687,6 +704,7 @@ def _test_create_issue(self, in_reply_to):
}, result.content)

def test_create_with_image_and_link(self):
self.expect_graphql_get_labels([])
self.expect_requests_post(github.REST_API_CREATE_ISSUE % ('foo', 'bar'), json={
'title': 'an issue title',
'body': '[bar](http://foo/) ![](https://baz/)',
Expand All @@ -712,6 +730,7 @@ def test_create_with_image_and_link(self):

def test_preview_issue(self):
for i in range(2):
self.expect_graphql_get_labels(['new silo'])
rendered = self.expect_markdown_render(ISSUE_OBJ['content'].strip())
self.mox.ReplayAll()

Expand All @@ -726,11 +745,12 @@ def test_preview_issue(self):
preview.description, preview)

def test_create_issue_tags_to_labels(self):
self.expect_graphql_get_labels(['label_1', 'label 3'])
resp = {'html_url': 'http://done'}
self.expect_requests_post(github.REST_API_CREATE_ISSUE % ('foo', 'bar'), json={
'title': 'an issue title',
'body': ISSUE_OBJ['content'].strip(),
'labels': ['label_1', 'label 2'],
'labels': ['label_1', 'label 3'],
}, response=resp, headers=EXPECTED_HEADERS)
self.mox.ReplayAll()

Expand All @@ -739,13 +759,14 @@ def test_create_issue_tags_to_labels(self):
self.assert_equals({'url': 'http://done'}, result.content)

def test_preview_issue_tags_to_labels(self):
self.expect_graphql_get_labels(['label_1', 'label 3'])
rendered = self.expect_markdown_render(ISSUE_OBJ_WITH_LABELS['content'].strip())
self.mox.ReplayAll()

preview = self.gh.preview_create(ISSUE_OBJ_WITH_LABELS)
self.assertIsNone(preview.error_plain, preview)
self.assertIn(
'<span class="verb">create a new issue</span> on <a href="https://github.com/foo/bar/issues">foo/bar</a> and attempt to add labels <span class="verb">label_1, label 2</span>:',
'<span class="verb">create a new issue</span> on <a href="https://github.com/foo/bar/issues">foo/bar</a> and attempt to add labels <span class="verb">label_1, label 3</span>:',
preview.description, preview)

def test_create_comment_without_in_reply_to(self):
Expand Down

0 comments on commit 158e2c9

Please sign in to comment.