Browse Source

Merge "Fix manual dequeue of github items"

tags/3.3.1
Zuul 5 months ago
parent
commit
2baecbeb41
4 changed files with 76 additions and 6 deletions
  1. 4
    4
      tests/base.py
  2. 1
    1
      tests/fakegithub.py
  3. 65
    0
      tests/unit/test_github_driver.py
  4. 6
    1
      zuul/driver/github/githubconnection.py

+ 4
- 4
tests/base.py View File

@@ -1228,7 +1228,7 @@ class FakeGithubConnection(githubconnection.GithubConnection):
1228 1228
         self.getGithubClient(project).addProject(project)
1229 1229
 
1230 1230
     def _getPullReviews(self, owner, project, number):
1231
-        pr = self.pull_requests[number]
1231
+        pr = self.pull_requests[int(number)]
1232 1232
         return pr.reviews
1233 1233
 
1234 1234
     def getGitUrl(self, project):
@@ -1246,13 +1246,13 @@ class FakeGithubConnection(githubconnection.GithubConnection):
1246 1246
     def commentPull(self, project, pr_number, message):
1247 1247
         # record that this got reported
1248 1248
         self.reports.append((project, pr_number, 'comment'))
1249
-        pull_request = self.pull_requests[pr_number]
1249
+        pull_request = self.pull_requests[int(pr_number)]
1250 1250
         pull_request.addComment(message)
1251 1251
 
1252 1252
     def mergePull(self, project, pr_number, commit_message='', sha=None):
1253 1253
         # record that this got reported
1254 1254
         self.reports.append((project, pr_number, 'merge'))
1255
-        pull_request = self.pull_requests[pr_number]
1255
+        pull_request = self.pull_requests[int(pr_number)]
1256 1256
         if self.merge_failure:
1257 1257
             raise Exception('Pull request was not merged')
1258 1258
         if self.merge_not_allowed_count > 0:
@@ -1272,7 +1272,7 @@ class FakeGithubConnection(githubconnection.GithubConnection):
1272 1272
     def labelPull(self, project, pr_number, label):
1273 1273
         # record that this got reported
1274 1274
         self.reports.append((project, pr_number, 'label', label))
1275
-        pull_request = self.pull_requests[pr_number]
1275
+        pull_request = self.pull_requests[int(pr_number)]
1276 1276
         pull_request.addLabel(label)
1277 1277
 
1278 1278
     def unlabelPull(self, project, pr_number, label):

+ 1
- 1
tests/fakegithub.py View File

@@ -379,7 +379,7 @@ class FakeGithubClient(object):
379 379
             project_name, self._data)
380 380
 
381 381
     def pull_request(self, owner, project, number):
382
-        fake_pr = self._data.pull_requests[number]
382
+        fake_pr = self._data.pull_requests[int(number)]
383 383
         return FakePull(fake_pr)
384 384
 
385 385
     def search_issues(self, query):

+ 65
- 0
tests/unit/test_github_driver.py View File

@@ -23,6 +23,8 @@ from unittest import mock, skip
23 23
 import git
24 24
 import github3.exceptions
25 25
 
26
+import zuul.rpcclient
27
+
26 28
 from tests.base import ZuulTestCase, simple_layout, random_sha1
27 29
 from tests.base import ZuulWebFixture
28 30
 
@@ -52,6 +54,8 @@ class TestGithubDriver(ZuulTestCase):
52 54
         self.assertEqual(str(A.number), zuulvars['change'])
53 55
         self.assertEqual(str(A.head_sha), zuulvars['patchset'])
54 56
         self.assertEqual('master', zuulvars['branch'])
57
+        self.assertEquals('https://github.com/org/project/pull/1',
58
+                          zuulvars['items'][0]['change_url'])
55 59
         self.assertEqual(1, len(A.comments))
