|
| 1 | +import { Octokit as RestOctokit } from "@octokit/rest"; |
| 2 | +import { paginateRest } from "@octokit/plugin-paginate-rest"; |
| 3 | +import { graphql } from "@octokit/graphql"; |
| 4 | + |
| 5 | +import { |
| 6 | + GITHUB_TOKEN, |
| 7 | + GITHUB_REPOSITORY, |
| 8 | + PR_FILES_STARTS_WITH, |
| 9 | +} from "./constants.mjs"; |
| 10 | + |
| 11 | +const graphqlWithAuth = graphql.defaults({ |
| 12 | + headers: { authorization: `token ${GITHUB_TOKEN}` }, |
| 13 | +}); |
| 14 | + |
| 15 | +const Octokit = RestOctokit.plugin(paginateRest); |
| 16 | +const octokit = new Octokit({ auth: GITHUB_TOKEN }); |
| 17 | + |
| 18 | +const gql = String.raw; |
| 19 | + |
| 20 | +export async function prsMergedSinceLast({ |
| 21 | + owner, |
| 22 | + repo, |
| 23 | + lastRelease: lastReleaseVersion, |
| 24 | +}) { |
| 25 | + let releases = await octokit.paginate(octokit.rest.repos.listReleases, { |
| 26 | + owner, |
| 27 | + repo, |
| 28 | + per_page: 100, |
| 29 | + }); |
| 30 | + |
| 31 | + let sorted = releases |
| 32 | + .sort((a, b) => { |
| 33 | + return new Date(b.published_at) - new Date(a.published_at); |
| 34 | + }) |
| 35 | + .filter((release) => { |
| 36 | + return release.tag_name.includes("experimental") === false; |
| 37 | + }); |
| 38 | + |
| 39 | + let lastReleaseIndex = sorted.findIndex((release) => { |
| 40 | + return release.tag_name === lastReleaseVersion; |
| 41 | + }); |
| 42 | + |
| 43 | + let lastRelease = sorted[lastReleaseIndex]; |
| 44 | + if (!lastRelease) { |
| 45 | + throw new Error( |
| 46 | + `Could not find last release ${lastRelease} in ${GITHUB_REPOSITORY}` |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + // if the lastRelease was a stable release, then we want to find the previous stable release |
| 51 | + let previousRelease; |
| 52 | + if (lastRelease.prerelease === false) { |
| 53 | + let stableReleases = sorted.filter((release) => { |
| 54 | + return release.prerelease === false; |
| 55 | + }); |
| 56 | + previousRelease = stableReleases.at(1); |
| 57 | + } else { |
| 58 | + previousRelease = sorted.at(lastReleaseIndex + 1); |
| 59 | + } |
| 60 | + |
| 61 | + if (!previousRelease) { |
| 62 | + throw new Error(`Could not find previous release in ${GITHUB_REPOSITORY}`); |
| 63 | + } |
| 64 | + |
| 65 | + let startDate = new Date(previousRelease.created_at); |
| 66 | + let endDate = new Date(lastRelease.created_at); |
| 67 | + |
| 68 | + let prs = await octokit.paginate(octokit.pulls.list, { |
| 69 | + owner, |
| 70 | + repo, |
| 71 | + state: "closed", |
| 72 | + sort: "updated", |
| 73 | + direction: "desc", |
| 74 | + }); |
| 75 | + |
| 76 | + let mergedPullRequestsSinceLastTag = prs.filter((pullRequest) => { |
| 77 | + if (!pullRequest.merged_at) return false; |
| 78 | + let mergedDate = new Date(pullRequest.merged_at); |
| 79 | + return mergedDate > startDate && mergedDate < endDate; |
| 80 | + }); |
| 81 | + |
| 82 | + let prsWithFiles = await Promise.all( |
| 83 | + mergedPullRequestsSinceLastTag.map(async (pr) => { |
| 84 | + let files = await octokit.paginate(octokit.pulls.listFiles, { |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + per_page: 100, |
| 88 | + pull_number: pr.number, |
| 89 | + }); |
| 90 | + |
| 91 | + return { |
| 92 | + ...pr, |
| 93 | + files, |
| 94 | + }; |
| 95 | + }) |
| 96 | + ); |
| 97 | + |
| 98 | + return { |
| 99 | + previousRelease: previousRelease.tag_name, |
| 100 | + merged: prsWithFiles.filter((pr) => { |
| 101 | + return pr.files.some((file) => { |
| 102 | + return checkIfStringStartsWith(file.filename, PR_FILES_STARTS_WITH); |
| 103 | + }); |
| 104 | + }), |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +export async function commentOnPullRequest({ owner, repo, pr, version }) { |
| 109 | + await octokit.issues.createComment({ |
| 110 | + owner, |
| 111 | + repo, |
| 112 | + issue_number: pr, |
| 113 | + body: `🤖 Hello there,\n\nWe just published version \`${version}\` which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!\n\nThanks!`, |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +export async function commentOnIssue({ owner, repo, issue, version }) { |
| 118 | + await octokit.issues.createComment({ |
| 119 | + owner, |
| 120 | + repo, |
| 121 | + issue_number: issue, |
| 122 | + body: `🤖 Hello there,\n\nWe just published version \`${version}\` which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!\n\nThanks!`, |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +async function getIssuesLinkedToPullRequest(prHtmlUrl, nodes = [], after) { |
| 127 | + let res = await graphqlWithAuth( |
| 128 | + gql` |
| 129 | + query GET_ISSUES_CLOSED_BY_PR($prHtmlUrl: URI!, $after: String) { |
| 130 | + resource(url: $prHtmlUrl) { |
| 131 | + ... on PullRequest { |
| 132 | + closingIssuesReferences(first: 100, after: $after) { |
| 133 | + nodes { |
| 134 | + number |
| 135 | + } |
| 136 | + pageInfo { |
| 137 | + hasNextPage |
| 138 | + endCursor |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + `, |
| 145 | + { prHtmlUrl, after } |
| 146 | + ); |
| 147 | + |
| 148 | + let newNodes = res?.resource?.closingIssuesReferences?.nodes ?? []; |
| 149 | + nodes.push(...newNodes); |
| 150 | + |
| 151 | + if (res?.resource?.closingIssuesReferences?.pageInfo?.hasNextPage) { |
| 152 | + return getIssuesLinkedToPullRequest( |
| 153 | + prHtmlUrl, |
| 154 | + nodes, |
| 155 | + res?.resource?.closingIssuesReferences?.pageInfo?.endCursor |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + return nodes; |
| 160 | +} |
| 161 | + |
| 162 | +export async function getIssuesClosedByPullRequests(prHtmlUrl, prBody) { |
| 163 | + let linked = await getIssuesLinkedToPullRequest(prHtmlUrl); |
| 164 | + if (!prBody) return linked; |
| 165 | + |
| 166 | + /** |
| 167 | + * This regex matches for one of github's issue references for auto linking an issue to a PR |
| 168 | + * as that only happens when the PR is sent to the default branch of the repo |
| 169 | + * https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword |
| 170 | + */ |
| 171 | + let regex = |
| 172 | + /(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s#([0-9]+)/gi; |
| 173 | + let matches = prBody.match(regex); |
| 174 | + if (!matches) return linked; |
| 175 | + |
| 176 | + let issues = matches.map((match) => { |
| 177 | + let [, issueNumber] = match.split(" #"); |
| 178 | + return { number: parseInt(issueNumber, 10) }; |
| 179 | + }); |
| 180 | + |
| 181 | + return [...linked, ...issues.filter((issue) => issue !== null)]; |
| 182 | +} |
| 183 | + |
| 184 | +function checkIfStringStartsWith(string, substrings) { |
| 185 | + return substrings.some((substr) => string.startsWith(substr)); |
| 186 | +} |
0 commit comments