Skip to content

Commit

Permalink
update automations
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanferrari committed Jun 19, 2024
1 parent a32ba1a commit 2bb7037
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
56 changes: 4 additions & 52 deletions .github/workflows/update-lecture.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,14 @@ jobs:
with:
python-version: '3.8'

- name: Parse issue content
id: parse_issue
env:
ISSUE_BODY: ${{ github.event.issue.body }}
run: python parse_issue.py

- name: Update _config.yml
run: |
LEC_NUM=$(echo "${{ env.Lecture_Number }}" | xargs)
DEMO=$(echo "${{ env.Demo }}" | xargs)
SLIDES=$(echo "${{ env.Slides }}" | xargs)
CONFIG_FILE="_config.yml"
SLIDE_LINE=" slide${LEC_NUM}: \"Slides\""
DEMO_LINE=" demo${LEC_NUM}: \"• Demo\""
if [ "$DEMO" == "None" ]; then
DEMO_REPLACEMENT=" demo${LEC_NUM}: \"\""
else
DEMO_LINK=$(echo "https://data8.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https://github.com/data-8/materials-su24&urlpath=tree/materials-su24/lec/${DEMO}/${DEMO}.ipynb&branch=main" | sed 's/&/\\&/g')
DEMO_REPLACEMENT=" demo${LEC_NUM}: \"• [Demo](${DEMO_LINK})\""
fi
SLIDES_REPLACEMENT=" slide${LEC_NUM}: \"[Slides](${SLIDES})\""
sed -i "s| slide${LEC_NUM}: \"Slides\"|${SLIDES_REPLACEMENT}|g" $CONFIG_FILE
sed -i "s| demo${LEC_NUM}: \"• Demo\"|${DEMO_REPLACEMENT}|g" $CONFIG_FILE
- name: Commit changes
run: |
git config user.name 'jonathanferrari'
git config user.email '[email protected]'
git add _config.yml
git commit -m "Update _config.yml for lecture ${LEC_NUM}"
git push
- name: Install GitHub CLI
run: sudo apt-get install -y gh

- name: Authenticate with GitHub CLI
run: echo "${{ secrets.GH_PAT }}" | gh auth login --with-token

- name: Close issue with comment
- name: Parse issue content and update config
env:
LEC_NUM: ${{ env.Lecture_Number }}
DEMO: ${{ env.Demo }}
SLIDES: ${{ env.Slides }}
run: |
if [ "$DEMO" == "None" ]; then
DEMO_TEXT="None"
else
DEMO_LINK=$(echo "https://data8.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https://github.com/data-8/materials-su24&urlpath=tree/materials-su24/lec/${DEMO}/${DEMO}.ipynb&branch=main" | sed 's/&/\\&/g')
DEMO_TEXT="[Demo](${DEMO_LINK})"
fi
gh issue comment ${{ github.event.issue.number }} --body "Updated \`_config.yml\` for lecture ${LEC_NUM}:
- Slides: [Slides](${SLIDES})
- Demo: $DEMO_TEXT"
gh issue close ${{ github.event.issue.number }}
ISSUE_BODY: ${{ github.event.issue.body }}
GITHUB_ISSUE_NUMBER: ${{ github.event.issue.number }}
run: python update-lecture.py
2 changes: 1 addition & 1 deletion .github/workflows/update-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
python update_config.py
python update-site.py
13 changes: 0 additions & 13 deletions parse_issue.py

This file was deleted.

64 changes: 64 additions & 0 deletions update-lecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
import re
import subprocess

# Get environment variables
issue_body = os.getenv('ISSUE_BODY')
config_file = "_config.yml"
issue_number = os.getenv('GITHUB_ISSUE_NUMBER')

# Parse issue body
lecture_number = re.search(r'Lecture Number: (\d+)', issue_body).group(1).strip()
demo = re.search(r'Demo: (.+)', issue_body).group(1).strip()
slides = re.search(r'Slides: (.+)', issue_body).group(1).strip()

# Create demo link if demo is not 'None'
if demo.lower() != "none":
demo_link = f"https://data8.datahub.berkeley.edu/hub/user-redirect/git-pull?repo=https://github.com/data-8/materials-su24&urlpath=tree/materials-su24/lec/{demo}/{demo}.ipynb&branch=main"
else:
demo_link = "None"

# Replace text in config file
with open(config_file, 'r') as file:
config_content = file.read()

slide_line = f" slide{lecture_number}: \"Slides\""
demo_line = f" demo{lecture_number}: \"• Demo\""

if demo_link == "None":
demo_replacement = f" demo{lecture_number}: \"\""
else:
demo_replacement = f" demo{lecture_number}: \"• [Demo]({demo_link})\""

slides_replacement = f" slide{lecture_number}: \"[Slides]({slides})\""

config_content = config_content.replace(slide_line, slides_replacement)
config_content = config_content.replace(demo_line, demo_replacement)

with open(config_file, 'w') as file:
file.write(config_content)

# Git operations
subprocess.run(["git", "config", "--global", "user.name", "jonathanferrari"])
subprocess.run(["git", "config", "--global", "user.email", "[email protected]"])
subprocess.run(["git", "add", config_file])
subprocess.run(["git", "commit", "-m", f"Update _config.yml for lecture {lecture_number} materials"])
subprocess.run(["git", "push"])

# Writing to environment file
with open(os.getenv('GITHUB_ENV'), 'a') as env_file:
env_file.write(f"Lecture_Number={lecture_number}\n")
env_file.write(f"Demo={demo}\n")
env_file.write(f"Demo_Link={demo_link}\n")
env_file.write(f"Slides={slides}\n")

# Create GitHub issue comment and close issue
if demo_link == "None":
demo_text = "None"
else:
demo_text = f"• [Demo]({demo_link})"

comment_body = f"Updated `_config.yml` for lecture {lecture_number}:\n- Slides: [Slides]({slides})\n- Demo: {demo_text}"

subprocess.run(["gh", "issue", "comment", issue_number, "--body", comment_body])
subprocess.run(["gh", "issue", "close", issue_number])
File renamed without changes.

0 comments on commit 2bb7037

Please sign in to comment.