Environment.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2022 Alson van der Meulen <alson.vandermeulen@dearhealth.com> # 4 # # 5 # This file is part of PyGithub. # 6 # http://pygithub.readthedocs.io/ # 7 # # 8 # PyGithub is free software: you can redistribute it and/or modify it under # 9 # the terms of the GNU Lesser General Public License as published by the Free # 10 # Software Foundation, either version 3 of the License, or (at your option) # 11 # any later version. # 12 # # 13 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 15 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 16 # details. # 17 # # 18 # You should have received a copy of the GNU Lesser General Public License # 19 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 20 # # 21 ################################################################################ 22 23 from datetime import datetime, timezone 24 25 import pytest # type: ignore 26 27 import github 28 import github.EnvironmentDeploymentBranchPolicy 29 import github.EnvironmentProtectionRule 30 import github.EnvironmentProtectionRuleReviewer 31 import github.NamedUser 32 import github.Team 33 34 from . import Framework 35 36 37 class Environment(Framework.TestCase): 38 def setUp(self): 39 self.tokenAuthMode = True 40 super().setUp() 41 self.repo = self.g.get_user().get_repo("PyGithub") 42 self.environment = self.repo.get_environment("dev") 43 44 def testAttributes(self): 45 self.assertEqual(self.environment.name, "dev") 46 self.assertEqual(self.environment.id, 464814513) 47 self.assertEqual(self.environment.node_id, "EN_kwDOHKhL9c4btIGx") 48 self.assertEqual( 49 self.environment.url, 50 "https://api.github.com/repos/alson/PyGithub/environments/dev", 51 ) 52 self.assertEqual( 53 self.environment.html_url, 54 "https://github.com/alson/PyGithub/deployments/activity_log?environments_filter=dev", 55 ) 56 self.assertEqual( 57 self.environment.created_at, 58 datetime(2022, 4, 13, 15, 6, 32, tzinfo=timezone.utc), 59 ) 60 self.assertEqual( 61 self.environment.updated_at, 62 datetime(2022, 4, 13, 15, 6, 32, tzinfo=timezone.utc), 63 ) 64 self.assertTrue(self.environment.deployment_branch_policy.protected_branches) 65 self.assertFalse(self.environment.deployment_branch_policy.custom_branch_policies) 66 67 def testProtectionRules(self): 68 protection_rules = self.environment.protection_rules 69 self.assertEqual(len(protection_rules), 3) 70 self.assertEqual(protection_rules[0].id, 216323) 71 self.assertEqual(protection_rules[0].node_id, "GA_kwDOHKhL9c4AA00D") 72 self.assertEqual(protection_rules[0].type, "branch_policy") 73 self.assertEqual(protection_rules[1].id, 216324) 74 self.assertEqual(protection_rules[1].node_id, "GA_kwDOHKhL9c4AA00E") 75 self.assertEqual(protection_rules[1].type, "required_reviewers") 76 self.assertEqual(protection_rules[2].id, 216325) 77 self.assertEqual(protection_rules[2].node_id, "GA_kwDOHKhL9c4AA00F") 78 self.assertEqual(protection_rules[2].type, "wait_timer") 79 self.assertEqual(protection_rules[2].wait_timer, 15) 80 81 def testReviewers(self): 82 # This is necessary so we can maintain our own expectations, which have been manually editted, for this test. 83 reviewers = self.repo.get_environment("dev").protection_rules[1].reviewers 84 self.assertEqual(len(reviewers), 2) 85 self.assertEqual(reviewers[0].type, "User") 86 self.assertIsInstance(reviewers[0].reviewer, github.NamedUser.NamedUser) 87 assert isinstance(reviewers[0].reviewer, github.NamedUser.NamedUser) # Make type checker happy 88 self.assertEqual(reviewers[0].reviewer.id, 19245) 89 self.assertEqual(reviewers[0].reviewer.login, "alson") 90 self.assertEqual(reviewers[0].reviewer.type, "User") 91 self.assertEqual(reviewers[1].type, "Team") 92 self.assertIsInstance(reviewers[1].reviewer, github.Team.Team) 93 assert isinstance(reviewers[1].reviewer, github.Team.Team) # Make type checker happy 94 self.assertEqual(reviewers[1].reviewer.id, 1) 95 self.assertEqual(reviewers[1].reviewer.slug, "justice-league") 96 self.assertEqual(reviewers[1].reviewer.url, "https://api.github.com/teams/1") 97 98 def testGetEnvironments(self): 99 environments = self.repo.get_environments() 100 self.assertEqual(environments.totalCount, 1) 101 self.assertEqual( 102 environments[0].url, 103 "https://api.github.com/repos/alson/PyGithub/environments/dev", 104 ) 105 self.assertEqual(environments[0].name, "dev") 106 107 def testCreateEnvironment(self): 108 environment = self.repo.create_environment("test") 109 self.assertEqual(environment.name, "test") 110 self.assertEqual(environment.id, 470015651) 111 self.assertEqual(environment.node_id, "EN_kwDOHKhL9c4cA96j") 112 self.assertEqual( 113 environment.url, 114 "https://api.github.com/repos/alson/PyGithub/environments/test", 115 ) 116 self.assertEqual( 117 environment.html_url, 118 "https://github.com/alson/PyGithub/deployments/activity_log?environments_filter=test", 119 ) 120 self.assertEqual( 121 environment.created_at, 122 datetime(2022, 4, 19, 14, 4, 32, tzinfo=timezone.utc), 123 ) 124 self.assertEqual( 125 environment.updated_at, 126 datetime(2022, 4, 19, 14, 4, 32, tzinfo=timezone.utc), 127 ) 128 self.assertEqual(len(environment.protection_rules), 0) 129 self.assertIsNone(environment.deployment_branch_policy) 130 131 def testUpdateEnvironment(self): 132 environment = self.repo.create_environment( 133 "test", 134 wait_timer=42, 135 reviewers=[github.EnvironmentProtectionRuleReviewer.ReviewerParams(type_="User", id_=19245)], 136 deployment_branch_policy=github.EnvironmentDeploymentBranchPolicy.EnvironmentDeploymentBranchPolicyParams( 137 protected_branches=True, custom_branch_policies=False 138 ), 139 ) 140 self.assertEqual(environment.name, "test") 141 self.assertEqual(environment.id, 470015651) 142 self.assertEqual(environment.node_id, "EN_kwDOHKhL9c4cA96j") 143 self.assertEqual( 144 environment.url, 145 "https://api.github.com/repos/alson/PyGithub/environments/test", 146 ) 147 self.assertEqual( 148 environment.html_url, 149 "https://github.com/alson/PyGithub/deployments/activity_log?environments_filter=test", 150 ) 151 self.assertEqual( 152 environment.created_at, 153 datetime(2022, 4, 19, 14, 4, 32, tzinfo=timezone.utc), 154 ) 155 self.assertEqual( 156 environment.updated_at, 157 datetime(2022, 4, 19, 14, 4, 32, tzinfo=timezone.utc), 158 ) 159 self.assertEqual(len(environment.protection_rules), 3) 160 self.assertEqual(environment.protection_rules[0].type, "required_reviewers") 161 self.assertEqual(len(environment.protection_rules[0].reviewers), 1) 162 self.assertEqual(environment.protection_rules[0].reviewers[0].type, "User") 163 self.assertEqual(environment.protection_rules[0].reviewers[0].reviewer.id, 19245) 164 self.assertEqual(environment.protection_rules[1].type, "wait_timer") 165 self.assertEqual(environment.protection_rules[1].wait_timer, 42) 166 self.assertEqual(environment.protection_rules[2].type, "branch_policy") 167 self.assertTrue(environment.deployment_branch_policy.protected_branches) 168 self.assertFalse(environment.deployment_branch_policy.custom_branch_policies) 169 170 def testDeleteEnvironment(self): 171 self.repo.delete_environment("test") 172 with pytest.raises(github.UnknownObjectException): 173 self.repo.get_environment("test")