|
| 1 | +const STAGED_LABEL = "status/staged-next-release"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Fetch issues using GitHub REST API |
| 5 | + * |
| 6 | + * @param {string} Octokit - Pre-authenticated REST client |
| 7 | + * @param {string} org - GitHub Organization |
| 8 | + * @param {string} repository - GitHub repository |
| 9 | + * @param {string} state - GitHub issue state (open, closed) |
| 10 | + * @param {string} label - Comma-separated issue labels to fetch |
| 11 | + * @return {Object[]} issues - Array of issues matching params |
| 12 | + */ |
| 13 | +const fetchIssues = async ({ |
| 14 | + gh_client, |
| 15 | + org, |
| 16 | + repository, |
| 17 | + state = "open", |
| 18 | + label = STAGED_LABEL, |
| 19 | +}) => { |
| 20 | + |
| 21 | + try { |
| 22 | + const { data: issues } = await gh_client.rest.issues.listForRepo({ |
| 23 | + owner: org, |
| 24 | + repo: repository, |
| 25 | + state: state, |
| 26 | + labels: label, |
| 27 | + }); |
| 28 | + |
| 29 | + return issues; |
| 30 | + |
| 31 | + } catch (error) { |
| 32 | + console.error(error); |
| 33 | + throw new Error("Failed to fetch issues") |
| 34 | + } |
| 35 | + |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Notify new release and close staged GitHub issue |
| 40 | + * |
| 41 | + * @param {string} Octokit - Pre-authenticated REST client |
| 42 | + * @param {string} owner - GitHub Organization |
| 43 | + * @param {string} repository - GitHub repository |
| 44 | + * @param {string} release_version - GitHub Release version |
| 45 | + */ |
| 46 | +const notifyRelease = async ({ |
| 47 | + gh_client, |
| 48 | + owner, |
| 49 | + repository, |
| 50 | + release_version, |
| 51 | +}) => { |
| 52 | + const release_url = `https://github.com/${owner}/${repository}/releases/tag/v${release_version}`; |
| 53 | + |
| 54 | + const issues = await fetchIssues({ |
| 55 | + gh_client: github, |
| 56 | + org: context.repo.owner, |
| 57 | + repository: context.repo.repo, |
| 58 | + }); |
| 59 | + |
| 60 | + issues.forEach((issue) => { |
| 61 | + console.info(`Updating issue number ${issue.number}`); |
| 62 | + |
| 63 | + const comment = `This is now released under [${release_version}](${release_url}) version!`; |
| 64 | + try { |
| 65 | + await gh_client.rest.issues.createComment({ |
| 66 | + owner, |
| 67 | + repository, |
| 68 | + issue_number, |
| 69 | + comment, |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + console.error(error); |
| 73 | + throw new Error(`Failed to update issue ${issue.number} about ${release_version} release`) |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + // Close issue and remove staged label; keep existing ones |
| 78 | + const labels = issue.labels |
| 79 | + .filter((label) => label.name != STAGED_LABEL) |
| 80 | + .map((label) => label.name); |
| 81 | + |
| 82 | + try { |
| 83 | + await gh_client.rest.issues.update({ |
| 84 | + repo: repository, |
| 85 | + owner: owner, |
| 86 | + issue_number: issue.number, |
| 87 | + state: "closed", |
| 88 | + labels: labels, |
| 89 | + }); |
| 90 | + } catch (error) { |
| 91 | + console.error(error); |
| 92 | + throw new Error("Failed to close issue") |
| 93 | + } |
| 94 | + |
| 95 | + console.info(`Issue number ${issue.number} closed and updated`); |
| 96 | + }); |
| 97 | +}; |
| 98 | + |
| 99 | +// github: https://octokit.github.io/rest.js/v18#usage |
| 100 | +// context: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts |
| 101 | +// core: https://github.com/actions/toolkit/tree/main/packages/core |
| 102 | +module.exports = async ({ github, context, core }) => { |
| 103 | + core.exportVariable("author", commit.data.commit.author.email); |
| 104 | + const { RELEASE_TAG_VERSION } = process.env; |
| 105 | + console.log(`Running post-release script for ${RELEASE_TAG_VERSION} version`); |
| 106 | + |
| 107 | + await notifyRelease({ |
| 108 | + gh_client: github, |
| 109 | + issues: issues, |
| 110 | + owner: context.repo.owner, |
| 111 | + repository: context.repo.repo, |
| 112 | + release_version: RELEASE_TAG_VERSION, |
| 113 | + }); |
| 114 | +}; |
0 commit comments