-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_class_repo_wrangler_vs.py
139 lines (118 loc) · 4.38 KB
/
git_class_repo_wrangler_vs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#%%
import pandas as pd
from github import Github
#import shutil
import random
#import time
import re
#%%
with open('.TOKEN','r') as f:
TOKEN = f.read()
#print(TOKEN)
#%%
roster = pd.read_csv('roster.csv')
git_users = roster['git-username'].to_list()
print(roster)
print(git_users)
#%%
# act as me using my personal access token
g = Github(TOKEN)
#see all my repos
for repo in g.get_user().get_repos():
print(repo.name)
# %%
#get a handle to class repo
repo = g.get_repo("quist00/2021-03-23-STROBE-git-class")
print(repo.name)
source_branch = 'main'
target_branch = 'newfeature'
sb = repo.get_branch(source_branch)
repo.create_git_ref(ref='refs/heads/' + target_branch, sha=sb.commit.sha)
print(list(repo.get_branches()))
#%%
#send invites to list of git usernames in roster file
for u in git_users:
repo.add_to_collaborators(u,'push')
# %%
#print out pending invites and collaborators
print("Invites")
for i in repo.get_pending_invitations():
print(i.invitee)
#print out collaborators
print("\nCollaborators")
for c in repo.get_collaborators():
print(c.name)
# %%
#create haiku file for each individual leaner, this will not work if file exists
content = ''
with open('haikus.txt','r') as f:
content = f.read()
for u in git_users:
# source_branch = 'main'
# sb = repo.get_branch(source_branch)
# repo.create_git_ref(ref='refs/heads/' + u, sha=sb.commit.sha)
repo.create_file('solo/{}.txt'.format(u), 'individual file for{}'.format(u), content,branch=u)
#print(list(repo.get_branches()))
#%%
# put each solo file into a conflicting state
conflict_string = "### This is an intentional conflict with your solo file. [KEEP THIS] Resolve by preserving your changes along with anything in brackets on this line.\n"
file_bytes = ''
contents = repo.get_contents("/solo")
for content_file in contents:
# by default file just appears as hashed array I think, so have to call decode
file_bytes= content_file.decoded_content
#regex on file bytes requires match in file bytes. To hard to write so convert to string.
file_as_string = file_bytes.decode('utf-8')
#replace all teh author lines with a stock phrase
updated_contents= re.sub(r'###.*?\n',conflict_string,file_as_string)
#print(updated_contents)
repo.update_file(content_file.path, "force conflict with {}".format(content_file.name), updated_contents, content_file.sha, branch="main")
# %%
#make random teams
# divides a list into n sized chunks
def divide_chunks(l, n):
# looping till length l
for i in range(0, len(l), n):
yield l[i:i + n]
#randomize list
random.shuffle(git_users)
#divide into teams
teams = list(divide_chunks(git_users, 2))
# if learners is odd make last team a team of 3
if len(teams)%2 == 1:
temp = teams.pop()
teams[-1] = teams[-1] + temp
print(teams)
# %%
# make a file for each team based on their usernames
content = ''
with open('haikus.txt','r') as f:
content = f.read()
for t in teams:
member_names = '-'.join(t)
repo.create_file('teams/{}.txt'.format(member_names), 'team file for{}'.format(member_names), content)
#shutil.copy('haikus.txt',('teams/{}.txt'.format('-'.join(t))))
# %%
# put team files into a conflicting state
conflict_string = "### This is an intentional conflict. [KEEP THIS] Resolve by preserving your changes along with anything in brackets on this line.\n"
file_bytes = ''
contents = repo.get_contents("/teams")
for content_file in contents:
# by default file just appears as hashed array I think, so have to call decode
file_bytes= content_file.decoded_content
#regex on file bytes requires match in file bytes. Too hard to write so convert to string.
file_as_string = file_bytes.decode('utf-8')
#replace all the author lines with a stock phrase
updated_contents= re.sub(r'###.*?\n',conflict_string,file_as_string)
#print(updated_contents)
repo.update_file(content_file.path, "force conflict with {}".format(content_file.name), updated_contents, content_file.sha, branch="main")
#%%
# delete all files in solo
contents = repo.get_contents("/solo")
for content_file in contents:
#repo.delete_file(content_file.path, "remove file from solo folder", content_file.sha, branch="main")
#%%
# delete all files in teams
contents = repo.get_contents("/teams")
for content_file in contents:
#repo.delete_file(content_file.path, "remove file from teams folder", content_file.sha, branch="main")