Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions .github/workflows/discussion-thread-link.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 || '';
Expand All @@ -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}`);