Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reporters/github: Add regex for report notifications only for allowed… #6601

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions master/buildbot/reporters/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
from buildbot.util.giturlparse import giturlparse

HOSTED_BASE_URL = 'https://api.github.com'
HOSTED_BASE_REGEX = r'github\.com\/(.+?)\/'


class GitHubStatusPush(ReporterBase):
name = "GitHubStatusPush"

def checkConfig(self, token, context=None, baseURL=None, verbose=False,
def checkConfig(self, token, context=None, baseURL=None, base_re=None, verbose=False,
debug=None, verify=None, generators=None,
**kwargs):

Expand All @@ -52,13 +53,14 @@ def checkConfig(self, token, context=None, baseURL=None, verbose=False,
httpclientservice.HTTPClientService.checkAvailable(self.__class__.__name__)

@defer.inlineCallbacks
def reconfigService(self, token, context=None, baseURL=None, verbose=False,
def reconfigService(self, token, context=None, baseURL=None, base_re=None, verbose=False,
debug=None, verify=None, generators=None,
**kwargs):
token = yield self.renderSecrets(token)
self.debug = debug
self.verify = verify
self.verbose = verbose
self.base_re = base_re
self.context = self.setup_context(context)

if generators is None:
Expand Down Expand Up @@ -141,6 +143,13 @@ def _extract_github_info(self, sourcestamp):
repo_name = None
project = sourcestamp['project']
repository = sourcestamp['repository']
base_re = self.base_re
if base_re is None:
base_re = HOSTED_BASE_REGEX
github_url = re.search(base_re, repository)
if not github_url:
log.msg('skipped as not a github url')
return None, None
if project and "/" in project:
repo_owner, repo_name = project.split('/')
elif repository:
Expand Down
70 changes: 69 additions & 1 deletion master/buildbot/test/unit/reporters/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def test_https(self):
class TestGitHubCommentPush(TestGitHubStatusPush):

def createService(self):
return GitHubCommentPush('XXYYZZ')
return GitHubCommentPush('XXYYZZ', base_re=r"test\_repo\/(.+?)\/")

@defer.inlineCallbacks
def test_basic(self):
Expand Down Expand Up @@ -431,3 +431,71 @@ def test_multiple_source_stamps_no_props(self):
yield self.sp._got_event(('builds', 20, 'finished'), build)
build['results'] = SUCCESS
yield self.sp._got_event(('builds', 20, 'finished'), build)


# Testing HOSTED_BASE_REGEX
class TestGitHubRegEX(TestGitHubStatusPush):

def createService(self):
return GitHubCommentPush('XXYYZZ', base_re=r"test\_repo\/(.+?)\/")

@defer.inlineCallbacks
def test_source_stamps(self):
repository = 'http://test_repo'
project = 'test_user/test_project'
codebase1 = 'test_codebase1'
codebase2 = 'test_codebase2'
codebase3 = 'test_codebase3'
branch2 = 'refs/pull/4192/merge'
branch3 = 'refs/pull/4193/merge'

self._http.expect(
'post',
'/repos/test_user/test_project/issues/4192/comments',
json={'body': 'Build done.'})
self._http.expect(
'post',
'/repos/test_user/test_project/issues/4192/comments',
json={'body': 'Build done.'})
self._http.expect(
'post',
'/repos/test_user/test_project/issues/4192/comments',
json={'body': 'Build done.'})
self._http.expect(
'post',
'/repos/test_user/test_project/issues/4192/comments',
json={'body': 'Build done.'})

# note that the first sourcestamp only has revision, second only branch and only the third
# has both
self.master.db.insertTestData([
fakedb.Master(id=92),
fakedb.Worker(id=13, name='wrk'),
fakedb.Builder(id=79, name='Builder0'),
fakedb.Buildset(id=98, results=SUCCESS, reason="test_reason1"),
fakedb.BuildsetSourceStamp(buildsetid=98, sourcestampid=234),
fakedb.BuildsetSourceStamp(buildsetid=98, sourcestampid=235),
fakedb.BuildsetSourceStamp(buildsetid=98, sourcestampid=236),
fakedb.SourceStamp(id=234, project=project, branch=None, revision='rev1',
repository=repository, codebase=codebase1),
fakedb.SourceStamp(id=235, project=project, branch=branch2, revision=None,
repository=repository, codebase=codebase2),
fakedb.SourceStamp(id=236, project=project, branch=branch3, revision='rev3',
repository=repository, codebase=codebase3),
fakedb.BuildRequest(id=11, buildsetid=98, builderid=79),
fakedb.Build(id=20, number=0, builderid=79, buildrequestid=11,
workerid=13, masterid=92, results=SUCCESS, state_string="build_text"),
fakedb.BuildProperty(buildid=20, name="buildername", value="Builder0"),
fakedb.BuildProperty(buildid=20, name="branch", value=branch2),
])

self.setup_fake_get_changes_for_build(has_change=False)

build = yield self.master.data.get(("builds", 20))

build['complete'] = False
yield self.sp._got_event(('builds', 20, 'new'), build)
build['complete'] = True
yield self.sp._got_event(('builds', 20, 'finished'), build)
build['results'] = SUCCESS
yield self.sp._got_event(('builds', 20, 'finished'), build)
3 changes: 2 additions & 1 deletion master/docs/manual/configuration/reporters/github_status.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ It requires a GitHub API token in order to operate.

You can create a token from your own `GitHub - Profile - Applications - Register new application <https://github.com/settings/applications>`_ or use an external tool to generate one.

.. py:class:: GitHubStatusPush(token, context=None, generators=None, baseURL=None, verbose=False)
.. py:class:: GitHubStatusPush(token, context=None, generators=None, baseURL=None, base_re=None, verbose=False)

:param string token: Token used for authentication. (can be a :ref:`Secret`)
:type context: renderable string
Expand All @@ -31,6 +31,7 @@ You can create a token from your own `GitHub - Profile - Applications - Register
:param generators: A list of report generators that will be used to generate reports to be sent by this reporter.
Currently the reporter will consider only the report generated by the first generator.
:param string baseURL: Specify the github api endpoint if you work with GitHub Enterprise
:param string base_re: Specify the regular expression that sourcestamps repository need to match for approve sending the notification
:param boolean verbose: If True, logs a message for each successful status push

.. _txrequests: https://pypi.python.org/pypi/txrequests