import { type Octokit } from '@octokit/rest' import coreLib from '@actions/core' type CRIArgs = { core: typeof coreLib octokit: Octokit reportTitle: string reportBody: string reportRepository: string reportLabel: string } export async function createReportIssue({ core, octokit, reportTitle, reportBody, reportRepository, reportLabel, }: CRIArgs) { const [owner, repo] = reportRepository.split('/') // Create issue let newReport try { const { data } = await octokit.request('POST /repos/{owner}/{repo}/issues', { owner, repo, title: reportTitle, body: reportBody, labels: [reportLabel], }) newReport = data core.info(`Created new report issue at ${newReport.html_url}\n`) } catch (error: any) { core.error(error) core.setFailed('Error creating new issue') throw error } return newReport } type LRArgs = { core: typeof coreLib octokit: Octokit newReport: any reportRepository: string reportAuthor: string reportLabel: string } export async function linkReports({ core, octokit, newReport, reportRepository, reportAuthor, reportLabel, }: LRArgs) { const [owner, repo] = reportRepository.split('/') core.info('Attempting to link reports...') // Find previous report issue let previousReports try { previousReports = await octokit.rest.issues.listForRepo({ owner, repo, creator: reportAuthor, labels: reportLabel, state: 'all', // We want to get the previous report, even if it is closed sort: 'created', direction: 'desc', per_page: 25, }) previousReports = previousReports.data } catch (error) { core.setFailed('Error listing issues for repo') throw error } core.info(`Found ${previousReports.length} previous reports`) if (previousReports.length <= 1) { core.info('No previous reports to link to') return } // 2nd report should be most recent previous report const previousReport = previousReports[1] // Comment the old report link on the new report try { await octokit.rest.issues.createComment({ owner, repo, issue_number: newReport.number, body: `⬅️ [Previous report](${previousReport.html_url})`, }) core.info(`Linked old report to new report via comment on new report, #${newReport.number}`) } catch (error) { core.setFailed(`Error commenting on newReport, #${newReport.number}`) throw error } // Comment on all previous reports that are still open for (const oldReport of previousReports) { if (oldReport.state === 'closed' || oldReport.html_url === newReport.html_url) { continue } // If an old report is not assigned to someone we close it const shouldClose = !oldReport.assignees?.length let body = `➡️ [Newer report](${newReport.html_url})` if (shouldClose) { body += '\n\nClosing in favor of newer report since there are no assignees on this issue' } try { await octokit.rest.issues.createComment({ owner, repo, issue_number: oldReport.number, body, }) core.info(`Linked old report to new report via comment on old report: #${oldReport.number}.`) } catch (error) { core.setFailed(`Error commenting on previousReport, #${oldReport.number}`) throw error } if (shouldClose) { try { await octokit.rest.issues.update({ owner, repo, issue_number: oldReport.number, state: 'closed', }) core.info(`Closing old report: #${oldReport.number} because it doesn't have assignees`) } catch (error) { core.setFailed(`Error closing previousReport, #${oldReport.number}`) throw error } } } }