-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (107 loc) · 5.52 KB
/
Copy pathmain.py
File metadata and controls
145 lines (107 loc) · 5.52 KB
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
140
141
142
143
144
145
# imports
import random
import os
import subprocess
import time
# Constants
TEXT_FILE_PATH = "text.txt"
COAUTHOR_1 = "Co-authored-by: first-co-author-name <first-co-author-name@example.com>"
COAUTHOR_2 = "Co-authored-by: second-co-author-name <second-co-author-name@example.com>"
COMMIT_MESSAGE = f"""
Added a small change to {TEXT_FILE_PATH}
{COAUTHOR_1}
{COAUTHOR_2}"""
# First Make a new branch with a meaningful name that dont looks like its a random branch
def gen_branch_name():
# get a dictionary of words
word_1 = ["space", "moon", "star", "planet", "galaxy", "universe", "comet", "asteroid", "blackhole", "wormhole", "nebula", "quasar", "pulsar", "supernova", "darkmatter", "lightyear", "gravity", "orbit", "cosmos", "void", "dimension", "time", "energy", "matter", "radiation", "singularity", "eventhorizon", "spacetime", "multiverse", "paralleluniverse", "extraterrestrial", "alien", "lifeform", "intelligence", "civilization", "technology", "robotics", "artificialintelligence", "machinelearning", "neuralnetwork", "quantumcomputing"]
word_2 = ["exploration", "discovery", "adventure", "journey", "mission", "expedition", "voyage", "quest", "odyssey", "pilgrimage", "safari", "crusade", "trek", "sojourn", "wanderlust", "roaming", "traveling", "wandering", "drifting", "floating", "gliding", "soaring", "flying", "hovering", "levitating", "ascending", "descending", "climbing", "scaling", "mountaineering"]
word_3 = ["explorer", "adventurer", "traveler", "wanderer", "nomad", "pilgrim", "journeyman", "wayfarer", "roamer", "drifter", "globetrotter", "voyager", "pioneer", "trailblazer", "pathfinder", "navigator", "scout", "seeker", "quester", "expeditionist", "explorator", "discoverer", "investigator", "researcher", "scientist", "scholar", "academic", "intellectual", "thinker", "philosopher", "theorist", "visionary", "futurist", "innovator", "inventor", "creator", "artist", "designer", "engineer"]
# generate a random number of 4 digits
suffix = random.randint(1000, 9999)
# generate a random word with the first, secon, and third word and the suffix
branch_name = f"{random.choice(word_1)}-{random.choice(word_2)}-{random.choice(word_3)}-{suffix}"
return str(branch_name)
# Now create a branch with that name
def create_branch(branch_name):
# check if the branch already exists
branches = subprocess.check_output(["git", "branch"]).decode("utf-8").split("\n")
if branch_name in branches:
print(f"Branch {branch_name} already exists")
return False
# create the branch
subprocess.call(["git", "checkout", "-b", branch_name])
print(f"Branch {branch_name} created")
return True
# create a function to switch to a specific branch
def switch_branch(branch_name):
# check if the branch exists
branches = subprocess.check_output(["git", "branch"]).decode("utf-8").split("\n")
if branch_name not in branches:
print(f"Branch {branch_name} does not exist")
return False
# switch to the branch
subprocess.call(["git", "checkout", branch_name])
print(f"Switched to branch {branch_name}")
return True
# a function to run a command in the terminal and also print the output
def run_command(command):
# run the command
result = subprocess.run(command, shell=True, capture_output=True, text=True)
# print the output
print(result.stdout)
# check if there was an error
if result.returncode != 0:
print(f"Error: {result.stderr}")
return False
return True
# create a function to create a file with the name text.txt
def create_file(file_path):
# check if the file already exists
if os.path.exists(file_path):
print(f"File {file_path} already exists")
return False
# create the file
with open(file_path, "w") as f:
f.write("This is a test file")
print(f"File {file_path} created")
return True
# make a function to add a small change to the text file
def add_change_to_text_file(file_path):
# check if the file exists
create_file(file_path)
# add a small change to the file
with open(file_path, "a") as f:
f.write("\nThis is a small change")
print(f"Small change added to {file_path}")
return True
def main():
# generate a branch name
branch_name = gen_branch_name()
# create the branch
create_branch(branch_name)
# add a small change to the text file
add_change_to_text_file(TEXT_FILE_PATH)
# add the file to git
run_command("git add .")
# commit the changes with a message
subprocess.run(["git", "commit", "-m", COMMIT_MESSAGE], check=True)
# push the changes to the remote repository
run_command(f"git push origin {branch_name}")
# create a pull request with the changes to the new branch and the main branch
run_command(f'gh pr create --base main --head {branch_name} --fill')
# merge the pull request
run_command(f'gh pr merge {branch_name} --squash')
run_command("git checkout main")
if __name__ == "__main__":
loopturns = int(input("How many times do you want to run the script? "))
# time delay to prevent rate limiting max 100 seconds
time_delay = int(input("How long do you want to wait between runs? (in seconds) "))
if time_delay < 100:
time_delay = 100
for i in range(loopturns):
main()
print(f"Finished run {i+1} of {loopturns}")
if i < loopturns - 1:
print(f"Waiting {time_delay} seconds before next run")
time.sleep(time_delay)