Skip to content

Commit 4d9a426

Browse files
eric-lyonsjoeldodge79drstrangelooker
committed
build: Update production git branches to main programmatically (#969)
* Create simple_schedule_plan.py Many people have come on chat about creating schedules with the sdk and I just wanted to create a barebones example, so we can direct them to a public example. * Update README.md Updated readme to point at simple_schedule_plan example * Update README.md Update readme to link to new example of adding users to a group from a CSV. * Adding a script to programmatically update Looker projects to use main as their default branch Co-authored-by: Joel Dodge <[email protected]> Co-authored-by: Dr. Strangelove <[email protected]>
1 parent 483ab43 commit 4d9a426

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import looker_sdk
2+
import logging
3+
import argparse
4+
from looker_sdk import models
5+
import pandas as pd
6+
7+
8+
'''
9+
Background: https://github.com/github/renaming
10+
Github has changed their default name for production branches from master to main.
11+
While master will still work to have a cohesive and consistent naming pattern this script
12+
will update existing Looker Projects' production branch to main. Running this with the default
13+
args will create a list of projects that do not use main as the production branch name.
14+
With the --update flag, you can programmatically change these to main.
15+
With the --omit flag, you can avoid making these changes on specific projects.
16+
'''
17+
18+
19+
def initialize_sdk():
20+
'''
21+
This assumes there is a looker.ini file in the same directory as the script.
22+
If in other directory, please specify the path
23+
'''
24+
sdk = looker_sdk.init40()
25+
return sdk
26+
27+
28+
def get_all_prod_branches(sdk, omit):
29+
'''
30+
This function loops through all projects in Looker.
31+
It checks if the project does not use main as the production branch.
32+
This also removes any bare repos from the list since they do not use git.
33+
The result of this is a tuple of project names, branches, and remote repos.
34+
It also writes a csv of the projects, remote urls and production branch names
35+
that do not use main and are not bare repos.
36+
ARGS: omit, sdk
37+
SDK is the result of initializing the python sdk.
38+
Omit is the result of the --omit flag, It accepts a comma separate listed of
39+
project names and will not update them.
40+
'''
41+
omit = omit
42+
# nonetype handling
43+
if omit:
44+
omit = omit
45+
else:
46+
omit = []
47+
target_projects = []
48+
target_branches = []
49+
safe_list = []
50+
bare_repos = []
51+
remotes = []
52+
projects = sdk.all_projects()
53+
string = "bare"
54+
for project in projects:
55+
if project.name in omit:
56+
safe_list.append(project.name)
57+
else:
58+
if project.git_production_branch_name != "main":
59+
url = project.git_remote_url
60+
if string in str(url):
61+
logging.info(
62+
project.name + " uses a bare repo and was removed from the list."
63+
)
64+
bare_repos.append(url)
65+
else:
66+
target_projects.append(project.name)
67+
target_branches.append(project.git_production_branch_name)
68+
remotes.append(project.git_remote_url)
69+
70+
df = pd.DataFrame(list(zip(target_projects, target_branches, remotes)), columns=["Projects", "Branches", "Remotes"])
71+
df.to_csv('git_branches.csv')
72+
return target_projects, target_branches, remotes
73+
74+
75+
def update_prod_branch(update, branches, sdk):
76+
'''
77+
This function takes the projects that are not set to use main as the production
78+
branch and update them to use main.
79+
ARGS: update, branches, sdk
80+
Update is from the --update flag. If not used this script just lists the projects
81+
that do not use main.
82+
If update is used, it will change the production branch to main if not outlined
83+
on the omit list.
84+
sdk is the result of initializing the python sdk.
85+
The args for this function are passed from the result of get_all_prod_branches.
86+
'''
87+
if update:
88+
target_projects = branches[0]
89+
for p in target_projects:
90+
sdk.update_project(project_id=p, body=models.WriteProject(git_production_branch_name="main"))
91+
logging.info(p + " was updated to main")
92+
93+
94+
95+
def main():
96+
'''
97+
This calls get prod branches and passes the result of that into update_prod_branches.
98+
This also logs the changes to an out file in the local directory called update_git_branch_script.log
99+
ARGS: None
100+
'''
101+
parser = argparse.ArgumentParser(
102+
description='Identify projects that do not use main as their production branch and update these')
103+
parser.add_argument('--update', '-u', action="store_true",
104+
help='Update branches to all use main')
105+
parser.add_argument('--omit', '-o', type=str,
106+
help='List of projects we want to omit')
107+
args = parser.parse_args()
108+
update = args.update
109+
omit = args.omit
110+
logging.basicConfig(filename='update_git_branch_script.log', level=logging.INFO)
111+
112+
sdk = initialize_sdk()
113+
branches = get_all_prod_branches(sdk, omit)
114+
update_prod_branch(update, branches, sdk)
115+
if __name__ == "__main__":
116+
main()

0 commit comments

Comments
 (0)