4848 let discussion = context.payload.discussion || null;
4949 let number = discussion ? discussion.number : null;
5050
51+ let discussionRepoMismatch = false;
52+
5153 if (manualUrl) {
5254 const match = manualUrl.match(/github\.com\/([^/]+)\/([^/]+)\/discussions\/(\d+)/i);
5355 if (!match) {
@@ -62,10 +64,13 @@ jobs:
6264 return;
6365 }
6466 const { data } = await github.request(
65- 'GET /repos/{owner}/{repo}/discussions/{number }',
66- { owner, repo, number }
67+ 'GET /repos/{owner}/{repo}/discussions/{discussion_number }',
68+ { owner, repo, discussion_number: number }
6769 );
6870 discussion = data;
71+ discussionRepoMismatch =
72+ owner.toLowerCase() !== context.repo.owner.toLowerCase() ||
73+ repo.toLowerCase() !== context.repo.repo.toLowerCase();
6974 }
7075
7176 if (!discussion) {
@@ -200,9 +205,18 @@ jobs:
200205
201206 const threadUrl = `https://lists.apache.org/thread/${tid}`;
202207
208+ if (discussionRepoMismatch) {
209+ core.warning(
210+ `Discussion repository ${owner}/${repo} does not match workflow repository ` +
211+ `${context.repo.owner}/${context.repo.repo}. Skipping PATCH.`
212+ );
213+ core.info(`Computed thread URL: ${threadUrl}`);
214+ return;
215+ }
216+
203217 const { data: current } = await github.request(
204- 'GET /repos/{owner}/{repo}/discussions/{number }',
205- { owner, repo, number }
218+ 'GET /repos/{owner}/{repo}/discussions/{discussion_number }',
219+ { owner, repo, discussion_number: number }
206220 );
207221
208222 const body = current.body || '';
@@ -214,9 +228,42 @@ jobs:
214228 const separator = body.trim().length ? '\n\n' : '';
215229 const newBody = `${body}${separator}---\n**Mailing list thread:** ${threadUrl}\n`;
216230
217- await github.request(
218- 'PATCH /repos/{owner}/{repo}/discussions/{discussion_number}',
219- { owner, repo, discussion_number: number, body: newBody }
220- );
231+ let updated = false;
232+
233+ try {
234+ await github.request(
235+ 'PATCH /repos/{owner}/{repo}/discussions/{discussion_number}',
236+ {
237+ owner,
238+ repo,
239+ discussion_number: number,
240+ body: newBody,
241+ headers: { 'X-GitHub-Api-Version': '2022-11-28' },
242+ }
243+ );
244+ updated = true;
245+ core.info('Updated discussion via REST API');
246+ } catch (error) {
247+ const status = error && error.status ? error.status : 'unknown';
248+ core.info(`REST update failed with status ${status}, attempting GraphQL`);
249+ if (status !== 403 && status !== 404) {
250+ throw error;
251+ }
252+ }
253+
254+ if (!updated) {
255+ const mutation = `
256+ mutation ($discussionId: ID!, $body: String!) {
257+ updateDiscussion(input: { discussionId: $discussionId, body: $body }) {
258+ discussion { number }
259+ }
260+ }
261+ `;
262+ await github.graphql(mutation, {
263+ discussionId: discussion.node_id,
264+ body: newBody,
265+ });
266+ core.info('Updated discussion via GraphQL');
267+ }
221268
222269 core.info(`Appended ${threadUrl}`);
0 commit comments