def test_triggers_webhooks(self): # Jobs trigger any relevant webhooks when they're enabled. self.useFixture(FeatureFixture({'code.git.webhooks.enabled': 'on'})) repository = self.factory.makeGitRepository() self.factory.makeGitRefs( repository, paths=['refs/heads/master', 'refs/tags/1.0']) hook = self.factory.makeWebhook( target=repository, event_types=['git:push:0.1']) job = GitRefScanJob.create(repository) paths = ('refs/heads/master', 'refs/tags/2.0') self.useFixture(GitHostingFixture(refs=self.makeFakeRefs(paths))) with dbuser('branchscanner'): JobRunner([job]).runAll() delivery = hook.deliveries.one() sha1 = lambda s: hashlib.sha1(s).hexdigest() self.assertThat( delivery, MatchesStructure( event_type=Equals('git:push:0.1'), payload=MatchesDict({ 'git_repository': Equals('/' + repository.unique_name), 'git_repository_path': Equals(repository.unique_name), 'ref_changes': Equals({ 'refs/tags/1.0': { 'old': {'commit_sha1': sha1('refs/tags/1.0')}, 'new': None}, 'refs/tags/2.0': { 'old': None, 'new': {'commit_sha1': sha1('refs/tags/2.0')}}, })}))) with dbuser(config.IWebhookDeliveryJobSource.dbuser): self.assertEqual( "<WebhookDeliveryJob for webhook %d on %r>" % ( hook.id, hook.target), repr(delivery))
def test_composeWebhookPayload(self): repository = self.factory.makeGitRepository() self.factory.makeGitRefs( repository, paths=['refs/heads/master', 'refs/tags/1.0']) sha1 = lambda s: hashlib.sha1(s).hexdigest() new_refs = { 'refs/heads/master': { 'sha1': sha1('master-ng'), 'type': 'commit'}, 'refs/tags/2.0': { 'sha1': sha1('2.0'), 'type': 'commit'}, } removed_refs = ['refs/tags/1.0'] payload = GitRefScanJob.composeWebhookPayload( repository, new_refs, removed_refs) self.assertEqual( {'git_repository': '/' + repository.unique_name, 'git_repository_path': repository.unique_name, 'ref_changes': { 'refs/heads/master': { 'old': {'commit_sha1': sha1('refs/heads/master')}, 'new': {'commit_sha1': sha1('master-ng')}}, 'refs/tags/1.0': { 'old': {'commit_sha1': sha1('refs/tags/1.0')}, 'new': None}, 'refs/tags/2.0': { 'old': None, 'new': {'commit_sha1': sha1('2.0')}}}}, payload)
def test_logs_bad_ref_info(self): repository = self.factory.makeGitRepository() job = GitRefScanJob.create(repository) self.useFixture(GitHostingFixture(refs={"refs/heads/master": {}})) expected_message = ( 'Unconvertible ref refs/heads/master {}: ' 'ref info does not contain "object" key') with self.expectedLog(expected_message): with dbuser("branchscanner"): JobRunner([job]).runAll() self.assertEqual([], list(repository.refs))
def test_run(self): # Ensure the job scans the repository. repository = self.factory.makeGitRepository() job = GitRefScanJob.create(repository) paths = ("refs/heads/master", "refs/tags/1.0") author = repository.owner author_date_start = datetime(2015, 1, 1, tzinfo=pytz.UTC) author_date_gen = time_counter(author_date_start, timedelta(days=1)) self.useFixture(GitHostingFixture( refs=self.makeFakeRefs(paths), commits=self.makeFakeCommits(author, author_date_gen, paths))) with dbuser("branchscanner"): JobRunner([job]).runAll() self.assertRefsMatch(repository.refs, repository, paths) self.assertEqual("refs/heads/master", repository.default_branch)
def test_merge_detection_triggers_webhooks(self): self.useFixture(FeatureFixture( {BRANCH_MERGE_PROPOSAL_WEBHOOKS_FEATURE_FLAG: 'on'})) repository = self.factory.makeGitRepository() target, source = self.factory.makeGitRefs( repository, paths=['refs/heads/target', 'refs/heads/source']) bmp = self.factory.makeBranchMergeProposalForGit( target_ref=target, source_ref=source) hook = self.factory.makeWebhook( target=repository, event_types=['merge-proposal:0.1']) new_refs = { target.path: {'object': { 'sha1': '0' * 40, 'type': 'commit', }}, source.path: {'object': { 'sha1': source.commit_sha1, 'type': 'commit', }}, } new_merges = {source.commit_sha1: '0' * 40} self.useFixture(GitHostingFixture(refs=new_refs, merges=new_merges)) job = GitRefScanJob.create(repository) with dbuser('branchscanner'): JobRunner([job]).runAll() delivery = hook.deliveries.one() self.assertThat( delivery, MatchesStructure( event_type=Equals('merge-proposal:0.1'), payload=MatchesDict({ 'merge_proposal': Equals( canonical_url(bmp, force_local_path=True)), 'action': Equals('modified'), 'old': ContainsDict( {'queue_status': Equals('Work in progress')}), 'new': ContainsDict({'queue_status': Equals('Merged')}), }))) with dbuser(config.IWebhookDeliveryJobSource.dbuser): self.assertEqual( "<WebhookDeliveryJob for webhook %d on %r>" % ( hook.id, hook.target), repr(delivery))
def test___repr__(self): # `GitRefScanJob` objects have an informative __repr__. repository = self.factory.makeGitRepository() job = GitRefScanJob.create(repository) self.assertEqual( "<GitRefScanJob for %s>" % repository.unique_name, repr(job))
def test_provides_interface(self): # `GitRefScanJob` objects provide `IGitRefScanJob`. repository = self.factory.makeGitRepository() self.assertProvides(GitRefScanJob.create(repository), IGitRefScanJob)