Skip to content

Commit

Permalink
Merge pull request #1721 from edgemaster/issue-2876
Browse files Browse the repository at this point in the history
Add context field to GitHubStatus updates
  • Loading branch information
Mikhail Sobolev committed Jun 16, 2015
2 parents acdd692 + 0d0e0c3 commit 5330478
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
9 changes: 7 additions & 2 deletions master/buildbot/status/github.py
Expand Up @@ -49,6 +49,7 @@ def _getGitHubState(results):


class GitHubStatus(StatusReceiverMultiService):

"""
Send build status to GitHub.
Expand All @@ -59,7 +60,7 @@ class GitHubStatus(StatusReceiverMultiService):

def __init__(self, token, repoOwner, repoName, sha=None,
startDescription=None, endDescription=None,
baseURL=None):
baseURL=None, context=None):
"""
Token for GitHub API.
"""
Expand All @@ -71,6 +72,7 @@ def __init__(self, token, repoOwner, repoName, sha=None,
self._sha = sha or Interpolate("%(src::revision)s")
self._repoOwner = repoOwner
self._repoName = repoName
self._context = context or Interpolate("buildbot/%(prop:buildername)s")
self._startDescription = startDescription or "Build started."
self._endDescription = endDescription or "Build done."

Expand Down Expand Up @@ -166,10 +168,11 @@ def _getGitHubRepoProperties(self, build):
"""
Return a dictionary with GitHub related properties from `build`.
"""
repoOwner, repoName, sha = yield defer.gatherResults([
repoOwner, repoName, sha, context = yield defer.gatherResults([
build.render(self._repoOwner),
build.render(self._repoName),
build.render(self._sha),
build.render(self._context),
])

if not repoOwner or not repoName:
Expand All @@ -185,6 +188,7 @@ def _getGitHubRepoProperties(self, build):
'sha': sha,
'targetURL': self._status.getURLForThing(build),
'buildNumber': str(build.getNumber()),
'context': context,
}
defer.returnValue(result)

Expand All @@ -200,6 +204,7 @@ def _sendGitHubStatus(self, status):
state=status['state'].encode('utf-8'),
target_url=status['targetURL'].encode('utf-8'),
description=status['description'].encode('utf-8'),
context=status['context'].encode('utf-8'),
)

success_message = (
Expand Down
14 changes: 12 additions & 2 deletions master/buildbot/test/unit/test_status_github.py
Expand Up @@ -132,6 +132,8 @@ def test_initialization_required_arguments(self):
self.assertEqual(status._sha, Interpolate("%(src::revision)s"))
self.assertEqual(status._startDescription, "Build started.")
self.assertEqual(status._endDescription, "Build done.")
self.assertEqual(status._context,
Interpolate("buildbot/%(prop:buildername)s"))

def test_custom_github_url(self):
"""
Expand Down Expand Up @@ -271,6 +273,7 @@ def test_sendStartStatus_ok(self):
'sha': '123',
'targetURL': 'http://example.tld',
'buildNumber': '1',
'context': 'context',
}
self.status._sendGitHubStatus = Mock(return_value=defer.succeed(None))
self.build.getTimes = lambda: (1, None)
Expand All @@ -286,6 +289,7 @@ def test_sendStartStatus_ok(self):
'sha': '123',
'targetURL': 'http://example.tld',
'buildNumber': '1',
'context': 'context',
# Augmented arguments.
'state': 'pending',
'description': 'Build started.',
Expand Down Expand Up @@ -330,6 +334,7 @@ def test_sendFinishStatus_ok(self):
'sha': '123',
'targetURL': 'http://example.tld',
'buildNumber': '1',
'context': 'context',
}
self.status._sendGitHubStatus = Mock(return_value=defer.succeed(None))
self.build.getTimes = lambda: (1, 3)
Expand All @@ -346,6 +351,7 @@ def test_sendFinishStatus_ok(self):
'sha': '123',
'targetURL': 'http://example.tld',
'buildNumber': '1',
'context': 'context',
# Augmented arguments.
'state': 'success',
'description': 'Build done.',
Expand Down Expand Up @@ -398,6 +404,7 @@ def test_getGitHubRepoProperties_ok(self):
self.status._repoOwner = Interpolate('owner')
self.status._repoName = Interpolate('name')
self.status._sha = Interpolate('sha')
self.status._context = Interpolate('context')
self.status._status = Mock()
self.status._status.getURLForThing = lambda build: 'http://example.org'
self.build.getNumber = lambda: 1
Expand All @@ -411,8 +418,8 @@ def test_getGitHubRepoProperties_ok(self):
'repoOwner': 'owner',
'sha': 'sha',
'targetURL': 'http://example.org',
},
result)
'context': 'context',
}, result)

def test_getGitHubState(self):
"""
Expand All @@ -435,6 +442,7 @@ def test_sendGitHubStatus_success(self):
'state': u'state-resum\xe9',
'targetURL': u'targetURL-resum\xe9',
'description': u'description-resum\xe9',
'context': u'context-resum\xe9',
}
self.status._github.repos.createStatus = Mock(
return_value=defer.succeed(None))
Expand All @@ -450,6 +458,7 @@ def test_sendGitHubStatus_success(self):
state='state-resum\xc3\xa9',
target_url='targetURL-resum\xc3\xa9',
description='description-resum\xc3\xa9',
context='context-resum\xc3\xa9',
)

self.assertLog(
Expand All @@ -468,6 +477,7 @@ def test_sendGitHubStatus_error(self):
'state': u'state',
'targetURL': u'targetURL',
'description': u'description',
'context': u'context',
}
error = MarkerError('fail-send-status')
self.status._github.repos.createStatus = Mock(
Expand Down
5 changes: 5 additions & 0 deletions master/docs/manual/cfg-statustargets.rst
Expand Up @@ -763,10 +763,12 @@ GitHubStatus
repoOwner = Interpolate("%(prop:github_repo_owner)s")
repoName = Interpolate("%(prop:github_repo_name)s")
sha = Interpolate("%(src::revision)s")
context = Interpolate("buildbot/%(prop:buildername)s")
gs = status.GitHubStatus(token='githubAPIToken',
repoOwner=repoOwner,
repoName=repoName,
sha=sha,
context=context,
startDescription='Build started.',
endDescription='Build done.')
buildbot_bbtools = util.BuilderConfig(
Expand Down Expand Up @@ -797,6 +799,9 @@ By default `sha` is defined as: `%(src::revision)s`.

In case any of `repoOwner`, `repoName` or `sha` returns `None`, `False` or empty string, the plugin will skip sending the status.

The `context` argument is passed to GitHub to differentiate between statuses. A static string can be passed or :class:`Interpolate` for dynamic substitution.
The default context is `buildbot/%(prop:buildername)s`.

You can define custom start and end build messages using the `startDescription` and `endDescription` optional interpolation arguments.

Starting with Buildbot version 0.8.11, :class:`GitHubStatus` supports additional parameter -- ``baseURL`` -- that allows to specify a different API base endpoint.
Expand Down
2 changes: 2 additions & 0 deletions master/docs/relnotes/index.rst
Expand Up @@ -129,6 +129,8 @@ Their API has slightly to changed in order to adapt to the new data API.

* :bb:reporter:`MailNotifier` no longer forces SSL 3.0 when ``useTls`` is true.

* :class:`~buildbot.status.github.GitHubStatus` now accepts a ``context`` parameter to be passed to the GitHub Status API.

Fixes
~~~~~

Expand Down

0 comments on commit 5330478

Please sign in to comment.