├── .gitignore ├── .github ├── CODEOWNERS └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── requirements.txt ├── team_permissions.csv ├── modules ├── title.py ├── createRepo.py ├── teamAccess.py ├── createProject.py └── initialise.py ├── LICENSE ├── git_autocreate.py ├── README.md └── CODE_OF_CONDUCT.md /.gitignore: -------------------------------------------------------------------------------- 1 | modules/__pycache__/** 2 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @vaishnav-canarys @grenstong 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | stdiomask 2 | requests 3 | termcolor -------------------------------------------------------------------------------- /team_permissions.csv: -------------------------------------------------------------------------------- 1 | Team,Permissions 2 | team_1,push 3 | test_team,admin -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Expected behavior** 14 | A clear and concise description of what you expected to happen. 15 | 16 | **Screenshots** 17 | If applicable, add screenshots to help explain your problem. 18 | 19 | **Desktop (please complete the following information):** 20 | - OS: [e.g. Windows,Linux] 21 | - Powershell Version [e.g. 4..0] 22 | 23 | **Additional context** 24 | Add any other context about the problem here. 25 | -------------------------------------------------------------------------------- /modules/title.py: -------------------------------------------------------------------------------- 1 | def showTitle(): 2 | print(" _____ _ _ _ _ _ ___ _ _____ _ " ) 3 | print(" | __ (_) | | | | | | | / _ \ | | / __ \ | | ") 4 | print(" | | \/_| |_| |_| |_ _| |__ ______/ /_\ \_ _| |_ ___ ______| / \/_ __ ___ __ _| |_ ___") 5 | print(" | | __| | __| _ | | | | '_ \______| _ | | | | __/ _ \______| | | '__/ _ \/ _` | __/ _ \\") 6 | print(" | |_\ \ | |_| | | | |_| | |_) | | | | | |_| | || (_) | | \__/\ | | __/ (_| | || __/") 7 | print(" \____/_|\__\_| |_/\__,_|_.__/ \_| |_/\__,_|\__\___/ \____/_| \___|\__,_|\__\___|") 8 | print() -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: feature 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /modules/createRepo.py: -------------------------------------------------------------------------------- 1 | from termcolor import colored 2 | from requests import post 3 | from json import dumps 4 | def createRepo(inputObject, head, type): 5 | RepoDetails = { 6 | 'name':inputObject.RepoName, 7 | 'description':inputObject.RepoDescription, 8 | 'visibility':type 9 | } 10 | 11 | print() 12 | print( "Create repository in organization") 13 | print( "=================================") 14 | 15 | create_repo_req_url = 'https://api.github.com/orgs/' + inputObject.Organization + '/repos' 16 | try: 17 | create_repo_response = post(url=create_repo_req_url, headers=head, data=dumps(RepoDetails)) 18 | repository_name = create_repo_response.json()['name'] 19 | print() 20 | print(repository_name,'repository is created') 21 | 22 | except: 23 | print(colored(create_repo_response.json(),'red')) -------------------------------------------------------------------------------- /modules/teamAccess.py: -------------------------------------------------------------------------------- 1 | from requests import put 2 | from termcolor import colored 3 | from json import dumps 4 | def provideTeamAccess(inputObject, head, csvReader): 5 | print() 6 | print( "Give repository access to a team") 7 | print( "=================================") 8 | try: 9 | for r in csvReader: 10 | teamname = r['Team'].replace(' ','-') 11 | repopermission = r['Permissions'] 12 | repobody = { 13 | 'permission' : repopermission 14 | } 15 | give_team_access_req_url = 'https://api.github.com/orgs/' + inputObject.Organization + '/teams/' + teamname.lower() + '/repos/' + inputObject.Organization + '/' + inputObject.RepoName 16 | put(give_team_access_req_url, headers=head, data=dumps(repobody)) 17 | team_newname = teamname.upper() 18 | repo_newname = inputObject.RepoName.upper() 19 | print('Team',team_newname,'has been given',repopermission,'to',repo_newname) 20 | except: 21 | print(colored("Unable to provide access for {}".format(teamname.upper()),'red')) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 CanarysAutomations 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /git_autocreate.py: -------------------------------------------------------------------------------- 1 | ''' 2 | The tool will communicate with the repositories and organizations of GitHub. 3 | This tool is intended to restrict manual labour as much as possible. 4 | The tool with the help of few inputs will: 5 | 1. Create a repository - An empty repository will be created 6 | 2. Repository Access - The teams will be given access to the created repository based on the csv file input 7 | 3. Project - An empty project will be created for the repository with the Columns defined by the user. 8 | ''' 9 | 10 | from modules.title import showTitle 11 | from modules.initialise import initialiseInputs 12 | from modules.createRepo import createRepo 13 | from modules.teamAccess import provideTeamAccess 14 | from modules.createProject import createProject 15 | from sys import argv 16 | 17 | argumentList = argv[1:] 18 | initialisedValues = initialiseInputs(argumentList) 19 | inputObject, head, csvReader, type = initialisedValues['inputObject'], initialisedValues['head'], initialisedValues['csvReader'], initialisedValues['type'] 20 | 21 | showTitle() 22 | createRepo(inputObject, head, type) 23 | provideTeamAccess(inputObject, head, csvReader) 24 | if not inputObject.ProjectName or not inputObject.Columns: 25 | print("\nProject Name or Columns not Specified. Skipping Project Creation\n") 26 | else: 27 | createProject(inputObject, head) 28 | -------------------------------------------------------------------------------- /modules/createProject.py: -------------------------------------------------------------------------------- 1 | from json import dumps 2 | from requests import post 3 | from termcolor import colored 4 | 5 | def createProject(inputObject, head): 6 | projectdetails = { 7 | 'name' : inputObject.ProjectName, 8 | 'body': 'created for repo ' + inputObject.RepoName 9 | } 10 | 11 | try: 12 | print() 13 | print("Create a project for the repository") 14 | print("===================================") 15 | create_project_req_url = 'https://api.github.com/repos/' + inputObject.Organization + '/' + inputObject.RepoName + '/projects' 16 | create_project_response = post(url=create_project_req_url, headers=head, data=dumps(projectdetails)) 17 | print(inputObject.ProjectName,"Project is Created") 18 | except: 19 | print(colored(create_project_response.json(),'red')) 20 | else: 21 | ColumnNames = inputObject.Columns.split(',') 22 | projectID = create_project_response.json()['id'] 23 | create_columns_req_url = 'https://api.github.com/projects/' + str(projectID) + '/columns' 24 | try: 25 | for c in ColumnNames: 26 | ColumnParams = { 'name' : c } 27 | create_columns_response = post(url=create_columns_req_url, headers=head, data=dumps(ColumnParams)) 28 | print("Project Columns are added\n") 29 | except: 30 | print(colored(create_columns_response.json(),'red')) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Autoprovision ⚙ GitHub Resources 2 | 3 | The tool will communicate with the repositories and organizations apis of GitHub. This tool is intended to restrict manual labour as much as possible. The tool with the help of few inputs will 4 | 5 | - **Create a repository**- An empty repository will be created 6 | 7 | - **Repository Access**- The teams will be given access to the created repository based on the excel file input 8 | 9 | - **Project**- A project will be created for the repository with the specified column names. Project Input is not Mandatory 10 | 11 | ## Benefits 12 | 13 | - This tool can be used as a template when repositories have to be created for an organization 14 | - Automate the creation of repositories with very few inputs 15 | 16 | ## GitHub REST API 17 | 18 | The endpoints used in the tool are GitHub's Rest API v3. A series of endpoints are made available by GitHub to alter resources like repositories, teams, projects and make organization level changes. For further reading on the GitHub's REST API please [click here](https://docs.github.com/en/free-pro-team@latest/rest/overview) 19 | 20 | ## Prerequisites 21 | 22 | - Only current organisational teams will be considered 23 | - Admin access to the organization for creating the repositories 24 | - Excel File with Team Names and permissions 25 | - GitHub PAT Token must be authorized to access the required organization 26 | - python 3.9 must be installed 27 | 28 | ## Usage Instructions :memo: 29 | 30 | For instructions on how to use the tool, please [click here](https://github.com/CanarysAutomations/autocreate-github-resources/wiki) 31 | 32 | ## Current Tool Limitations :x: :x: 33 | 34 | - In the future, GitHub could change its remaining endpoints used in the tool without warning 35 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at devopstools@ecanarys.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /modules/initialise.py: -------------------------------------------------------------------------------- 1 | from sys import exit 2 | from getopt import getopt 3 | from stdiomask import getpass 4 | from csv import DictReader 5 | 6 | class inputClass: 7 | def __init__ (self, argumentList=None): 8 | if not argumentList: 9 | self.PATToken = getpass('GitHub Token: ') 10 | self.Organization = input('GitHub Organization: ') 11 | self.RepoName = input('Repository Name: ') 12 | self.Repository_Visibility = input('Repository Visibility: ') 13 | self.RepoDescription = input('Add the Repository Description: ') 14 | self.ProjectName = input('Project to be created for the Repository. Press ENTER if not required: ') 15 | if self.ProjectName != "": 16 | self.Columns = input('Project Column Names to be Created: ') 17 | else: 18 | self.Columns = "" 19 | self.CsvSource = input('CSV Source (Use / for path separator): ') 20 | else: 21 | if len(argumentList) == 16: 22 | options = "t:o:r:v:d:p:c:f:" 23 | long_options = ["Token =", "Organization =","Repository =", "Visibility =","Description =","Project =","Columns =","File ="] 24 | arguments, values = getopt(argumentList, options, long_options) 25 | for currentArgument, currentValue in arguments: 26 | if currentArgument in ("-p", "--Project "): 27 | self.ProjectName = currentValue 28 | elif currentArgument in ("-c", "--Columns "): 29 | self.Columns = currentValue 30 | else: 31 | options = "t:o:r:v:d:f:" 32 | long_options = ["Token =", "Organization =","Repository =", "Visibility =","Description =","File ="] 33 | self.Columns = None 34 | self.ProjectName = None 35 | arguments, values = getopt(argumentList, options, long_options) 36 | for currentArgument, currentValue in arguments: 37 | if currentArgument in ("-t", "--Token "): 38 | self.PATToken = currentValue 39 | elif currentArgument in ("-o", "--Organization "): 40 | self.Organization = currentValue 41 | elif currentArgument in ("-r", "--Repository "): 42 | self.RepoName = currentValue 43 | elif currentArgument in ("-v", "--Visibility "): 44 | self.Repository_Visibility = currentValue 45 | elif currentArgument in ("-d", "--Description "): 46 | self.RepoDescription = currentValue 47 | elif currentArgument in ("-f", "--File "): 48 | self.CsvSource = currentValue 49 | 50 | 51 | def initialiseInputs(argumentList): 52 | if len(argumentList) == 0: 53 | inputObject = inputClass() 54 | elif len(argumentList) != 16 and len(argumentList) != 12: 55 | print("Refer README for unattended usage. Supports only 6 or 8 arguments.") 56 | choice = input("Enter \"Y\" to input values manually: ") 57 | if (choice.upper() == "Y"): 58 | inputObject = inputClass() 59 | else: 60 | exit("Aborting.......") 61 | elif len(argumentList) == 16: 62 | if set(["-t","-o","-r","-v","-d","-p","-c","-f"]) <= set(argumentList) or set(["--Token", "--Organization","--Repository", "--Visibility","--Description","--Project","--Columns","--File"]) <= set(argumentList): 63 | inputObject = inputClass(argumentList) 64 | else: 65 | print("Refer README for unattended usage. Proper arguments not provided.") 66 | choice = input("Enter \"Y\" to input values manually: ") 67 | if (choice.upper() == "Y"): 68 | inputObject = inputClass() 69 | else: 70 | exit("Aborting.......") 71 | else: 72 | if set(['-t','-o','-r','-v','-d','-f']) <= set(argumentList) or set(["--Token", "--Organization","--Repository", "--Visibility","--Description","--File"]) <= set(argumentList): 73 | inputObject = inputClass(argumentList) 74 | else: 75 | print(argumentList) 76 | print("Refer README for unattended usage. Proper arguments not provided.") 77 | choice = input("Enter \"Y\" to input values manually: ") 78 | if (choice.upper() == "Y"): 79 | inputObject = inputClass() 80 | else: 81 | exit("Aborting.......") 82 | 83 | csvReader = DictReader(open(inputObject.CsvSource)) 84 | if inputObject.Repository_Visibility == 'public': 85 | type = 'public' 86 | elif inputObject.Repository_Visibility == 'internal': 87 | type = 'internal' 88 | else: 89 | type='private' 90 | 91 | head = { 92 | 'Authorization' : 'Bearer ' + inputObject.PATToken, 93 | 'Accept' : 'application/vnd.github.v3+json;application/vnd.github.nebula-preview+json;application/vnd.github.inertia-preview+json' 94 | } 95 | returnValues = {'inputObject':inputObject,'head':head,'csvReader':csvReader,'type':type} 96 | return returnValues --------------------------------------------------------------------------------