diff --git a/.github/workflows/update-lecture.yml b/.github/workflows/update-lecture.yml index 6f7a688..e991560 100644 --- a/.github/workflows/update-lecture.yml +++ b/.github/workflows/update-lecture.yml @@ -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 'jonathanferrari@berkeley.edu' - 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 \ No newline at end of file diff --git a/.github/workflows/update-site.yml b/.github/workflows/update-site.yml index b1342a9..bedd64d 100644 --- a/.github/workflows/update-site.yml +++ b/.github/workflows/update-site.yml @@ -30,4 +30,4 @@ jobs: env: GH_TOKEN: ${{ secrets.GH_PAT }} run: | - python update_config.py \ No newline at end of file + python update-site.py \ No newline at end of file diff --git a/parse_issue.py b/parse_issue.py deleted file mode 100644 index bb930ce..0000000 --- a/parse_issue.py +++ /dev/null @@ -1,13 +0,0 @@ -import os -import re - -issue_body = os.getenv('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() - -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"Slides={slides}\n") diff --git a/update-lecture.py b/update-lecture.py new file mode 100644 index 0000000..8a4335f --- /dev/null +++ b/update-lecture.py @@ -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", "jonathanferrari@berkeley.edu"]) +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]) \ No newline at end of file diff --git a/update_config.py b/update-site.py similarity index 100% rename from update_config.py rename to update-site.py