Skip to content

Commit 30722e6

Browse files
committed
Diff channels on PRs
1 parent a146270 commit 30722e6

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Diff channels
2+
on:
3+
pull_request:
4+
paths:
5+
- channels.yml
6+
- channels.schema.yml
7+
jobs:
8+
diff-channels:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- uses: actions/checkout@v4
15+
with:
16+
ref: ${{ github.base_ref }}
17+
path: pr-base
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: "3.11"
23+
24+
- name: Cache dependencies
25+
uses: actions/cache@v3
26+
id: cache-dep
27+
with:
28+
path: venv
29+
key: deps-1-${{ hashfiles('script/requirements.txt') }}
30+
31+
- name: Install Dependencies
32+
run: |
33+
python3 -m venv venv
34+
venv/bin/pip install -U pip wheel
35+
venv/bin/pip install -r script/requirements.txt
36+
venv/bin/pip install .
37+
38+
- name: Diff channels
39+
run: |
40+
source venv/bin/activate
41+
python -m sr.discord_bot diff pr-base/channels.yml channels.yml > /tmp/channel-diff.txt
42+
43+
- name: Post diff as comment on PR
44+
uses: thollander/actions-comment-pull-request@v3
45+
with:
46+
file-path: /tmp/channel-diff.txt
47+
comment-tag: execution

channels.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
- name: community-updates
112112
topic: Discord community updates
113113
use_case: discord
114+
- name: ci-test
114115

115116
# Do not change the category below. It is populated automatically when users join.
116117
- name: Welcome

src/sr/discord_bot/__main__.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import argparse
55

66
import sentry_sdk
7+
import yaml
78
from dotenv import load_dotenv
89
from discord import Intents
910

1011
from sr.discord_bot.bot import BotClient
12+
from sr.discord_bot.channel import ChannelSet
13+
from sr.discord_bot.schema import ChannelDefinition
1114

1215
load_dotenv()
1316
logger = logging.getLogger("srbot")
@@ -20,24 +23,42 @@
2023
traces_sample_rate=1.0,
2124
)
2225

23-
intents = Intents.default()
24-
intents.members = True # Listen to member joins
25-
26-
token = os.getenv("DISCORD_TOKEN")
27-
if token is None:
28-
print("No token provided.", file=sys.stderr)
29-
exit(1)
30-
3126
parser = argparse.ArgumentParser(description="Student Robotics Discord Bot")
3227
subcommands = parser.add_subparsers(dest="command")
3328
parser_run = subcommands.add_parser("run", help="Run the Discord bot")
3429
parser_plan = subcommands.add_parser("plan", help="List pending guild changes")
3530
parser_apply = subcommands.add_parser("apply", help="Apply pending guild changes")
31+
parser_diff = subcommands.add_parser("diff", help="Compare two channel configurations")
32+
parser_diff.add_argument("old_config", type=argparse.FileType('r'))
33+
parser_diff.add_argument("new_config", type=argparse.FileType('r'))
34+
3635
args = parser.parse_args()
3736

3837
if args.command is None:
3938
parser.print_help()
4039
exit(1)
40+
elif args.command == "diff":
41+
# The diff command doesn't require us to connect to Discord so we can handle it here
42+
if args.old_config is None or args.new_config is None:
43+
parser.print_help()
44+
exit(1)
45+
old = [ChannelDefinition.load(ch) for ch in yaml.load(args.old_config.read(), Loader=yaml.Loader)]
46+
new = [ChannelDefinition.load(ch) for ch in yaml.load(args.new_config.read(), Loader=yaml.Loader)]
47+
diff = ChannelSet.diff(old, new)
48+
if len(diff) > 0:
49+
for change in diff:
50+
print(change)
51+
else:
52+
print("No changes found.")
53+
exit(0)
54+
55+
intents = Intents.default()
56+
intents.members = True # Listen to member joins
57+
58+
token = os.getenv("DISCORD_TOKEN")
59+
if token is None:
60+
print("No token provided.", file=sys.stderr)
61+
exit(1)
4162

4263
bot = BotClient(logger=logger, intents=intents)
4364
bot.mode = args.command

0 commit comments

Comments
 (0)