Skip to content

Commit

Permalink
feat: paginate remote branches (#37)
Browse files Browse the repository at this point in the history
* feat: paginate remote branches

* pin editorconfig-checker to 5.1.8 (config v3.0.3)

* don't commit metafile
  • Loading branch information
tranhl authored Sep 12, 2024
1 parent fb65201 commit 7cec053
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .editorconfig-checker
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Version": "v3.0.1",
"Version": "v3.0.3",
"Verbose": false,
"Format": "",
"Debug": false,
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm exec lint-staged
npm run lint:ec
npm run test:ci
npm run build
35 changes: 20 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42834,10 +42834,10 @@ var remark2 = remark().use(remarkGfm).data("settings", {
// src/main.ts
async function main({
octokit,
mainBranch,
perennialBranches,
currentPullRequest,
pullRequests,
mainBranch,
perennialBranches,
skipSingleStacks
}) {
const repoGraph = new import_graphology.MultiDirectedGraph();
Expand Down Expand Up @@ -46861,18 +46861,21 @@ var inputs = {
mainBranch = mainBranchInput !== "" ? mainBranchInput : mainBranch;
return mainBranch;
},
async getPerennialBranches(octokit, config2, context3) {
const [{ data: unprotectedBranches }, { data: protectedBranches }] = await Promise.all([
octokit.rest.repos.listBranches({ ...context3.repo }),
octokit.rest.repos.listBranches({ ...context3.repo, protected: true })
]);
async getRemoteBranches(octokit, context3) {
const remoteBranches = await octokit.paginate(
"GET /repos/{owner}/{repo}/branches",
{
...context3.repo,
per_page: 100
},
(response) => response.data.map((branch) => branch.name)
);
core2.startGroup("Inputs: Remote branches");
core2.info(`Unprotected: ${JSON.stringify(unprotectedBranches)}`);
core2.info(`Protected: ${JSON.stringify(protectedBranches)}`);
core2.info(JSON.stringify(remoteBranches));
core2.endGroup();
const repoBranches = [...unprotectedBranches, ...protectedBranches].map(
(branch) => branch.name
);
return remoteBranches;
},
async getPerennialBranches(config2, remoteBranches) {
let explicitBranches = [];
explicitBranches = config2?.branches?.perennials ?? explicitBranches;
const perennialBranchesInput = core2.getMultilineInput("perennial-branches", {
Expand All @@ -46892,7 +46895,7 @@ var inputs = {
perennialRegex = perennialRegexInput !== "" ? perennialRegexInput : perennialRegex;
const perennialBranches = [
...explicitBranches,
...repoBranches.filter(
...remoteBranches.filter(
(branch) => perennialRegex ? RegExp(perennialRegex).test(branch) : false
)
];
Expand Down Expand Up @@ -46984,16 +46987,18 @@ async function run() {
return;
}
const octokit = github2.getOctokit(inputs.getToken());
const [mainBranch, perennialBranches, pullRequests] = await Promise.all([
const [mainBranch, remoteBranches, pullRequests] = await Promise.all([
inputs.getMainBranch(octokit, config, github2.context),
inputs.getPerennialBranches(octokit, config, github2.context),
inputs.getRemoteBranches(octokit, github2.context),
inputs.getPullRequests(octokit, github2.context)
]);
const perennialBranches = await inputs.getPerennialBranches(config, remoteBranches);
const context3 = {
octokit,
currentPullRequest: inputs.getCurrentPullRequest(github2.context),
pullRequests,
mainBranch,
remoteBranches,
perennialBranches,
skipSingleStacks: inputs.getSkipSingleStacks()
};
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"engineStrict": true,
"scripts": {
"dev": "npm run build -- --watch",
"build": "esbuild src/index.ts --outfile=dist/index.js --bundle --platform=node --target=node20 ",
"build": "esbuild src/index.ts --outfile=dist/index.js --bundle --platform=node --target=node20",
"test": "vitest",
"test:ci": "vitest run",
"lint:eslint": "eslint src --max-warnings=0",
Expand All @@ -30,7 +30,7 @@
"devDependencies": {
"@types/node": "^20.11.30",
"@vercel/style-guide": "^5.2.0",
"editorconfig-checker": "^5.1.5",
"editorconfig-checker": "5.1.8",
"esbuild": "^0.20.2",
"eslint": "^8.57.0",
"eslint-plugin-prettier": "^5.1.3",
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ async function run() {

const octokit = github.getOctokit(inputs.getToken())

const [mainBranch, perennialBranches, pullRequests] = await Promise.all([
const [mainBranch, remoteBranches, pullRequests] = await Promise.all([
inputs.getMainBranch(octokit, config, github.context),
inputs.getPerennialBranches(octokit, config, github.context),
inputs.getRemoteBranches(octokit, github.context),
inputs.getPullRequests(octokit, github.context),
])
const perennialBranches = await inputs.getPerennialBranches(config, remoteBranches)

const context = {
octokit,
currentPullRequest: inputs.getCurrentPullRequest(github.context),
pullRequests,
mainBranch,
remoteBranches,
perennialBranches,
skipSingleStacks: inputs.getSkipSingleStacks(),
}
Expand Down
39 changes: 4 additions & 35 deletions src/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,53 +91,22 @@ describe('getMainBranch', () => {
})

describe('getPerennialBranches', () => {
const octokit = {
rest: {
repos: {
listBranches: async () => ({
data: [
{
name: 'main',
commit: { sha: '', url: '' },
protection: false,
},
{
name: 'release-v1.0.0',
commit: { sha: '', url: '' },
protection: false,
},
{
name: 'v1.0.0',
commit: { sha: '', url: '' },
protection: false,
},
],
}),
},
},
} as unknown as Octokit
const remoteBranches = ['main', 'release-v1.0.0', 'v1.0.0']
const config: Config = {
branches: {
perennials: ['dev', 'staging', 'prod'],
'perennial-regex': '^release-.*$',
},
}
const context = {
repo: {},
} as unknown as typeof github.context

it('should default to no branches', async () => {
const perennialBranches = await inputs.getPerennialBranches(
octokit,
undefined,
context
)
const perennialBranches = await inputs.getPerennialBranches(undefined, remoteBranches)

expect(perennialBranches).toStrictEqual([])
})

it('should override default with config', async () => {
const perennialBranches = await inputs.getPerennialBranches(octokit, config, context)
const perennialBranches = await inputs.getPerennialBranches(config, remoteBranches)

expect(perennialBranches).toStrictEqual(['dev', 'staging', 'prod', 'release-v1.0.0'])
})
Expand All @@ -153,7 +122,7 @@ describe('getPerennialBranches', () => {
)
vi.stubEnv('INPUT_PERENNIAL-REGEX', '^v.*$')

const perennialBranches = await inputs.getPerennialBranches(octokit, config, context)
const perennialBranches = await inputs.getPerennialBranches(config, remoteBranches)

expect(perennialBranches).toStrictEqual(['test', 'uat', 'live', 'v1.0.0'])
})
Expand Down
33 changes: 17 additions & 16 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,27 @@ export const inputs = {
return mainBranch
},

async getPerennialBranches(
octokit: Octokit,
config: Config | undefined,
context: typeof github.context
): Promise<string[]> {
const [{ data: unprotectedBranches }, { data: protectedBranches }] =
await Promise.all([
octokit.rest.repos.listBranches({ ...context.repo }),
octokit.rest.repos.listBranches({ ...context.repo, protected: true }),
])
async getRemoteBranches(octokit: Octokit, context: typeof github.context) {
const remoteBranches = await octokit.paginate(
'GET /repos/{owner}/{repo}/branches',
{
...context.repo,
per_page: 100,
},
(response) => response.data.map((branch) => branch.name)
)

core.startGroup('Inputs: Remote branches')
core.info(`Unprotected: ${JSON.stringify(unprotectedBranches)}`)
core.info(`Protected: ${JSON.stringify(protectedBranches)}`)
core.info(JSON.stringify(remoteBranches))
core.endGroup()

const repoBranches = [...unprotectedBranches, ...protectedBranches].map(
(branch) => branch.name
)
return remoteBranches
},

async getPerennialBranches(
config: Config | undefined,
remoteBranches: string[]
): Promise<string[]> {
let explicitBranches: string[] = []
explicitBranches = config?.branches?.perennials ?? explicitBranches
const perennialBranchesInput = core.getMultilineInput('perennial-branches', {
Expand All @@ -87,7 +88,7 @@ export const inputs = {

const perennialBranches = [
...explicitBranches,
...repoBranches.filter((branch) =>
...remoteBranches.filter((branch) =>
perennialRegex ? RegExp(perennialRegex).test(branch) : false
),
]
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { remark } from './remark'

export async function main({
octokit,
mainBranch,
perennialBranches,
currentPullRequest,
pullRequests,
mainBranch,
perennialBranches,
skipSingleStacks,
}: Context) {
const repoGraph = new MultiDirectedGraph<StackNodeAttributes>()
Expand Down

0 comments on commit 7cec053

Please sign in to comment.