From 3312019f11ffe0067eef18fe9d66aa8dcb58b0f9 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 16 Jun 2023 16:58:42 +0200 Subject: [PATCH] chore(ci): update post release workflow (#1506) --- .github/scripts/label_related_issue.js | 37 +++--- .github/scripts/post_release.js | 154 +++++++++++++------------ 2 files changed, 103 insertions(+), 88 deletions(-) diff --git a/.github/scripts/label_related_issue.js b/.github/scripts/label_related_issue.js index e57f6621ba..57c938d8af 100644 --- a/.github/scripts/label_related_issue.js +++ b/.github/scripts/label_related_issue.js @@ -7,15 +7,16 @@ const { HANDLE_MAINTAINERS_TEAM, PR_IS_MERGED, RELATED_ISSUE_REGEX, -} = require("./constants"); + LABEL_RELEASED, +} = require('./constants'); module.exports = async ({ github, context, core }) => { if (IGNORE_AUTHORS.includes(PR_AUTHOR)) { - return core.notice("Author in IGNORE_AUTHORS list; skipping..."); + return core.notice('Author in IGNORE_AUTHORS list; skipping...'); } - if (PR_IS_MERGED == "false") { - return core.notice("Only merged PRs to avoid spam; skipping"); + if (PR_IS_MERGED == 'false') { + return core.notice('Only merged PRs to avoid spam; skipping'); } const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY); @@ -60,22 +61,28 @@ module.exports = async ({ github, context, core }) => { /** * Keep all labels except those that start with 'status/' or 'need-' or equal to 'help-wanted' * as those are contextual to issues still in progress. + * + * If the issue was already marked with the 'status/completed' label, then we'll keep that, otherwise + * we'll add the 'status/pending-release' label. */ + let hasCompletedLabel = false; const newLabels = currentLabels.data - .filter( - (label) => - !label.name.startsWith("status/") && - !label.name.startsWith("need-") && - label.name !== "help-wanted" - ) + .filter((label) => { + if (label.name === LABEL_RELEASED) { + hasCompletedLabel = true; + } + return ( + !label.name.startsWith('status/') && + !label.name.startsWith('need-') && + label.name !== 'help-wanted' + ); + }) .map((label) => label.name); - // Add the status/pending-release label - newLabels.push(LABEL_PENDING_RELEASE); + // Add the status/pending-release or status/completed label + newLabels.push(hasCompletedLabel ? LABEL_RELEASED : LABEL_PENDING_RELEASE); try { - core.info( - `Auto-labeling related issue ${issue} for release while removing 'status/*' and 'need-*' labels` - ); + core.info(`Auto-labeling related issue ${issue} completed`); return await github.rest.issues.setLabels({ issue_number: issue, owner: context.repo.owner, diff --git a/.github/scripts/post_release.js b/.github/scripts/post_release.js index 7eeebeb24b..3af2dccdc3 100644 --- a/.github/scripts/post_release.js +++ b/.github/scripts/post_release.js @@ -1,4 +1,4 @@ -const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require("./constants"); +const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require('./constants'); /** * Fetch issues using GitHub REST API @@ -12,26 +12,28 @@ const { LABEL_PENDING_RELEASE, LABEL_RELEASED } = require("./constants"); * @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client} */ const fetchIssues = async ({ - gh_client, - org, - repository, - state = "all", - label = LABEL_PENDING_RELEASE, + gh_client, + core, + org, + repository, + state = 'all', + label = LABEL_PENDING_RELEASE, }) => { - try { - const { data: issues } = await gh_client.rest.issues.listForRepo({ - owner: org, - repo: repository, - state: state, - labels: label, - }); - - return issues; - } catch (error) { - console.error(error); - throw new Error("Failed to fetch issues"); - } + try { + const { data: issues } = await gh_client.rest.issues.listForRepo({ + owner: org, + repo: repository, + state: state, + labels: label, + }); + return issues.filter( + (issue) => Object(issue).hasOwnProperty('pull_request') === false + ); + } catch (error) { + core.setFailed(error); + throw new Error('Failed to fetch issues'); + } }; /** @@ -44,68 +46,74 @@ const fetchIssues = async ({ * @see {@link https://octokit.github.io/rest.js/v18#usage|Octokit client} */ const notifyRelease = async ({ - gh_client, - owner, - repository, - release_version, + gh_client, + core, + owner, + repository, + release_version, }) => { - const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version.replace(/v/g, '')}`; - - const issues = await fetchIssues({ - gh_client: gh_client, - org: owner, - repository: repository, - state: "closed" - }); + const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version.replace( + /v/g, + '' + )}`; - issues.forEach(async (issue) => { - console.info(`Updating issue number ${issue.number}`); + const issues = await fetchIssues({ + gh_client: gh_client, + org: owner, + repository: repository, + state: 'closed', + }); - const comment = `This is now released under [${release_version}](${release_url}) version!`; - try { - await gh_client.rest.issues.createComment({ - owner: owner, - repo: repository, - body: comment, - issue_number: issue.number, - }); - } catch (error) { - console.error(error); - throw new Error(`Failed to update issue ${issue.number} about ${release_version} release`) - } + issues.forEach(async (issue) => { + core.info(`Updating issue number ${issue.number}`); + const comment = `This is now released under [${release_version}](${release_url}) version!`; + try { + await gh_client.rest.issues.createComment({ + owner: owner, + repo: repository, + body: comment, + issue_number: issue.number, + }); + } catch (error) { + core.setFailed(error); + throw new Error( + `Failed to update issue ${issue.number} about ${release_version} release` + ); + } - // Close issue and remove staged label; keep existing ones - const labels = issue.labels - .filter((label) => label.name != LABEL_PENDING_RELEASE) - .map((label) => label.name) - .push(LABEL_RELEASED); + // Remove staged label; keep existing ones + const labels = issue.labels + .filter((label) => label.name != LABEL_PENDING_RELEASE) + .map((label) => label.name); - try { - await gh_client.rest.issues.setLabels({ - repo: repository, - owner, - issue_number: issue.number, - labels, - }); - } catch (error) { - console.error(error); - throw new Error("Failed to close issue") - } + // Update labels including the released one + try { + await gh_client.rest.issues.setLabels({ + repo: repository, + owner, + issue_number: issue.number, + labels: [...labels, LABEL_RELEASED], + }); + } catch (error) { + core.setFailed(error); + throw new Error('Failed to label issue'); + } - console.info(`Issue number ${issue.number} closed and updated`); - }); + core.info(`Issue number ${issue.number} labeled`); + }); }; // context: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts -module.exports = async ({ github, context }) => { - const { RELEASE_VERSION } = process.env; - console.log(`Running post-release script for ${RELEASE_VERSION} version`); +module.exports = async ({ github, context, core }) => { + const { RELEASE_VERSION } = process.env; + core.info(`Running post-release script for ${RELEASE_VERSION} version`); - await notifyRelease({ - gh_client: github, - owner: context.repo.owner, - repository: context.repo.repo, - release_version: RELEASE_VERSION, - }); -}; \ No newline at end of file + await notifyRelease({ + gh_client: github, + core, + owner: context.repo.owner, + repository: context.repo.repo, + release_version: RELEASE_VERSION, + }); +};