56 60
         self.assertThat(
57 61
             A.comments[0],
@@ -935,6 +939,67 @@ class TestGithubDriver(ZuulTestCase):
935 939
                                           new_sha='0' * 40,
936 940
                                           modified_files=['README.md'])
937 941
 
942
+    @simple_layout('layouts/basic-github.yaml', driver='github')
943
+    def test_client_dequeue_change_github(self):
944
+        "Test that the RPC client can dequeue a github pull request"
945
+
946
+        client = zuul.rpcclient.RPCClient('127.0.0.1',
947
+                                          self.gearman_server.port)
948
+        self.addCleanup(client.shutdown)
949
+
950
+        self.executor_server.hold_jobs_in_build = True
951
+        A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
952
+
953
+        self.fake_github.emitEvent(A.getPullRequestOpenedEvent())
954
+        self.waitUntilSettled()
955
+
956
+        client.dequeue(
957
+            tenant='tenant-one',
958
+            pipeline='check',
959
+            project='org/project',
960
+            change='{},{}'.format(A.number, A.head_sha),
961
+            ref=None)
962
+
963
+        self.waitUntilSettled()
964
+
965
+        tenant = self.sched.abide.tenants.get('tenant-one')
966
+        check_pipeline = tenant.layout.pipelines['check']
967
+        self.assertEqual(check_pipeline.getAllItems(), [])
968
+        self.assertEqual(self.countJobResults(self.history, 'ABORTED'), 2)
969
+
970
+        self.executor_server.hold_jobs_in_build = False
971
+        self.executor_server.release()
972
+        self.waitUntilSettled()
973
+
974
+    @simple_layout('layouts/basic-github.yaml', driver='github')
975
+    def test_client_enqueue_change_github(self):
976
+        "Test that the RPC client can enqueue a pull request"
977
+        A = self.fake_github.openFakePullRequest('org/project', 'master', 'A')
978
+
979
+        client = zuul.rpcclient.RPCClient('127.0.0.1',
980
+                                          self.gearman_server.port)
981
+        self.addCleanup(client.shutdown)
982
+        r = client.enqueue(tenant='tenant-one',
983
+                           pipeline='check',
984
+                           project='org/project',
985
+                           trigger='github',
986
+                           change='{},{}'.format(A.number, A.head_sha))
987
+        self.waitUntilSettled()
988
+
989
+        self.assertEqual(self.getJobFromHistory('project-test1').result,
990
+                         'SUCCESS')
991
+        self.assertEqual(self.getJobFromHistory('project-test2').result,
992
+                         'SUCCESS')
993
+        self.assertEqual(r, True)
994
+
995
+        # check that change_url is correct
996
+        job1_params = self.getJobFromHistory('project-test1').parameters
997
+        job2_params = self.getJobFromHistory('project-test2').parameters
998
+        self.assertEquals('https://github.com/org/project/pull/1',
999
+                          job1_params['zuul']['items'][0]['change_url'])
1000
+        self.assertEquals('https://github.com/org/project/pull/1',
1001
+                          job2_params['zuul']['items'][0]['change_url'])
1002
+
938 1003
 
939 1004
 class TestGithubUnprotectedBranches(ZuulTestCase):
940 1005
     config_file = 'zuul-github-driver.conf'

+ 6
- 1
zuul/driver/github/githubconnection.py View File

@@ -786,7 +786,12 @@ class GithubConnection(BaseConnection):
786 786
         if event.change_number:
787 787
             change = self._getChange(project, event.change_number,
788 788
                                      event.patch_number, refresh=refresh)
789
-            change.url = event.change_url
789
+            if hasattr(event, 'change_url') and event.change_url:
790
+                change.url = event.change_url
791
+            else:
792
+                # The event has no change url so just construct it
793
+                change.url = self.getPullUrl(
794
+                    event.project_name, event.change_number)
790 795
             change.uris = [
791 796
                 '%s/%s/pull/%s' % (self.server, project, change.number),
792 797
             ]

Loading…
Cancel
Save