Skip to content

Commit

Permalink
ci: update previous release detection
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <[email protected]>

Signed-off-by: Logan McAnsh <[email protected]>
  • Loading branch information
mcansh authored Jan 11, 2023
1 parent f7473cb commit 9953f73
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
41 changes: 15 additions & 26 deletions scripts/release/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
OWNER,
REPO,
PR_FILES_STARTS_WITH,
IS_NIGHTLY_RELEASE,
IS_STABLE_RELEASE,
AWAITING_RELEASE_LABEL,
} from "./constants";
import {
Expand Down Expand Up @@ -53,7 +53,7 @@ async function commentOnIssuesAndPrsAboutRelease() {
let prLabels = pr.labels.map((label) => label.name);
let prIsAwaitingRelease = prLabels.includes(AWAITING_RELEASE_LABEL);

if (!IS_NIGHTLY_RELEASE && prIsAwaitingRelease) {
if (IS_STABLE_RELEASE && prIsAwaitingRelease) {
promises.push(
removeLabel({ owner: OWNER, repo: REPO, issue: pr.number })
);
Expand All @@ -73,27 +73,19 @@ async function commentOnIssuesAndPrsAboutRelease() {

issuesCommentedOn.add(issue.number);
let issueUrl = getGitHubUrl("issue", issue.number);
console.log(`commenting on issue ${issueUrl}`);

if (IS_NIGHTLY_RELEASE || !prIsAwaitingRelease) {
console.log(`commenting on ${issueUrl}`);
promises.push(
commentOnIssue({
owner: OWNER,
repo: REPO,
issue: issue.number,
version: VERSION,
})
);
} else {
console.log(`commenting on and closing ${issueUrl}`);
promises.push(
commentOnIssue({
owner: OWNER,
repo: REPO,
issue: issue.number,
version: VERSION,
})
);
promises.push(
commentOnIssue({
owner: OWNER,
repo: REPO,
issue: issue.number,
version: VERSION,
})
);

if (IS_STABLE_RELEASE) {
console.log(`closing issue ${issueUrl}`);
promises.push(
closeIssue({ owner: OWNER, repo: REPO, issue: issue.number })
);
Expand All @@ -104,10 +96,7 @@ async function commentOnIssuesAndPrsAboutRelease() {
let result = await Promise.allSettled(promises);
let rejected = result.filter((r) => r.status === "rejected");
if (rejected.length > 0) {
console.log(
"🚨 failed to comment on some issues/prs - the most likely reason is they were issues that were turned into discussions, which don't have an api to comment with"
);
console.log(rejected);
console.error("🚨 failed to comment on some issues/prs", rejected);
}
}

Expand Down
3 changes: 2 additions & 1 deletion scripts/release/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cleanupRef, cleanupTagName, isNightly } from "./utils";
import { cleanupRef, cleanupTagName, isNightly, isStable } from "./utils";

if (!process.env.DEFAULT_BRANCH) {
throw new Error("DEFAULT_BRANCH is required");
Expand Down Expand Up @@ -32,3 +32,4 @@ export const NIGHTLY_BRANCH = process.env.NIGHTLY_BRANCH;
export const PR_FILES_STARTS_WITH = ["packages/"];
export const IS_NIGHTLY_RELEASE = isNightly(VERSION);
export const AWAITING_RELEASE_LABEL = "awaiting release";
export const IS_STABLE_RELEASE = isStable(VERSION);
70 changes: 48 additions & 22 deletions scripts/release/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
DEFAULT_BRANCH,
PACKAGE_VERSION_TO_FOLLOW,
AWAITING_RELEASE_LABEL,
IS_NIGHTLY_RELEASE,
IS_STABLE_RELEASE,
} from "./constants";
import { gql, graphqlWithAuth, octokit } from "./octokit";
import type { MinimalTag } from "./utils";
import { isNightly, isStable } from "./utils";
import { cleanupTagName } from "./utils";
import { checkIfStringStartsWith } from "./utils";

Expand Down Expand Up @@ -140,34 +143,32 @@ function getPreviousTagFromCurrentTag(

return { tag: tagName, date, isPrerelease };
})
.filter((v: any): v is MinimalTag => typeof v !== "undefined");
.filter((v: any): v is MinimalTag => typeof v !== "undefined")
.filter((tag) => {
if (IS_STABLE_RELEASE) return isStable(tag.tag);
let isNightlyTag = isNightly(tag.tag);
if (IS_NIGHTLY_RELEASE) return isNightlyTag;
return !isNightlyTag;
})
.sort((a, b) => {
if (IS_NIGHTLY_RELEASE) {
return b.date.getTime() - a.date.getTime();
}

return semver.rcompare(a.tag, b.tag);
});

let currentTagIndex = validTags.findIndex((tag) => tag.tag === currentTag);
let currentTagInfo: MinimalTag | undefined = validTags.at(currentTagIndex);
let previousTagInfo: MinimalTag | undefined;

if (!currentTagInfo) {
throw new Error(`Could not find last tag ${currentTag}`);
}

// if the currentTag was a stable tag, then we want to find the previous stable tag
if (!currentTagInfo.isPrerelease) {
validTags = validTags
.filter((tag) => !tag.isPrerelease)
.sort((a, b) => semver.rcompare(a.tag, b.tag));

currentTagIndex = validTags.findIndex((tag) => tag.tag === currentTag);
currentTagInfo = validTags.at(currentTagIndex);
if (!currentTagInfo) {
throw new Error(`Could not find last stable tag ${currentTag}`);
}
throw new Error(`Could not find tag ${currentTag}`);
}

previousTagInfo = validTags.at(currentTagIndex + 1);
if (!previousTagInfo) {
throw new Error(
`Could not find previous prerelease tag from ${currentTag}`
);
throw new Error(`Could not find previous tag from ${currentTag}`);
}

return {
Expand Down Expand Up @@ -232,21 +233,35 @@ interface GitHubGraphqlTag {
interface GitHubGraphqlTagResponse {
repository: {
refs: {
pageInfo: {
hasNextPage: boolean;
endCursor: string;
};
nodes: Array<GitHubGraphqlTag>;
};
};
}

async function getTags(owner: string, repo: string) {
async function getTags(
owner: string,
repo: string,
endCursor?: string,
nodes: Array<GitHubGraphqlTag> = []
): Promise<GitHubGraphqlTag[]> {
let response: GitHubGraphqlTagResponse = await graphqlWithAuth(
gql`
query GET_TAGS($owner: String!, $repo: String!) {
query GET_TAGS($owner: String!, $repo: String!, $endCursor: String) {
repository(owner: $owner, name: $repo) {
refs(
refPrefix: "refs/tags/"
first: 100
orderBy: { field: TAG_COMMIT_DATE, direction: DESC }
after: $endCursor
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
target {
Expand All @@ -267,15 +282,26 @@ async function getTags(owner: string, repo: string) {
}
}
`,
{ owner, repo }
{ owner, repo, endCursor }
);

return response.repository.refs.nodes.filter((node) => {
let filtered = response.repository.refs.nodes.filter((node) => {
return (
node.name.startsWith(PACKAGE_VERSION_TO_FOLLOW) ||
node.name.startsWith("v0.0.0-nightly-")
);
});

if (response.repository.refs.pageInfo.hasNextPage) {
console.log("has next page", response.repository.refs.pageInfo.endCursor);

return getTags(owner, repo, response.repository.refs.pageInfo.endCursor, [
...nodes,
...filtered,
]);
}

return [...nodes, ...filtered];
}

export async function getIssuesClosedByPullRequests(
Expand Down
6 changes: 6 additions & 0 deletions scripts/release/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as semver from "semver";

import { GITHUB_REPOSITORY, PACKAGE_VERSION_TO_FOLLOW } from "./constants";

export function checkIfStringStartsWith(
Expand Down Expand Up @@ -34,3 +36,7 @@ export function cleanupRef(ref: string) {
export function isNightly(tagName: string) {
return tagName.startsWith("v0.0.0-nightly-");
}

export function isStable(tagName: string) {
return semver.prerelease(tagName) === null;
}

0 comments on commit 9953f73

Please sign in to comment.