Skip to content

Commit

Permalink
Merge pull request #328 from crs-k/improve-efficiency-of-calls
Browse files Browse the repository at this point in the history
reduce repeated api calls
  • Loading branch information
crs-k authored Feb 26, 2022
2 parents 1f11c32 + 4b66de5 commit 9c3dc3a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
days-before-stale: 2
days-before-delete: 10
days-before-delete: 60
max-issues: 60
comment-updates: true
tag-committer: true
Expand Down
77 changes: 61 additions & 16 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.

33 changes: 15 additions & 18 deletions src/stale-branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {logActiveBranch} from './functions/logging/log-active-branch'
import {logBranchGroupColor} from './functions/logging/log-branch-group-color'
import {logLastCommitColor} from './functions/logging/log-last-commit-color'
import {logMaxIssues} from './functions/logging/log-max-issues'
import {removeElementFromStringArray} from './utils/remove-element-from-array'
import {updateIssue} from './functions/update-issue'

export async function run(): Promise<void> {
Expand All @@ -22,39 +23,36 @@ export async function run(): Promise<void> {
//Collect Branches & budget
const branches = await getBranches()
let issueBudgetRemaining = await getIssueBudget()
const existingIssue = await getIssues()

// Assess Branches
for (const branchToCheck of branches) {
if (issueBudgetRemaining < 1) break

const lastCommitDate = await getRecentCommitDate(branchToCheck.commmitSha)
const lastCommitLogin = await getRecentCommitLogin(branchToCheck.commmitSha)
const currentDate = new Date().getTime()
const commitDate = new Date(lastCommitDate).getTime()
const commitAge = getDays(currentDate, commitDate)
const branchName = branchToCheck.branchName
const filteredIssue = existingIssue.data.filter(branchIssue => branchIssue.title === `[${branchName}] is STALE`)

core.startGroup(logBranchGroupColor(branchName, commitAge, daysBeforeStale, daysBeforeDelete))
core.info(logLastCommitColor(commitAge, daysBeforeStale, daysBeforeDelete))

//Create issues for stale branches
if (commitAge > daysBeforeStale) {
const existingIssue = await getIssues()

//Create new issue if existing issue is not found & issue budget is >0
if (!existingIssue.data.find(findIssue => findIssue.title === `[${branchName}] is STALE`) && issueBudgetRemaining > 0) {
await createIssue(branchName, commitAge, lastCommitLogin)
issueBudgetRemaining--

core.info(logMaxIssues(issueBudgetRemaining))
outputStales.push(branchName)
if (outputStales.includes(branchName) === false) {
outputStales.push(branchName)
}
}
}

//Close issues if a branch becomes active again
if (commitAge < daysBeforeStale) {
const existingIssue = await getIssues()
const filteredIssue = existingIssue.data.filter(branchIssue => branchIssue.title === `[${branchName}] is STALE`)
for (const issueToClose of filteredIssue) {
if (issueToClose.title === `[${branchName}] is STALE`) {
core.info(logActiveBranch(branchName))
Expand All @@ -65,36 +63,35 @@ export async function run(): Promise<void> {

//Update existing issues
if (commitAge > daysBeforeStale) {
const existingIssue = await getIssues()
//filter out issues that do not match this Action's title convention
const filteredIssue = existingIssue.data.filter(branchIssue => branchIssue.title === `[${branchName}] is STALE`)
//Update existing issues
for (const issueToUpdate of filteredIssue) {
if (issueToUpdate.title === `[${branchName}] is STALE`) {
await updateIssue(issueToUpdate.number, branchName, commitAge, lastCommitLogin)
outputStales.push(branchName)
if (outputStales.includes(branchName) === false) {
outputStales.push(branchName)
}
}
}
}

//Delete expired branches
if (commitAge > daysBeforeDelete) {
const existingIssue = await getIssues()
const filteredIssue = existingIssue.data.filter(branchIssue => branchIssue.title === `[${branchName}] is STALE`)
for (const issueToDelete of filteredIssue) {
if (issueToDelete.title === `[${branchName}] is STALE`) {
await deleteBranch(branchName)
await closeIssue(issueToDelete.number)
outputDeletes.push(branchName)
if (outputDeletes.includes(branchName) === false) {
outputDeletes.push(branchName)
removeElementFromStringArray(outputStales, branchName)
}
}
}
}
core.endGroup()
}
core.notice(`Stale Branches: ${outputStales.length}`)
core.notice(`Deleted Branches: ${outputDeletes.length}`)
core.setOutput('stale-branches', JSON.stringify(outputStales))
core.setOutput('deleted-branches', JSON.stringify(outputDeletes))
core.info(`Stale Branches: ${outputStales.length}`)
core.info(`Deleted Branches: ${outputDeletes.length}`)
} catch (error) {
if (error instanceof Error) core.setFailed(`Action failed. Error: ${error.message}`)
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/remove-element-from-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as core from '@actions/core'

export function removeElementFromStringArray(stringArray: string[], element: string): void {
try {
let index = 0
for (const value of stringArray) {
if (value === element) index = stringArray.indexOf(value)
stringArray.splice(index, 1)
}
} catch (err) {
if (err instanceof Error) core.info(`Error: ${err.message}`)
}
}

0 comments on commit 9c3dc3a

Please sign in to comment.