Skip to content

Commit

Permalink
Merge pull request #1065 from ldaneliukas/patch-1
Browse files Browse the repository at this point in the history
Improvements for GitHub Enterprise
  • Loading branch information
crs-k authored Dec 6, 2023
2 parents 11fe52a + 7049856 commit c020f08
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Inputs are defined in [`action.yml`](action.yml). None are required.:
| `stale-branch-label` | Label to be applied to issues created for stale branches. <br>**As of v2, this _must_ be unique to this workflow**. | `stale branch 🗑️` |
| `compare-branches` | This compares each branch to the repo's default branch. <ul><li>When set to `info`, additional output describes if the current branch is ahead, behind, diverged, or identical to the default branch.<br>![image](https://user-images.githubusercontent.com/26232872/157590411-7c97806c-a509-4002-b7a5-a1e4a5da08eb.png)</li> <li>When set to `save`, this prevents branches from being deleted if they are ahead of or diverged from the default branch.</li> <li>When set to `off`, no additional calls are made.</li></ul> | off |
| `branches-filter-regex` | An optional Regex that will be used to filter branches from this action. | '' |
| `rate-limit` | If this is enabled, the action will stop if it exceeds 95% of the GitHub API rate limit. | false |

### Outputs
Outputs are defined in [`action.yml`](action.yml):
Expand Down Expand Up @@ -107,6 +108,7 @@ jobs:
stale-branch-label: 'stale branch 🗑️'
compare-branches: 'info'
branches-filter-regex: '^((?!dependabot))'
rate-limit: false


```
Expand Down
3 changes: 2 additions & 1 deletion __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const validInputs: Inputs = {
maxIssues: 30,
tagLastCommitter: true,
staleBranchLabel: 'stale branch 🗑️',
compareBranches: 'save'
compareBranches: 'save',
rateLimit: true
}

type ListIssuesResponseDataType = GetResponseTypeFromEndpointMethod<typeof githubActual.github.rest.issues.listForRepo>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/functions/get-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Get Context Function', () => {
await validateInputs()

expect(core.getInput).toHaveBeenCalledTimes(6)
expect(core.getBooleanInput).toHaveBeenCalledTimes(2)
expect(core.getBooleanInput).toHaveBeenCalledTimes(3)
})

test('Expect Failure: - TypeError', async () => {
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ inputs:
description: 'An optional Regex that will be used to filter branches from this action.'
required: false
default: ''
rate-limit:
description: 'If this is enabled, the action will stop if it exceeds 95% of the GitHub API rate limit.'
required: false
default: true
outputs:
deleted-branches:
description: 'List of all deleted branches.'
Expand Down
34 changes: 20 additions & 14 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.

3 changes: 2 additions & 1 deletion src/functions/get-committer-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function getRecentCommitLogin(sha: string): Promise<string> {
per_page: 1,
page: 1
})
lastCommitter = commitResponse.data.committer!.login
const commitData = commitResponse.data
lastCommitter = commitData.committer?.login || commitData.author?.login || commitData.commit?.committer?.name || commitData.commit?.author?.name
assert.ok(lastCommitter, 'Committer cannot be empty.')
} catch (err) {
if (err instanceof Error) {
Expand Down
3 changes: 3 additions & 0 deletions src/functions/get-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export async function validateInputs(): Promise<Inputs> {
throw new Error('branches-filter-regex must be 50 characters or less')
}

const inputRateLimit = core.getBooleanInput('rate-limit')

//Assign inputs
result.daysBeforeStale = inputDaysBeforeStale
result.daysBeforeDelete = inputDaysBeforeDelete
Expand All @@ -80,6 +82,7 @@ export async function validateInputs(): Promise<Inputs> {
result.staleBranchLabel = inputStaleBranchLabel
result.compareBranches = inputCompareBranches
result.branchesFilterRegex = branchesFilterRegex
result.rateLimit = inputRateLimit
} catch (err: unknown) {
if (err instanceof Error) {
core.setFailed(`Failed to validate inputs. Error: ${err.message}`)
Expand Down
27 changes: 15 additions & 12 deletions src/stale-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ export async function run(): Promise<void> {
// Assess Branches
for (const branchToCheck of branches) {
// Break if Rate Limit usage exceeds 95%
const rateLimit = await getRateLimit()
if (rateLimit.used > 95) {
core.info(logRateLimitBreak(rateLimit))
core.setFailed('Exiting to avoid rate limit violation.')
break
if (validInputs.rateLimit) {
const rateLimit = await getRateLimit()
if (rateLimit.used > 95) {
core.info(logRateLimitBreak(rateLimit))
core.setFailed('Exiting to avoid rate limit violation.')
break
}
}

//Get age of last commit, generate issue title, and filter existing issues to current branch
Expand Down Expand Up @@ -135,14 +137,15 @@ export async function run(): Promise<void> {
core.startGroup(logOrphanedIssues(existingIssue.length))
for (const issueToDelete of existingIssue) {
// Break if Rate Limit usage exceeds 95%
const rateLimit = await getRateLimit()
if (rateLimit.used > 95) {
core.info(logRateLimitBreak(rateLimit))
core.setFailed('Exiting to avoid rate limit violation.')
break
} else {
await closeIssue(issueToDelete.issueNumber)
if (validInputs.rateLimit) {
const rateLimit = await getRateLimit()
if (rateLimit.used > 95) {
core.info(logRateLimitBreak(rateLimit))
core.setFailed('Exiting to avoid rate limit violation.')
break
}
}
await closeIssue(issueToDelete.issueNumber)
}
core.endGroup()
}
Expand Down
9 changes: 9 additions & 0 deletions src/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ export interface Inputs {
* Must be 50 characters or less.
*/
branchesFilterRegex?: string

/**
* If this is enabled, the action will stop if it exceeds 95% of the GitHub API rate limit.
*
* Must meet YAML 1.2 "Core Schema" specification.
*
* Support boolean input list: `true | True | TRUE | false | False | FALSE`.
*/
rateLimit: boolean
}

0 comments on commit c020f08

Please sign in to comment.