Skip to content

Commit

Permalink
Merge pull request #487 from uclaacm/daniel/announcements
Browse files Browse the repository at this point in the history
announcements
  • Loading branch information
jamesmwu authored Feb 2, 2025
2 parents b130707 + 58f10d9 commit a9f7212
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/announcements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
name: Update Announcements
if: |
!github.event.issue.pull_request &&
github.event.issue.number == 181 &&
github.event.issue.number == 469 &&
(github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'CONTRIBUTOR' ||
Expand All @@ -30,7 +30,7 @@ jobs:
python -m pip install -r .github/python_requirements.txt
- name: Run update_announcements script
run: |
python update_announcements.py
python src/scripts/update_announcements.py
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
Expand Down
1 change: 1 addition & 0 deletions github/workflows/announcements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

53 changes: 53 additions & 0 deletions src/scripts/update_announcements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Initial function:
# Inputs: Github API credentials (not using for now)
# call the github API to get all the comments for a certain issue on the hoth github
# format the data into a json containing the data that we want
# inject the json into a json file

import requests
import json
import sys
import os

# User whitelist and issue number we are looking for
user_whitelist = ['jamesmwu', 'danielhzhou']
# HOTH repository issues accessed via Github API
repo_issues = "https://api.github.com/repos/uclaacm/hoth.uclaacm.com/issues"

issue_num = '469'

url = repo_issues + '/' + issue_num + '/' + 'comments'

# Get given issue
try:
announcements = requests.get(url)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
print("Request completed with status code: ", announcements.status_code)

# Decode json to Python dictionary for parsing
try:
announcements_json = announcements.json()
except requests.exceptions.JSONDecodeError as e:
raise SystemExit(e)
valid_comments = []

# Keep track of every comment from valid usernames
for index, element in enumerate(announcements_json):
if element['user']['login'] in user_whitelist:
body = element['body']
# If there is no partition, subject will contain the entire string
subject, partition, comment = body.partition('(Subject)')
comment = {'id': index, 'subject': subject.strip(), 'body': comment.strip(), 'timestamp': element['created_at']}
valid_comments.append(comment)

# Insert all valid comment json objects into a file in reverse order
filename = 'src/data/announcements.json'
json_string = json.dumps(valid_comments[::-1], indent=4)
try:
with open(filename, 'w') as f:
f.write(json_string)
except EnvironmentError:
print("Error opening/writing to file. ")

print("Finished.")

0 comments on commit a9f7212

Please sign in to comment.