|
5 | 5 | types: |
6 | 6 | - opened |
7 | 7 |
|
8 | | -permissions: |
9 | | - contents: read |
| 8 | +permissions: {} |
10 | 9 |
|
11 | 10 | jobs: |
12 | 11 | notify-new-bugs-announce: |
13 | | - runs-on: ubuntu-slim |
14 | | - permissions: |
15 | | - issues: read |
| 12 | + runs-on: ubuntu-latest |
16 | 13 | timeout-minutes: 10 |
17 | 14 | steps: |
18 | | - - uses: actions/setup-node@v7.0.0 |
19 | | - with: |
20 | | - node-version: 20 |
21 | | - # TODO: Replace with a committed lockfile and `npm ci`, or drop npm |
22 | | - # and call the Mailgun REST API from the standard library. |
23 | | - - run: npm install mailgun.js form-data # zizmor: ignore[adhoc-packages] |
24 | 15 | - name: Send notification |
25 | | - uses: actions/github-script@v9.0.0 |
26 | 16 | env: |
27 | 17 | MAILGUN_API_KEY: ${{ secrets.MAILGUN_PYTHON_ORG_MAILGUN_KEY }} |
28 | | - with: |
29 | | - script: | |
30 | | - const Mailgun = require("mailgun.js"); |
31 | | - const formData = require('form-data'); |
32 | | - const mailgun = new Mailgun(formData); |
33 | | - const DOMAIN = "mailgun.python.org"; |
34 | | - const mg = mailgun.client({username: 'api', key: process.env.MAILGUN_API_KEY}); |
35 | | - github.rest.issues.get({ |
36 | | - issue_number: context.issue.number, |
37 | | - owner: context.repo.owner, |
38 | | - repo: context.repo.repo, |
39 | | - }) |
40 | | - .then(function(issue) { |
41 | | - const payload = { |
42 | | - author : issue.data.user.login, |
43 | | - issue : issue.data.number, |
44 | | - title : issue.data.title, |
45 | | - url : issue.data.html_url, |
46 | | - labels : issue.data.labels.map(label => { return label.name }).join(", "), |
47 | | - assignee : issue.data.assignees.map(assignee => { return assignee.login }), |
48 | | - // We need to truncate the body size, because the max size for |
49 | | - // the whole payload is 16kb. We want to be safe and assume that |
50 | | - // body can take up to ~8kb of space. |
51 | | - body : (issue.data.body || "").substring(0, 8000) |
52 | | - }; |
| 18 | + shell: python |
| 19 | + run: | |
| 20 | + import base64 |
| 21 | + import json |
| 22 | + import os |
| 23 | + import urllib.error |
| 24 | + import urllib.parse |
| 25 | + import urllib.request |
53 | 26 |
|
54 | | - const data = { |
55 | | - from: "CPython Issues <github@mailgun.python.org>", |
56 | | - to: "new-bugs-announce@python.org", |
57 | | - subject: `[Issue ${issue.data.number}] ${issue.data.title}`, |
58 | | - template: "new-github-issue", |
59 | | - 'o:tracking-clicks': 'no', |
60 | | - 'h:X-Mailgun-Variables': JSON.stringify(payload) |
61 | | - }; |
62 | | - return mg.messages.create(DOMAIN, data) |
63 | | - }) |
64 | | - .then(msg => console.log(msg)); |
| 27 | + with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as f: |
| 28 | + issue = json.load(f)["issue"] |
| 29 | +
|
| 30 | + payload = { |
| 31 | + "author": issue["user"]["login"], |
| 32 | + "issue": issue["number"], |
| 33 | + "title": issue["title"], |
| 34 | + "url": issue["html_url"], |
| 35 | + "labels": ", ".join(label["name"] for label in issue["labels"]), |
| 36 | + "assignee": [assignee["login"] for assignee in issue["assignees"]], |
| 37 | + # We need to truncate the body size, because the max size for |
| 38 | + # the whole payload is 16 kB. We want to be safe and assume that |
| 39 | + # body can take up to ~8 kB of space. Truncate by bytes, not |
| 40 | + # characters, so non-ASCII text can't exceed that. |
| 41 | + "body": (issue["body"] or "").encode()[:8000].decode(errors="ignore"), |
| 42 | + } |
| 43 | +
|
| 44 | + data = urllib.parse.urlencode( |
| 45 | + { |
| 46 | + "from": "CPython Issues <github@mailgun.python.org>", |
| 47 | + "to": "new-bugs-announce@python.org", |
| 48 | + "subject": f"[Issue {issue['number']}] {issue['title']}", |
| 49 | + "template": "new-github-issue", |
| 50 | + "o:tracking-clicks": "no", |
| 51 | + "h:X-Mailgun-Variables": json.dumps(payload, ensure_ascii=False), |
| 52 | + } |
| 53 | + ).encode() |
| 54 | +
|
| 55 | + credentials = b"api:" + os.environ["MAILGUN_API_KEY"].encode() |
| 56 | + request = urllib.request.Request( |
| 57 | + "https://api.mailgun.net/v3/mailgun.python.org/messages", |
| 58 | + data=data, |
| 59 | + headers={ |
| 60 | + "Authorization": "Basic " + base64.b64encode(credentials).decode(), |
| 61 | + }, |
| 62 | + ) |
| 63 | + try: |
| 64 | + with urllib.request.urlopen(request) as response: |
| 65 | + print(response.read().decode()) |
| 66 | + except urllib.error.HTTPError as e: |
| 67 | + print(f"Mailgun returned HTTP {e.code}: {e.read().decode()}") |
| 68 | + raise SystemExit(1) |
0 commit comments