diff --git a/.github/workflows/discussion-thread-link.yml b/.github/workflows/discussion-thread-link.yml index 3399c836ee1e..021fed005079 100644 --- a/.github/workflows/discussion-thread-link.yml +++ b/.github/workflows/discussion-thread-link.yml @@ -48,6 +48,8 @@ jobs: let discussion = context.payload.discussion || null; let number = discussion ? discussion.number : null; + let discussionRepoMismatch = false; + if (manualUrl) { const match = manualUrl.match(/github\.com\/([^/]+)\/([^/]+)\/discussions\/(\d+)/i); if (!match) { @@ -62,10 +64,13 @@ jobs: return; } const { data } = await github.request( - 'GET /repos/{owner}/{repo}/discussions/{number}', - { owner, repo, number } + 'GET /repos/{owner}/{repo}/discussions/{discussion_number}', + { owner, repo, discussion_number: number } ); discussion = data; + discussionRepoMismatch = + owner.toLowerCase() !== context.repo.owner.toLowerCase() || + repo.toLowerCase() !== context.repo.repo.toLowerCase(); } if (!discussion) { @@ -200,9 +205,18 @@ jobs: const threadUrl = `https://lists.apache.org/thread/${tid}`; + if (discussionRepoMismatch) { + core.warning( + `Discussion repository ${owner}/${repo} does not match workflow repository ` + + `${context.repo.owner}/${context.repo.repo}. Skipping PATCH.` + ); + core.info(`Computed thread URL: ${threadUrl}`); + return; + } + const { data: current } = await github.request( - 'GET /repos/{owner}/{repo}/discussions/{number}', - { owner, repo, number } + 'GET /repos/{owner}/{repo}/discussions/{discussion_number}', + { owner, repo, discussion_number: number } ); const body = current.body || ''; @@ -214,9 +228,42 @@ jobs: const separator = body.trim().length ? '\n\n' : ''; const newBody = `${body}${separator}---\n**Mailing list thread:** ${threadUrl}\n`; - await github.request( - 'PATCH /repos/{owner}/{repo}/discussions/{discussion_number}', - { owner, repo, discussion_number: number, body: newBody } - ); + let updated = false; + + try { + await github.request( + 'PATCH /repos/{owner}/{repo}/discussions/{discussion_number}', + { + owner, + repo, + discussion_number: number, + body: newBody, + headers: { 'X-GitHub-Api-Version': '2022-11-28' }, + } + ); + updated = true; + core.info('Updated discussion via REST API'); + } catch (error) { + const status = error && error.status ? error.status : 'unknown'; + core.info(`REST update failed with status ${status}, attempting GraphQL`); + if (status !== 403 && status !== 404) { + throw error; + } + } + + if (!updated) { + const mutation = ` + mutation ($discussionId: ID!, $body: String!) { + updateDiscussion(input: { discussionId: $discussionId, body: $body }) { + discussion { number } + } + } + `; + await github.graphql(mutation, { + discussionId: discussion.node_id, + body: newBody, + }); + core.info('Updated discussion via GraphQL'); + } core.info(`Appended ${threadUrl}`);