Skip to content

Instantly share code, notes, and snippets.

@Bowenislandsong
Created January 28, 2019 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bowenislandsong/5a0b121ab9ea242accdec402a5634363 to your computer and use it in GitHub Desktop.
Save Bowenislandsong/5a0b121ab9ea242accdec402a5634363 to your computer and use it in GitHub Desktop.
Creating Repositories inside organization. Add members as assigned. Using PyGithub
# Creating repositories in an organization from a list
# Add people from the list to their repositories
# by Bowen Song
# Input List File Format:
# <repository name> | <Member username/email> | <role>
# ceph-end-to-end-tracing bowenislandsong member
# $ pip install PyGithub
# Create "New personal access token" and set all "repo" and "admin:org" to read and write
# Get Authentication and put below
from github import Github
import csv
# ---------------------------- Input information here and run the script below --------------------------
git = Github('Token')
org = git.get_organization('orgnization')
listpath = "./test.txt"
admins = ["Bowenislandsong"]
# ---------------------------- Now Run the code, remeber the libraries, if we failed to add people, there will be a txt file with their information --------------------------
# Read List file
repo = []
member = []
with open(listpath, 'r') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
if len(row) == 2:
repo.append(row[0])
member.append((row[1],"admin"))
# sort them into a map
sortedlist = {}
failedList = []
for i in range(len(repo)):
if repo[i] not in sortedlist:
sortedlist[repo[i]] = [member[i]]
else:
sortedlist[repo[i]].append(member[i])
# function to see if repo exits
def isRepoExist(organization,repo_name):
try:
organization.get_repo(repo_name)
return True
except:
return False
# function to add member
def addToRepo(myrepo, repo_name, usr, perm):
try:
myrepo.add_to_collaborators(usr,permission=perm)
except:
failedList.append((repo_name,usr,perm))
# create repositories
for repo_name in repo:
if (not isRepoExist(org,repo_name)):
org.create_repo(repo_name)
tmprepo = org.get_repo(repo_name)
for ad in admins:
addToRepo(tmprepo, repo_name, ad,"admin")
for onemember in sortedlist[repo_name]:
addToRepo(tmprepo, repo_name, onemember[0],onemember[1])
# print out the members failed to be added
newf = open("missed people.txt", 'a')
newf.write("# <repository name> | <Member username> | <role>\n")
for person in failedList:
print("we have missed: "+person[0] + " " + person[1] + " " + person[2])
newf.write(person[0] + " " + person[1] + " " + person[2]+"\n")
newf.close()
# <repository name> | <Member username> | <role>
search_ceph bowenislandsong member
i-am-ceph bowenislandsong admin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment