Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/chromium-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import { getSupportedBranches } from './utils/get-supported-branches.js';
import { getContent } from './utils/github-utils.js';
import { getOctokit } from './utils/octokit.js';
import { roll } from './utils/roll.js';
import { ReposGetBranchResponseItem, ReposListBranchesResponseItem } from './types.js';
import { Branch } from './types.js';
import { Octokit } from '@octokit/rest';

type BranchItem = ReposGetBranchResponseItem | ReposListBranchesResponseItem;

// The outcome of a main branch roll: the Chromium version main is currently on
// (its landed DEPS version, not the version the open roll PR targets) and the
// release branches the roll PR covers with `target/N-x-y` labels.
Expand All @@ -22,7 +20,7 @@ interface MainRollResult {

async function rollReleaseBranch(
github: Octokit,
branch: BranchItem,
branch: Branch,
mainRoll?: MainRollResult | null,
) {
const d = debug(`roller/chromium:rollReleaseBranch('${branch.name}')`);
Expand Down Expand Up @@ -171,15 +169,7 @@ export async function handleChromiumCheck(target?: string): Promise<void> {
}
} else {
d('Fetching release branches for electron/electron');
const branches: ReposListBranchesResponseItem[] = await github.paginate(
github.repos.listBranches.endpoint.merge({
...REPOS.electron,
protected: true,
}),
);

const supported = getSupportedBranches(branches);
const releaseBranches = branches.filter((branch) => supported.includes(branch.name));
const releaseBranches = await getSupportedBranches(github);
d(`Found ${releaseBranches.length} release branches`);

// Roll main first, so that the release branches its roll PR covers with
Expand Down
11 changes: 1 addition & 10 deletions src/node-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { MAIN_BRANCH, REPOS, ROLL_TARGETS } from './constants.js';
import { getContent } from './utils/github-utils.js';
import { getOctokit } from './utils/octokit.js';
import { roll } from './utils/roll.js';
import { ReposListBranchesResponseItem } from './types.js';
import { getSupportedBranches } from './utils/get-supported-branches.js';
import { getLatestLTSVersion } from './utils/get-nodejs-lts.js';

Expand All @@ -15,15 +14,7 @@ export async function handleNodeCheck(target?: string): Promise<void> {
const github = await getOctokit();

d('Fetching release branches for electron/electron');
const branches: ReposListBranchesResponseItem[] = await github.paginate(
github.repos.listBranches.endpoint.merge({
...REPOS.electron,
protected: true,
}),
);

const supported = getSupportedBranches(branches, 3);
const releaseBranches = branches.filter((branch) => supported.includes(branch.name));
const releaseBranches = await getSupportedBranches(github, 3);
d(`Found ${releaseBranches.length} release branches`);

let failed = false;
Expand Down
13 changes: 8 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { RestEndpointMethodTypes } from '@octokit/rest';
import type { RestEndpointMethodTypes } from '@octokit/rest';

export type PullsGetResponseItem = RestEndpointMethodTypes['pulls']['get']['response']['data'];
export type PullsListResponseItem = RestEndpointMethodTypes['pulls']['list']['response']['data'][0];
export type ReposListBranchesResponseItem =
RestEndpointMethodTypes['repos']['listBranches']['response']['data'][0];
export type ReposGetBranchResponseItem =
RestEndpointMethodTypes['repos']['getBranch']['response']['data'];

export interface Branch {
name: string;
commit: {
sha: string;
};
}
83 changes: 72 additions & 11 deletions src/utils/get-supported-branches.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,91 @@
// Get array of currently supported branches
export function getSupportedBranches(
branches: { name: string }[],
import type { Octokit } from '@octokit/rest';
import type { Branch } from '../types.js';

/**
* Get array of currently supported branches
*/
export async function getSupportedBranches(
github: Octokit,
numSupportedVersions = 4,
): string[] {
const releaseBranches = branches
): Promise<Branch[]> {
if (numSupportedVersions < 1) {
throw new Error('numSupportedVersions must be greater than 0');
}

if (numSupportedVersions > 100) {
throw new Error('numSupportedVersions must be less than or equal to 100');
}

const branchRefs: { name: string; target: { oid: string } }[] = [];
let cursor: string | null = null;

while (true) {
const { repository } = await github.graphql<{
repository: {
refs: {
nodes: { name: string; target: { oid: string } }[];
pageInfo: { hasNextPage: boolean; endCursor: string | null };
};
};
}>(
`query ($owner: String!, $repo: String!, $branchQuery: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
refs(refPrefix: "refs/heads/", query: $branchQuery, first: 100, after: $cursor) {
nodes {
name
target {
... on Commit {
oid
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`,
{ owner: 'electron', repo: 'electron', branchQuery: '-x-y', cursor },
);

branchRefs.push(...repository.refs.nodes);

if (!repository.refs.pageInfo.hasNextPage) break;

cursor = repository.refs.pageInfo.endCursor;
if (cursor === null) {
throw new Error('GitHub returned no cursor for the next page of branches');
}
}

const releaseBranches = branchRefs
.filter((branch) => {
const releasePattern = /^(\d)+-(?:(?:[0-9]+-x$)|(?:x+-y$))$/;
return releasePattern.test(branch.name);
})
.map((b) => b.name);
.map((branch) => ({
name: branch.name,
commit: { sha: branch.target.oid },
}));

const filtered: Record<string, string> = {};
const filtered: Record<string, Branch> = {};
releaseBranches
.sort((a, b) => {
const aParts = a.split('-');
const bParts = b.split('-');
const aParts = a.name.split('-');
const bParts = b.name.split('-');
for (let i = 0; i < aParts.length; i += 1) {
if (aParts[i] === bParts[i]) continue;
return parseInt(aParts[i], 10) - parseInt(bParts[i], 10);
}
return 0;
})
.forEach((branch) => {
return (filtered[branch.split('-')[0]] = branch);
return (filtered[branch.name.split('-')[0]] = branch);
});

const values = Object.values(filtered);
return values.sort((a, b) => parseInt(a, 10) - parseInt(b, 10)).slice(-numSupportedVersions);
return values
.sort((a, b) => parseInt(a.name.split('-')[0], 10) - parseInt(b.name.split('-')[0], 10))
.slice(-numSupportedVersions);
}
10 changes: 2 additions & 8 deletions src/utils/get-target-branch-labels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Octokit } from '@octokit/rest';

import { REPOS } from '../constants.js';
import { ReposListBranchesResponseItem } from '../types.js';
import { getSupportedBranches } from './get-supported-branches.js';

export const ELECTRON_RELEASE_SCHEDULE_URL = 'https://releases.electronjs.org/schedule.json';
Expand Down Expand Up @@ -29,13 +27,9 @@ export async function getBranchesTrackedByMain(
}
const schedule = (await response.json()) as ReleaseScheduleEntry[];

const branches: ReposListBranchesResponseItem[] = await octokit.paginate(
octokit.repos.listBranches.endpoint.merge({
...REPOS.electron,
protected: true,
}),
const supported = await getSupportedBranches(octokit).then((branches) =>
branches.map((branch) => branch.name),
);
const supported = getSupportedBranches(branches);

return schedule
.filter(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/roll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ROLLER_BOT_LOGIN,
RollTarget,
} from '../constants.js';
import { ReposListBranchesResponseItem, PullsListResponseItem } from '../types.js';
import { Branch, PullsListResponseItem } from '../types.js';
import { getOctokit } from './octokit.js';
import { getPRText } from './pr-text.js';
import { updateDepsFile } from './update-deps.js';
Expand All @@ -21,7 +21,7 @@ import { getBranchesTrackedByMain } from './get-target-branch-labels.js';

interface RollParams {
rollTarget: RollTarget;
electronBranch: ReposListBranchesResponseItem;
electronBranch: Branch;
targetVersion: string;
prNumber?: number;
previousVersion?: string;
Expand Down
Loading