Created
November 3, 2022 22:05
-
-
Save walkerk1980/8a6f6879b32260360854a89bb880a48d to your computer and use it in GitHub Desktop.
Script to create a github environment from bash shell with an allowed branch pattern - must be logged into gh cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import select | |
| import sys | |
| import subprocess | |
| import json | |
| class GitHubEnvironments: | |
| def __init__(self) -> None: | |
| pass | |
| def __we_dont_like_spaces(self, environment_name: str) -> None: | |
| self.environment_name = environment_name | |
| if ' ' in self.environment_name: | |
| raise Exception('This class does not support environment names with spaces') | |
| def __create_environment(self, environment_name: str) -> str: | |
| self.environment_name = environment_name | |
| create_environment_json_input = json.dumps({ | |
| 'deployment_branch_policy': { | |
| 'protected_branches': False, | |
| 'custom_branch_policies' : True, | |
| } | |
| }) | |
| create_environment_command = f'jq -n "{create_environment_json_input}"|gh api \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -X PUT /repos/:owner/:repo/environments/"{self.environment_name}" \ | |
| --input -' | |
| # run cmd | |
| create_environment_response = subprocess.Popen( | |
| create_environment_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
| shell=True) | |
| stdout, stderr = create_environment_response.communicate() | |
| # Throw exception if there is an error | |
| if stderr: | |
| raise RuntimeError(stderr) | |
| # Wait for the process to terminate | |
| while create_environment_response.poll() is None: | |
| select([], [], [], 0.5) | |
| return stdout | |
| def set_branch_policy(self, environment_name: str, allowed_branch_pattern: str) -> str: | |
| self.environment_name = environment_name | |
| self.allowed_branch_pattern = allowed_branch_pattern | |
| set_branch_policy_command = f'gh api \ | |
| --method POST \ | |
| -H "Accept: application/vnd.github+json" \ | |
| /repos/:owner/:repo/environments/{self.environment_name}/deployment-branch-policies \ | |
| -f name="{self.allowed_branch_pattern}"' | |
| # run cmd | |
| set_branch_policy_response = subprocess.Popen( | |
| set_branch_policy_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
| shell=True) | |
| stdout, stderr = set_branch_policy_response.communicate() | |
| # Throw exception if there is an error | |
| if stderr: | |
| raise RuntimeError(stderr) | |
| # Wait for the process to terminate | |
| while set_branch_policy_response.poll() is None: | |
| select([], [], [], 0.5) | |
| return stdout | |
| def new(self, environment_name: str, allowed_branch_pattern: str) -> None: | |
| self.environment_name = environment_name | |
| self.allowed_branch_pattern = allowed_branch_pattern | |
| self.__we_dont_like_spaces(self.environment_name) | |
| self.__create_environment(self.environment_name) | |
| self.set_branch_policy(self.environment_name, self.allowed_branch_pattern) | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 2: | |
| env_name = sys.argv[1] | |
| allowed_branch_pattern = sys.argv[2] | |
| else: | |
| print('Usage: ./GitHubEnvironments.py <environment_name> <allowed_branch_pattern>') | |
| print('Note: you may need to qoute the allowed_branch_pattern if it contains wildcards') | |
| sys.exit(1) | |
| gh_environments = GitHubEnvironments() | |
| print(f'Creating environment {env_name}') | |
| gh_environments.new( | |
| environment_name=env_name, | |
| allowed_branch_pattern=allowed_branch_pattern) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment