@@ -2,16 +2,15 @@ name: Push → Discussion
22
33on :
44 push :
5- branches :
6- - ' ** ' # change to [ main ] if you only want main pushes
5+ branches : ['**']
6+ workflow_dispatch : {} # allows manual run for testing
77
88permissions :
99 contents : read
10- discussions : write # needed to create discussions
10+ discussions : write
1111
1212env :
13- # Must match an existing Discussions category in this repo
14- DISCUSSIONS_CATEGORY : Push Updates
13+ DISCUSSIONS_CATEGORY : Push Updates # must match the category name in Discussions
1514
1615jobs :
1716 post :
@@ -20,52 +19,56 @@ jobs:
2019 - name : Create a discussion for this push
2120 uses : actions/github-script@v7
2221 with :
22+ github-token : ${{ secrets.GITHUB_TOKEN }}
2323 script : |
24- const core = require("@actions/core");
2524 const { owner, repo } = context.repo;
26- const push = context.payload;
27-
28- // --- 1) Find category by name ---
2925 const desiredName = (process.env.DISCUSSIONS_CATEGORY || "").trim();
30- const { data: categories } = await github.request(
31- "GET /repos/{owner}/{repo}/discussions/categories",
32- { owner, repo }
33- );
34- if (!categories?.length) {
35- core.setFailed("No Discussions categories found. Enable Discussions and create at least one category.");
26+
27+ // 1) Fetch categories
28+ let categories;
29+ try {
30+ const res = await github.request(
31+ "GET /repos/{owner}/{repo}/discussions/categories",
32+ { owner, repo }
33+ );
34+ categories = res.data || [];
35+ } catch (e) {
36+ core.setFailed(`Failed to fetch categories: ${e.message}`);
3637 return;
3738 }
38- const category = categories.find(c => c.name.toLowerCase() === desiredName.toLowerCase()) || categories[0];
3939
40- // --- 2) Build title/body from push payload ---
41- const branch = (push.ref || "").replace("refs/heads/", "");
42- const pusher =
43- push.pusher?.name ||
44- push.sender?.login ||
45- "unknown";
40+ // 2) Find category
41+ const category = categories.find(
42+ c => c.name.toLowerCase() === desiredName.toLowerCase()
43+ );
44+ if (!category) {
45+ const names = categories.map(c => `- ${c.name} (id: ${c.id})`).join("\n");
46+ core.setFailed(
47+ `Category "${desiredName}" not found.\nAvailable categories:\n${names}`
48+ );
49+ return;
50+ }
4651
52+ // 3) Build discussion content
53+ const push = context.payload;
54+ const branch = (push.ref || "").replace("refs/heads/", "") || "(unknown)";
55+ const pusher = push.pusher?.name || push.sender?.login || "unknown";
4756 const commits = push.commits || [];
4857 const commitCount = commits.length;
4958
50- // Title like: "Push to main by Jarrod — 3 commits"
51- const title = `Push to ${branch || "(unknown branch)"} by ${pusher} — ${commitCount} commit${commitCount === 1 ? "" : "s"}`;
59+ const title = `Push to ${branch} by ${pusher} — ${commitCount} commit${commitCount === 1 ? "" : "s"}`;
5260
53- // Each commit: short sha, first-line message, author, link
54- const lines = [];
55- for (const c of commits) {
56- const sha7 = (c.id || "").substring(0, 7);
57- const firstLine = (c.message || "").split("\n")[0];
61+ const lines = commits.map(c => {
62+ const sha7 = (c.id || "").slice(0,7);
63+ const first = (c.message || "").split("\n")[0];
5864 const author = c.author?.name || "unknown";
59- const url = c.url || `https://github.com/${owner}/${repo}/commit/${c.id}`;
60- lines.push(`- [\`${sha7}\`](${url}) ${firstLine} — _${author}_`);
61- }
65+ return `- [\`${sha7}\`](${c.url}) ${first} — _${author}_`;
66+ });
6267
63- // Compare URL (payload usually provides it)
6468 const compareUrl =
6569 push.compare ||
6670 `https://github.com/${owner}/${repo}/compare/${push.before}...${push.after}`;
6771
68- // Top banner + details + footer
6972 const body = [
7073 `> _This discussion was created automatically by GitHub Actions from a \`push\` event._`,
7174 "",
@@ -79,16 +82,13 @@ jobs:
7982 "<sub>— end of automated log —</sub>"
8083 ].join("\n");
8184
82- // --- 3) Create the Discussion ---
83- const { data: discussion } = await github.request(
84- "POST /repos/{owner}/{repo}/discussions",
85- {
86- owner,
87- repo,
88- title,
89- body,
90- category_id: category.id
91- }
92- );
93-
94- core.info(`Created discussion: ${discussion.html_url}`);
85+ // 4) Create the discussion
86+ try {
87+ const { data: discussion } = await github.request(
88+ "POST /repos/{owner}/{repo}/discussions",
89+ { owner, repo, title, body, category_id: category.id }
90+ );
91+ core.info(`Created discussion: ${discussion.html_url}`);
92+ } catch (e) {
93+ core.setFailed(`Failed to create discussion: ${e.message}`);
94+ }
0 commit comments