Skip to content

Commit d141e61

Browse files
authored
Merge pull request #21 from ziyak97:refactor/zj/update-model
refactor: ♻️ switch to query + async iterator model
2 parents b7c516b + cb2b3ae commit d141e61

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

scripts/startRelease.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,49 @@ const semanticVersionType = process.argv[3];
1919
* @typedef {Object} Release
2020
* @property {string} tag_name - The tag name of the release
2121
* @property {string} published_at - The date and time when the release was published
22+
* @property {string} name - The name of the release
23+
* @property {boolean} prerelease - Whether the release is a prerelease
2224
*/
2325

2426
/**
25-
* Retrieves releases for a repository.
27+
* Retrieves lastest stable release for a repository.
2628
*
2729
* @param {string} owner - The owner of the repository
2830
* @param {string} repo - The name of the repository
2931
* @returns {Promise<Array<Release>>} An array of releases for the repository
3032
*/
31-
async function getReleases(owner, repo) {
33+
async function getLastStableRelease(owner, repo) {
34+
const iterator = octokit.paginate.iterator(octokit.rest.repos.listReleases, {
35+
owner,
36+
repo,
37+
per_page: 100,
38+
});
39+
40+
for await (const { data: releases } of iterator) {
41+
for (const release of releases) {
42+
if (!release.prerelease) {
43+
return release;
44+
}
45+
}
46+
}
47+
}
48+
49+
/**
50+
* Get the latest release for a given repository.
51+
* @async
52+
* @function
53+
* @param {string} owner - The owner of the repository.
54+
* @param {string} repo - The name of the repository.
55+
* @returns {Promise<Release>} - A Promise that resolves to the latest release object.
56+
*/
57+
async function getLastestRelease(owner, repo) {
3258
const releases = await octokit.paginate(octokit.repos.listReleases, {
3359
owner,
3460
repo,
61+
per_page: 1,
3562
});
36-
return releases;
63+
64+
return releases[0];
3765
}
3866

3967
/**
@@ -45,14 +73,9 @@ async function getReleases(owner, repo) {
4573
* @returns {Promise<Array<PullRequest>>} An array of merged pull requests for the repository
4674
*/
4775
async function getMergedPullRequests(owner, repo, published_at) {
48-
const pullRequests = await octokit.paginate(octokit.pulls.list, {
49-
owner,
50-
repo,
51-
state: "closed",
52-
});
53-
const mergedPullRequests = pullRequests.filter(
54-
(pr) => pr.merged_at && new Date(pr.merged_at) > new Date(published_at || 0)
55-
);
76+
const query = `repo:${owner}/${repo} is:pr is:merged updated:>${published_at}`;
77+
const response = await octokit.search.issuesAndPullRequests({ q: query });
78+
const mergedPullRequests = response.data.items;
5679
return mergedPullRequests;
5780
}
5881

@@ -171,8 +194,8 @@ function generateContributorsList(mergedPullRequests) {
171194
async function createCanaryRelease() {
172195
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
173196

174-
const releases = await getReleases(owner, repo);
175-
const latestRelease = releases[0];
197+
const latestRelease = await getLastestRelease(owner, repo);
198+
176199
let tag_name = `v0.0.0-canary.0`;
177200
let releaseNotes = "";
178201
if (latestRelease) {
@@ -181,15 +204,9 @@ async function createCanaryRelease() {
181204
const major = parseInt(match[1].substring(1));
182205
const minor = parseInt(match[2]);
183206
const patch = parseInt(match[3]);
184-
const canary = latestRelease.tag_name.includes("canary")
185-
? parseInt(
186-
latestRelease.tag_name.substring(
187-
latestRelease.tag_name.lastIndexOf(".") + 1
188-
)
189-
)
190-
: -1;
191-
192-
if (canary >= 0) {
207+
const canary = latestRelease.prerelease;
208+
209+
if (canary) {
193210
tag_name = `v${major}.${minor}.${patch}-canary.${canary + 1}`;
194211
} else {
195212
// Increment version number based on semantic version type
@@ -264,31 +281,20 @@ async function createCanaryRelease() {
264281
async function createRelease() {
265282
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
266283

267-
const releases = await getReleases(owner, repo);
268-
const latestCanaryRelease = releases.find((release) =>
269-
release.tag_name.includes("canary")
270-
);
284+
const latestRelease = await getLastestRelease(owner, repo);
285+
const latestCanaryRelease = latestRelease.prerelease ? latestRelease : null;
271286

272287
if (!latestCanaryRelease) {
273288
console.log("No canary releases found for repository");
274289
return;
275290
}
276291

277-
const tag_name = latestCanaryRelease.tag_name.split("-canary")[0];
278-
const name = `${tag_name}`;
279-
280-
// Get all canary releases of the same version
281-
const version = tag_name.substring(1);
282-
const canaryReleases = releases.filter((release) =>
283-
new RegExp(`^v${version}(-canary\\.\\d*)?$`).test(release.tag_name)
284-
);
292+
const lastStableRelease = await getLastStableRelease(owner, repo);
285293

286-
// Get merged pull requests between first canary release and new release
287-
const firstCanaryRelease = canaryReleases[canaryReleases.length - 1];
288294
const mergedPullRequests = await getMergedPullRequests(
289295
owner,
290296
repo,
291-
firstCanaryRelease.published_at
297+
lastStableRelease.published_at
292298
);
293299

294300
// Guard clause: No merged pull requests

0 commit comments

Comments
 (0)