Skip to content

Commit df06365

Browse files
committed
Update push-to-discussions.yml
1 parent 070bbe7 commit df06365

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed
Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
name: Push → Discussion
1+
name: Push → Discussion (GraphQL)
22

33
on:
44
push:
55
branches: ['**']
6-
workflow_dispatch: {} # allows manual run for testing
6+
workflow_dispatch: {}
77

88
permissions:
99
contents: read
1010
discussions: write
1111

1212
env:
13-
DISCUSSIONS_CATEGORY: Push Updates # must match the category name in Discussions
13+
DISCUSSIONS_CATEGORY: Push Updates # must match the category name in your repo
1414

1515
jobs:
1616
post:
@@ -24,32 +24,45 @@ jobs:
2424
const { owner, repo } = context.repo;
2525
const desiredName = (process.env.DISCUSSIONS_CATEGORY || "").trim();
2626
27-
// 1) Fetch categories
28-
let categories;
27+
// 1) GraphQL: get repo id + discussion categories
28+
const query = `
29+
query($owner: String!, $name: String!) {
30+
repository(owner: $owner, name: $name) {
31+
id
32+
hasDiscussionsEnabled
33+
discussionCategories(first: 50) {
34+
nodes { id name }
35+
}
36+
}
37+
}
38+
`;
39+
let repoInfo;
2940
try {
30-
const res = await github.request(
31-
"GET /repos/{owner}/{repo}/discussions/categories",
32-
{ owner, repo }
33-
);
34-
categories = res.data || [];
41+
repoInfo = await github.graphql(query, { owner, name: repo });
3542
} catch (e) {
36-
core.setFailed(`Failed to fetch categories: ${e.message}`);
43+
core.setFailed(\`GraphQL repo/category lookup failed: \${e.message}\`);
3744
return;
3845
}
3946
40-
// 2) Find category
41-
const category = categories.find(
42-
c => c.name.toLowerCase() === desiredName.toLowerCase()
43-
);
47+
const repository = repoInfo?.repository;
48+
if (!repository) {
49+
core.setFailed("Repository not found by GraphQL; check owner/repo context.");
50+
return;
51+
}
52+
if (!repository.hasDiscussionsEnabled) {
53+
core.setFailed("Discussions are not enabled for this repository (Settings → General → Features → Discussions).");
54+
return;
55+
}
56+
57+
const categories = repository.discussionCategories?.nodes || [];
58+
const category = categories.find(c => c.name.toLowerCase() === desiredName.toLowerCase());
4459
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-
);
60+
const names = categories.map(c => `- ${c.name}`).join("\n") || "(none found)";
61+
core.setFailed(`Category "${desiredName}" not found.\nAvailable categories:\n${names}`);
4962
return;
5063
}
5164
52-
// 3) Build discussion content
65+
// 2) Build title/body from push payload
5366
const push = context.payload;
5467
const branch = (push.ref || "").replace("refs/heads/", "") || "(unknown)";
5568
const pusher = push.pusher?.name || push.sender?.login || "unknown";
@@ -62,12 +75,13 @@ jobs:
6275
const sha7 = (c.id || "").slice(0,7);
6376
const first = (c.message || "").split("\n")[0];
6477
const author = c.author?.name || "unknown";
65-
return `- [\`${sha7}\`](${c.url}) ${first} — _${author}_`;
78+
const url = c.url || \`https://github.com/${owner}/${repo}/commit/\${c.id}\`;
79+
return `- [\`${sha7}\`](${url}) ${first} — _${author}_`;
6680
});
6781
6882
const compareUrl =
6983
push.compare ||
70-
`https://github.com/${owner}/${repo}/compare/${push.before}...${push.after}`;
84+
\`https://github.com/${owner}/${repo}/compare/\${push.before}...\${push.after}\`;
7185
7286
const body = [
7387
`> _This discussion was created automatically by GitHub Actions from a \`push\` event._`,
@@ -82,13 +96,28 @@ jobs:
8296
"<sub>— end of automated log —</sub>"
8397
].join("\n");
8498
85-
// 4) Create the discussion
99+
// 3) GraphQL: create discussion
100+
const mutation = `
101+
mutation($repoId: ID!, $catId: ID!, $title: String!, $body: String!) {
102+
createDiscussion(input: {
103+
repositoryId: $repoId,
104+
categoryId: $catId,
105+
title: $title,
106+
body: $body
107+
}) {
108+
discussion { url }
109+
}
110+
}
111+
`;
86112
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}`);
113+
const res = await github.graphql(mutation, {
114+
repoId: repository.id,
115+
catId: category.id,
116+
title,
117+
body
118+
});
119+
const url = res?.createDiscussion?.discussion?.url;
120+
core.info(\`Created discussion: \${url}\`);
92121
} catch (e) {
93-
core.setFailed(`Failed to create discussion: ${e.message}`);
122+
core.setFailed(\`Failed to create discussion via GraphQL: \${e.message}\`);
94123
}

0 commit comments

Comments
 (0)