Branch.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> # 4 # Copyright 2012 Zearin <zearin@gonk.net> # 5 # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> # 6 # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> # 7 # Copyright 2015 Kyle Hornberg <khornberg@users.noreply.github.com> # 8 # Copyright 2016 Jannis Gebauer <ja.geb@me.com> # 9 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 10 # Copyright 2018 Steve Kowalik <steven@wedontsleep.org> # 11 # Copyright 2018 sfdye <tsfdye@gmail.com> # 12 # # 13 # This file is part of PyGithub. # 14 # http://pygithub.readthedocs.io/ # 15 # # 16 # PyGithub is free software: you can redistribute it and/or modify it under # 17 # the terms of the GNU Lesser General Public License as published by the Free # 18 # Software Foundation, either version 3 of the License, or (at your option) # 19 # any later version. # 20 # # 21 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 23 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 24 # details. # 25 # # 26 # You should have received a copy of the GNU Lesser General Public License # 27 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 28 # # 29 ################################################################################ 30 31 import github 32 33 from . import Framework 34 35 36 class Branch(Framework.TestCase): 37 def setUp(self): 38 super().setUp() 39 self.repo = self.g.get_user().get_repo("PyGithub") 40 self.branch = self.repo.get_branch("topic/RewriteWithGeneratedCode") 41 self.protected_branch = self.repo.get_branch("integrations") 42 self.organization_branch = self.g.get_repo("PyGithub/PyGithub", lazy=True).get_branch("master") 43 44 def testAttributes(self): 45 self.assertEqual(self.branch.name, "topic/RewriteWithGeneratedCode") 46 self.assertEqual(self.branch.commit.sha, "1292bf0e22c796e91cc3d6e24b544aece8c21f2a") 47 self.assertEqual( 48 self.branch.protection_url, 49 "https://api.github.com/repos/jacquev6/PyGithub/branches/topic/RewriteWithGeneratedCode/protection", 50 ) 51 self.assertFalse(self.branch.protected) 52 self.assertEqual(repr(self.branch), 'Branch(name="topic/RewriteWithGeneratedCode")') 53 54 def testEditProtection(self): 55 self.protected_branch.edit_protection( 56 strict=True, 57 require_code_owner_reviews=True, 58 required_approving_review_count=2, 59 ) 60 branch_protection = self.protected_branch.get_protection() 61 self.assertTrue(branch_protection.required_status_checks.strict) 62 self.assertEqual(branch_protection.required_status_checks.contexts, []) 63 self.assertTrue(branch_protection.enforce_admins) 64 self.assertFalse(branch_protection.required_linear_history) 65 self.assertFalse(branch_protection.required_pull_request_reviews.dismiss_stale_reviews) 66 self.assertTrue(branch_protection.required_pull_request_reviews.require_code_owner_reviews) 67 self.assertEqual( 68 branch_protection.required_pull_request_reviews.required_approving_review_count, 69 2, 70 ) 71 72 def testEditProtectionDismissalUsersWithUserOwnedBranch(self): 73 with self.assertRaises(github.GithubException) as raisedexp: 74 self.protected_branch.edit_protection(dismissal_users=["jacquev6"]) 75 self.assertEqual(raisedexp.exception.status, 422) 76 self.assertEqual( 77 raisedexp.exception.data, 78 { 79 "documentation_url": "https://developer.github.com/v3/repos/branches/#update-branch-protection", 80 "message": "Validation Failed", 81 "errors": ["Only organization repositories can have users and team restrictions"], 82 }, 83 ) 84 85 def testEditProtectionPushRestrictionsWithUserOwnedBranch(self): 86 with self.assertRaises(github.GithubException) as raisedexp: 87 self.protected_branch.edit_protection(user_push_restrictions=["jacquev6"], team_push_restrictions=[]) 88 self.assertEqual(raisedexp.exception.status, 422) 89 self.assertEqual( 90 raisedexp.exception.data, 91 { 92 "documentation_url": "https://developer.github.com/v3/repos/branches/#update-branch-protection", 93 "message": "Validation Failed", 94 "errors": ["Only organization repositories can have users and team restrictions"], 95 }, 96 ) 97 98 def testEditProtectionPushRestrictionsAndDismissalUser(self): 99 self.organization_branch.edit_protection(dismissal_users=["jacquev6"], user_push_restrictions=["jacquev6"]) 100 branch_protection = self.organization_branch.get_protection() 101 self.assertListKeyEqual( 102 branch_protection.required_pull_request_reviews.dismissal_users, 103 lambda u: u.login, 104 ["jacquev6"], 105 ) 106 self.assertListKeyEqual( 107 branch_protection.required_pull_request_reviews.dismissal_teams, 108 lambda u: u.slug, 109 [], 110 ) 111 self.assertListKeyEqual( 112 branch_protection.get_user_push_restrictions(), 113 lambda u: u.login, 114 ["jacquev6"], 115 ) 116 self.assertListKeyEqual(branch_protection.get_team_push_restrictions(), lambda u: u.slug, []) 117 118 def testRemoveProtection(self): 119 self.assertTrue(self.protected_branch.protected) 120 self.protected_branch.remove_protection() 121 protected_branch = self.repo.get_branch("integrations") 122 self.assertFalse(protected_branch.protected) 123 with self.assertRaises(github.GithubException) as raisedexp: 124 protected_branch.get_protection() 125 self.assertEqual(raisedexp.exception.status, 404) 126 self.assertEqual( 127 raisedexp.exception.data, 128 { 129 "documentation_url": "https://developer.github.com/v3/repos/branches/#get-branch-protection", 130 "message": "Branch not protected", 131 }, 132 ) 133 134 def testEditRequiredStatusChecks(self): 135 self.protected_branch.edit_required_status_checks(strict=True) 136 required_status_checks = self.protected_branch.get_required_status_checks() 137 self.assertTrue(required_status_checks.strict) 138 self.assertEqual(required_status_checks.contexts, ["foo/bar"]) 139 140 def testRemoveRequiredStatusChecks(self): 141 self.protected_branch.remove_required_status_checks() 142 with self.assertRaises(github.GithubException) as raisedexp: 143 self.protected_branch.get_required_status_checks() 144 self.assertEqual(raisedexp.exception.status, 404) 145 self.assertEqual( 146 raisedexp.exception.data, 147 { 148 "documentation_url": "https://developer.github.com/v3/repos/branches/#get-required-status-checks-of-protected-branch", 149 "message": "Required status checks not enabled", 150 }, 151 ) 152 153 def testEditRequiredPullRequestReviews(self): 154 self.protected_branch.edit_required_pull_request_reviews( 155 dismiss_stale_reviews=True, required_approving_review_count=2 156 ) 157 required_pull_request_reviews = self.protected_branch.get_required_pull_request_reviews() 158 self.assertTrue(required_pull_request_reviews.dismiss_stale_reviews) 159 self.assertTrue(required_pull_request_reviews.require_code_owner_reviews) 160 self.assertEqual(required_pull_request_reviews.required_approving_review_count, 2) 161 162 def testEditRequiredPullRequestReviewsWithTooLargeApprovingReviewCount(self): 163 with self.assertRaises(github.GithubException) as raisedexp: 164 self.protected_branch.edit_required_pull_request_reviews(required_approving_review_count=9) 165 self.assertEqual(raisedexp.exception.status, 422) 166 self.assertEqual( 167 raisedexp.exception.data, 168 { 169 "documentation_url": "https://developer.github.com/v3/repos/branches/#update-pull-request-review-enforcement-of-protected-branch", 170 "message": "Invalid request.\n\n9 must be less than or equal to 6.", 171 }, 172 ) 173 174 def testEditRequiredPullRequestReviewsWithUserBranchAndDismissalUsers(self): 175 with self.assertRaises(github.GithubException) as raisedexp: 176 self.protected_branch.edit_required_pull_request_reviews(dismissal_users=["jacquev6"]) 177 self.assertEqual(raisedexp.exception.status, 422) 178 self.assertEqual( 179 raisedexp.exception.data, 180 { 181 "documentation_url": "https://developer.github.com/v3/repos/branches/#update-pull-request-review-enforcement-of-protected-branch", 182 "message": "Dismissal restrictions are supported only for repositories owned by an organization.", 183 }, 184 ) 185 186 def testRemoveRequiredPullRequestReviews(self): 187 self.protected_branch.remove_required_pull_request_reviews() 188 required_pull_request_reviews = self.protected_branch.get_required_pull_request_reviews() 189 self.assertFalse(required_pull_request_reviews.dismiss_stale_reviews) 190 self.assertFalse(required_pull_request_reviews.require_code_owner_reviews) 191 self.assertEqual(required_pull_request_reviews.required_approving_review_count, 1) 192 193 def testAdminEnforcement(self): 194 self.protected_branch.remove_admin_enforcement() 195 self.assertFalse(self.protected_branch.get_admin_enforcement()) 196 self.protected_branch.set_admin_enforcement() 197 self.assertTrue(self.protected_branch.get_admin_enforcement()) 198 199 def testAddUserPushRestrictions(self): 200 self.organization_branch.add_user_push_restrictions("sfdye") 201 self.assertListKeyEqual( 202 self.organization_branch.get_user_push_restrictions(), 203 lambda u: u.login, 204 ["jacquev6", "sfdye"], 205 ) 206 207 def testReplaceUserPushRestrictions(self): 208 self.assertListKeyEqual( 209 self.organization_branch.get_user_push_restrictions(), 210 lambda u: u.login, 211 ["jacquev6"], 212 ) 213 self.organization_branch.replace_user_push_restrictions("sfdye") 214 self.assertListKeyEqual( 215 self.organization_branch.get_user_push_restrictions(), 216 lambda u: u.login, 217 ["sfdye"], 218 ) 219 220 def testRemoveUserPushRestrictions(self): 221 self.organization_branch.remove_user_push_restrictions("jacquev6") 222 self.assertListKeyEqual( 223 self.organization_branch.get_user_push_restrictions(), 224 lambda u: u.login, 225 ["sfdye"], 226 ) 227 228 def testAddTeamPushRestrictions(self): 229 self.organization_branch.add_team_push_restrictions("pygithub-owners") 230 self.assertListKeyEqual( 231 self.organization_branch.get_team_push_restrictions(), 232 lambda t: t.slug, 233 ["pygithub-owners"], 234 ) 235 236 def testReplaceTeamPushRestrictions(self): 237 self.assertListKeyEqual( 238 self.organization_branch.get_team_push_restrictions(), 239 lambda t: t.slug, 240 ["pygithub-owners"], 241 ) 242 self.organization_branch.replace_team_push_restrictions("org-team") 243 self.assertListKeyEqual( 244 self.organization_branch.get_team_push_restrictions(), 245 lambda t: t.slug, 246 ["org-team"], 247 ) 248 249 def testRemoveTeamPushRestrictions(self): 250 self.organization_branch.remove_team_push_restrictions("org-team") 251 self.assertListKeyEqual( 252 self.organization_branch.get_team_push_restrictions(), 253 lambda t: t.slug, 254 ["pygithub-owners"], 255 ) 256 257 def testRemovePushRestrictions(self): 258 self.organization_branch.remove_push_restrictions() 259 with self.assertRaises(github.GithubException) as raisedexp: 260 list(self.organization_branch.get_user_push_restrictions()) 261 self.assertEqual(raisedexp.exception.status, 404) 262 self.assertEqual( 263 raisedexp.exception.data, 264 { 265 "documentation_url": "https://developer.github.com/v3/repos/branches/#list-team-restrictions-of-protected-branch", 266 "message": "Push restrictions not enabled", 267 }, 268 ) 269 270 def testGetRequiredSignatures(self): 271 required_signature = self.protected_branch.get_required_signatures() 272 assert required_signature 273 274 def testRemoveRequiredSignatures(self): 275 self.protected_branch.remove_required_signatures() 276 277 def testAddRequiredSignatures(self): 278 self.protected_branch.add_required_signatures()