Skip to content

Commit

Permalink
Merge pull request #285 from crs-k/pagination
Browse files Browse the repository at this point in the history
feature: pagination to handle repos with >100 branches
  • Loading branch information
crs-k authored Feb 19, 2022
2 parents 8a47a82 + d101816 commit f5b60c5
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 61 deletions.
7 changes: 7 additions & 0 deletions __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {PaginateInterface} from '@octokit/plugin-paginate-rest'

export const context = {
repo: {
owner: 'owner',
Expand Down Expand Up @@ -44,6 +46,8 @@ let createComment = jest.fn().mockReturnValue({
data: {id: 1, owner: 'owner', repo: 'repo'}
})

let iterator = jest.fn().mockReturnValue(listBranches)

const github = {
rest: {
git: {deleteRef},
Expand All @@ -58,6 +62,9 @@ const github = {
listBranches,
getCommit
}
},
paginate: {
iterator
}
}

Expand Down
12 changes: 3 additions & 9 deletions __tests__/functions/get-branches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ import {getBranches} from '../../src/functions/get-branches'
import {github} from '../../src/functions/get-context'

describe('Get Branches Function', () => {
test('getBranches endpoint is called', async () => {
test('github.paginate.iterator endpoint is called', async () => {
await getBranches()

expect(github.rest.repos.listBranches).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
protected: false,
per_page: 100,
page: 1
})
expect(github.paginate.iterator).toHaveBeenCalled()
})

test('Action fails elegantly', async () => {
Expand All @@ -30,7 +24,7 @@ describe('Get Branches Function', () => {

await getBranches()
expect(core.setFailed).toHaveBeenCalledWith(
`Failed to retrieve branches for repo. Error: Response cannot be empty.`
`Failed to retrieve branches for repo. Error: o[Symbol.iterator] is not a function`
)
expect(core.setFailed).toHaveBeenCalledWith(`Failed to retrieve branches for repo.`)
})
Expand Down
2 changes: 1 addition & 1 deletion __tests__/functions/get-issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const assert = require('assert')
import {getIssues} from '../../src/functions/get-issues'
import {github} from '../../src/functions/get-context'

describe('Get Commits Function', () => {
describe('Get Issues Function', () => {
beforeEach(() => {
jest.clearAllMocks()
})
Expand Down
2 changes: 1 addition & 1 deletion __tests__/functions/get-time.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as timeFuncs from '../../src/functions/get-time'

describe('Time Functions', () => {
describe('Get Time Functions', () => {
let startDate = new Date('2022-01-24T01:45:30.000Z')
let endDate = new Date('2022-01-22T01:45:30.000Z')

Expand Down
2 changes: 1 addition & 1 deletion __tests__/functions/update-issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let branchName = 'test'
let commitAge = 100
let lastCommitter = 'crs-k'

describe('Get Commits Function', () => {
describe('Update Issue Function', () => {
test('updateIssue endpoint is called with tag committer enabled', async () => {
Object.defineProperty(context, 'commentUpdates', {value: true})
Object.defineProperty(context, 'tagLastCommitter', {value: true})
Expand Down
36 changes: 28 additions & 8 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sourcemap-register.js

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions package-lock.json

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

31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@
"author": "",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0",
"@octokit/types": "^6.34.0",
"@types/semver": "^7.3.9",
"assert": "^2.0.0",
"@actions/core": "1.6.0",
"@actions/github": "5.0.0",
"@octokit/plugin-paginate-rest": "2.17.0",
"@octokit/types": "6.34.0",
"@types/semver": "7.3.9",
"assert": "2.0.0",
"node-fetch": ">=3.1.1"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.18",
"@typescript-eslint/parser": "^5.12.0",
"@vercel/ncc": "^0.33.3",
"eslint": "^8.9.0",
"eslint-plugin-github": "^4.3.5",
"eslint-plugin-jest": "^26.1.1",
"js-yaml": "^4.1.0",
"@types/jest": "27.4.0",
"@types/node": "17.0.18",
"@typescript-eslint/parser": "5.12.0",
"@vercel/ncc": "0.33.3",
"eslint": "8.9.0",
"eslint-plugin-github": "4.3.5",
"eslint-plugin-jest": "26.1.1",
"js-yaml": "4.1.0",
"prettier": "2.5.1",
"ts-jest": "^27.1.3",
"typescript": "^4.5.5"
"ts-jest": "27.1.3",
"typescript": "4.5.5"
}
}
21 changes: 13 additions & 8 deletions src/functions/get-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ type ListBranchesResponseDataType = GetResponseTypeFromEndpointMethod<
>

export async function getBranches(): Promise<ListBranchesResponseDataType> {
let branches: ListBranchesResponseDataType
let branches = {} as ListBranchesResponseDataType

try {
// Get info from the most recent release
const response = await github.rest.repos.listBranches({
const parameters = {
owner,
repo,
protected: false,
per_page: 100,
page: 1
})
branches = response
per_page: 100
}

assert.ok(response, 'Response cannot be empty.')
for await (const response of github.paginate.iterator(
github.rest.repos.listBranches,
parameters
)) {
// do whatever you want with each response, break out of the loop, etc.
branches = response
core.info(`${branches.data.length} branches found`)
assert.ok(response, 'Response cannot be empty.')
}
} catch (err) {
if (err instanceof Error) {
core.setFailed(`Failed to retrieve branches for ${repo}. Error: ${err.message}`)
Expand Down

0 comments on commit f5b60c5

Please sign in to comment.