Skip to content

Commit 9fdbbfe

Browse files
authored
Merge pull request #453 from cibere/plugin-release-webhook
add plugin release webhook notification
2 parents 6c322cf + dae44c8 commit 9fdbbfe

File tree

6 files changed

+77
-25
lines changed

6 files changed

+77
-25
lines changed

.github/workflows/python-passed.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ jobs:
1919
uses: actions/setup-python@v5
2020
with:
2121
python-version: 3.x
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install -r ./ci/envs/requirements-update-tested.txt
2226
- name: Run script
23-
run: python ./ci/src/update-tested.py
27+
run: python ./ci/src/update-tested.py ${{ secrets.DISCORD_WEBHOOK }}
2428
- name: Update plugin manifest
2529
if: success()
2630
uses: stefanzweifel/git-auto-commit-action@v4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aiohttp
2+
tqdm

ci/src/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
github_url = "https://github.com"
4545
release_date = "LatestReleaseDate"
4646
date_added = "DateAdded"
47+
website = "Website"
4748

4849
# typing
4950
PluginType = Dict[str, str]

ci/src/discord.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import aiohttp
2-
2+
from tqdm.asyncio import tqdm
33
from _utils import *
44

55
MAX_BODY_LEN = 1024
66

77

8-
async def update_hook(webhook_url: str, info: dict, latest_ver: str, release: dict) -> None:
8+
async def update_hook(webhook_url: str, info: PluginType, latest_ver: str, release: dict) -> None:
99
embed = {
1010
"content": None,
1111
"embeds": [
@@ -43,6 +43,43 @@ async def update_hook(webhook_url: str, info: dict, latest_ver: str, release: di
4343
embed['embeds'][0]['fields'].append({"name": "Release Notes", "value": truncate_release_notes(release['html_url'], release.get('body', ""))})
4444
async with aiohttp.ClientSession() as session:
4545
await session.post(webhook_url, json=embed)
46+
47+
async def release_hook(webhook_url: str, info: PluginType) -> None:
48+
embed = {
49+
"content": None,
50+
"embeds": [
51+
{
52+
"title": info[plugin_name],
53+
"description": f"New Plugin!\nReleased at v{info[version]}.",
54+
"url": info[website],
55+
"color": 5763719, # green
56+
"fields": [
57+
{
58+
"name": "Plugin Description",
59+
"value": info[description]
60+
},
61+
{
62+
"name": "Plugin Language",
63+
"value": info[language_name]
64+
}
65+
],
66+
"author": {
67+
"name": info[author]
68+
},
69+
"thumbnail": {
70+
"url": info[icon_path]
71+
}
72+
}
73+
]
74+
}
75+
if 'github.com' in info[url_sourcecode].lower():
76+
github_username = info[url_sourcecode].split('/')[3]
77+
embed['embeds'][0]['author']['name'] = github_username
78+
embed['embeds'][0]['author']['url'] = f"{github_url}/{github_username}"
79+
embed['embeds'][0]["author"]["icon_url"] = f"{github_url}/{github_username}.png?size=40"
80+
81+
async with aiohttp.ClientSession() as session:
82+
await session.post(webhook_url, json=embed)
4683

4784
def truncate_release_notes(url: str, release_notes: str, length: int = MAX_BODY_LEN) -> str:
4885
if len(release_notes) <= length:

ci/src/update-tested.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
import sys
2-
import json
3-
import os
4-
import zipfile
5-
import io
6-
from datetime import datetime
1+
import asyncio
2+
from datetime import datetime, UTC
3+
from sys import argv
4+
from _utils import plugin_reader, plugin_writer, date_added
5+
from discord import release_hook
76

8-
from _utils import clean, id_name, language_list, language_name, plugin_reader, plugin_writer, release_date, date_added
97

10-
if __name__ == "__main__":
8+
async def update_tested():
9+
webhook_url = None
10+
if len(argv) > 1:
11+
webhook_url = argv[1]
12+
1113
plugin_infos = plugin_reader()
1214

1315
for idx, plugin in enumerate(plugin_infos):
1416
if plugin["Language"] == "python" and "Tested" not in plugin.keys():
1517
plugin_infos[idx]["Tested"] = True
18+
1619
# Add date added if field is not present
1720
if plugin.get(date_added) is None:
18-
plugin_infos[idx][date_added] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
19-
plugin_writer(plugin_infos)
20-
21-
22-
21+
plugin_infos[idx][date_added] = datetime.now(UTC).strftime(
22+
"%Y-%m-%dT%H:%M:%SZ"
23+
)
2324

25+
if webhook_url:
26+
await release_hook(webhook_url, plugin)
2427

28+
plugin_writer(plugin_infos)
2529

2630

31+
if __name__ == "__main__":
32+
asyncio.run(update_tested())

ci/src/updater.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
from _utils import *
1313
from discord import update_hook
1414

15-
1615
async def batch_github_plugin_info(
17-
info: P, tags: ETagsType, github_token=None, webhook_url: str = None
16+
info: P, tags: ETagsType, github_token=None, webhook_url: str | None = None
1817
) -> P:
1918
try:
2019
headers = {"authorization": f"token {github_token}"}
@@ -47,9 +46,10 @@ async def batch_github_plugin_info(
4746
info[release_date] = latest_rel.get("published_at")
4847
if assets:
4948
info[url_download] = assets[0]["browser_download_url"]
50-
await send_notification(
51-
info, clean(latest_rel["tag_name"], "v"), latest_rel, webhook_url
52-
)
49+
if webhook_url:
50+
await send_notification(
51+
info, clean(latest_rel["tag_name"], "v"), latest_rel, webhook_url
52+
)
5353
info[version] = clean(latest_rel["tag_name"], "v")
5454

5555
tags[info[id_name]] = res.headers.get(etag, "")
@@ -62,7 +62,7 @@ async def batch_github_plugin_info(
6262

6363

6464
async def batch_plugin_infos(
65-
plugin_infos: Ps, tags: ETagsType, github_token, webhook_url: str = None
65+
plugin_infos: Ps, tags: ETagsType, github_token, webhook_url: str | None = None
6666
) -> Ps:
6767
return await tqdm.gather(
6868
*[
@@ -72,7 +72,7 @@ async def batch_plugin_infos(
7272
)
7373

7474

75-
def remove_unused_etags(plugin_infos: Ps, etags: ETagsType) -> ETagsType:
75+
def remove_unused_etags(plugin_infos: PluginsType, etags: ETagsType) -> ETagsType:
7676
etags_updated = {}
7777
plugin_ids = [info.get("ID") for info in plugin_infos]
7878

@@ -90,16 +90,18 @@ def remove_unused_etags(plugin_infos: Ps, etags: ETagsType) -> ETagsType:
9090

9191

9292
async def send_notification(
93-
info: P, latest_ver, release, webhook_url: str = None
93+
info: PluginType, latest_ver, release, webhook_url: str | None = None
9494
) -> None:
95+
if not webhook_url:
96+
return
97+
9598
if version_tuple(info[version]) != version_tuple(latest_ver):
9699
tqdm.write(f"Update detected: {info[plugin_name]} {latest_ver}")
97100
try:
98101
await update_hook(webhook_url, info, latest_ver, release)
99102
except Exception as e:
100103
tqdm.write(str(e))
101104

102-
103105
async def main():
104106
webhook_url = None
105107
if len(argv) > 1:

0 commit comments

Comments
 (0)