-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-site.py
More file actions
57 lines (46 loc) · 2.02 KB
/
update-site.py
File metadata and controls
57 lines (46 loc) · 2.02 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
import os
import re
import subprocess
from github import Github
# Get the GitHub token from the environment
token = os.getenv('GH_TOKEN')
# Initialize the GitHub client
g = Github(token)
# The repository where the config.yml needs to be updated
repo = g.get_repo("data-8/su24")
# Get the latest commit in the current branch
output = subprocess.check_output(["git", "diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"])
changed_files = output.decode("utf-8").splitlines()
# Define the base URL
base_url = "https://data8.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fdata-8%2Fmaterials-su24&branch=main&urlpath=tree%2Fmaterials-su24%2F{parent_directory}%2F{sub_directory}%2F{sub_directory}.ipynb"
# Find new directories
new_dirs = []
for file in changed_files:
parts = file.split('/')
if len(parts) == 2 and parts[0] in ["hw", "lab", "proj"]:
new_dirs.append((parts[0], parts[1]))
if not new_dirs:
print("No new directories found.")
exit(0)
# Clone the su24 repository
subprocess.run(["git", "clone", f"https://{token}@github.com/data-8/su24.git"])
os.chdir("su24")
# Read the current config.yml file
with open("config.yml", "r") as f:
lines = f.readlines()
# Update the config.yml file
for parent_directory, sub_directory in new_dirs:
new_link = base_url.format(parent_directory=parent_directory, sub_directory=sub_directory)
for i, line in enumerate(lines):
if line.startswith(f"{sub_directory}:"):
name = line.split(":")[1].strip()
lines[i] = f"{sub_directory}: [{name}]({new_link})\n"
# Write the changes to the config.yml file
with open("config.yml", "w") as f:
f.writelines(lines)
# Commit and push the changes
subprocess.run(["git", "config", "user.name", "github-actions[bot]"])
subprocess.run(["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"])
subprocess.run(["git", "add", "config.yml"])
subprocess.run(["git", "commit", "-m", "Update config.yml with new directories"])
subprocess.run(["git", "push"])