Skip to content

Commit

Permalink
Add script to comment on PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
beyarkay committed Sep 10, 2022
1 parent 26a4665 commit 2d19fc2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build-calendars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ jobs:
with:
command: run
args: --release

- name: Create summary of calendar changes
id: create_summary
run: python calendars_to_pr_comment.py

- name: Comment summary to PR
uses: mb2dev/github-action-comment-pull-request@1.0.0
with:
message: ${{ steps.create_summary.outputs.pr-comment }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41 changes: 41 additions & 0 deletions calendars_to_pr_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ics import Calendar
import datetime

with open("calendars/western-cape-stellenbosch.ics", "r") as f:
c = Calendar(f.read())
output = ["@beyarkay here's a load shedding summary based on this PR:"]
output.append("### Load shedding for Stellenbosch")
output.append("")
fmt_str = "ddd D MMMM HH:mm"
if len(c.events) == 0:
output.append("No load shedding for Stellenbosch")
for e in sorted(c.events):
days = e.duration.days
hours, rem = divmod(e.duration.seconds, 3600)
minutes, seconds = divmod(rem, 60)
duration_fmt = (
"" if days == 0 else str(days) + " days, "
) + f"{hours}h{minutes}m"
start = e.begin.to("Africa/Johannesburg")
finsh = e.end.to("Africa/Johannesburg")
lines = e.description.splitlines()
national_ls_str = "National loadshedding information scraped from "
source = None
for l in lines:
if national_ls_str in l:
source = l.replace(national_ls_str, "")
linked_name = (
f"[{e.name}]({source})" if source is not None else f"{e.name} (no source)"
)
if start.date() == finsh.date():
output.append(
f"- {linked_name}: {start.format('ddd D MMMM [**]HH:mm')}-{finsh.format('HH:mm')}** (for {duration_fmt})"
)
else:
output.append(
f"- {linked_name}: **{start.format(fmt_str)} - {finsh.format(fmt_str)}** (for {duration_fmt})"
)
# Use `%0A` as the newline character in order to appease GitHub
# https://github.com/orgs/community/discussions/26288#discussioncomment-3251236
joined_output = "%0A".join(output).replace("\n", "%0A")
print(f"::set-output name=pr-comment::{joined_output}")
1 change: 1 addition & 0 deletions manually_specified.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ changes:
start: 2022-09-10T10:00:00
finsh: 2022-09-12T04:59:00
source: https://twitter.com/Eskom_SA/status/1568494585113976835

- stage: 2
start: 2022-09-12T05:00:00
finsh: 2022-09-16T23:59:00
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
arrow==1.2.3
attrs==22.1.0
beautifulsoup4==4.11.1
black==22.6.0
certifi==2022.6.15
charset-normalizer==2.1.0
click==8.1.3
distro==1.7.0
html5lib==1.1
ics==0.7.2
idna==3.3
lxml==4.9.1
mypy-extensions==0.4.3
Expand All @@ -14,10 +17,13 @@ pathspec==0.9.0
platformdirs==2.5.2
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
requests==2.28.1
six==1.16.0
soupsieve==2.3.2.post1
tabula-py==2.4.0
TatSu==5.8.3
tomli==2.0.1
urllib3==1.26.9
webencodings==0.5.1
yamllint==1.27.1

0 comments on commit 2d19fc2

Please sign in to comment.