Skip to content

Commit

Permalink
Merge pull request #230 from crs-k/comment-update-flag
Browse files Browse the repository at this point in the history
added input for enabling comments on issue updates
  • Loading branch information
crs-k authored Jan 24, 2022
2 parents 881cc5a + b1cf28f commit ef48ac6
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 55 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
days-before-stale: 2
#comment-updates: true

Build:
name: Build
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This Action automatically deletes stale branches.

When a branch has been inactive for the configured `days-before-stale` input, an issue is opened with the title `[branch-name] is STALE`. Each time the workflow runs, a comment will be added to the issue with updated days since last commit and days remaining until the branch will be deleted on the following workflow run. The time to delete is a function of inputs `days-before-delete` minus `days-before-stale`.
When a branch has been inactive for the configured `days-before-stale` input, an issue is opened with the title `[branch-name] is STALE`. The time to delete is a function of inputs `days-before-delete` minus `days-before-stale`.

* By default, a stale branch is defined as a branch that:
* has had no commits in the last 120 days.
Expand All @@ -30,6 +30,7 @@ Inputs are defined in [`action.yml`](action.yml):
| `repo-token` | `Yes`| Token to use to authenticate with GitHub API. [GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication#about-the-github_token-secret) suggested. | N/A |
| `days-before-stale` | `No` | Number of days a branch has been inactive before it is considered stale. | 120 days |
| `days-before-delete` | `No` | Number of days a branch has been inactive before it is deleted. | 180 days |
| `comment-updates` | `No` | If this is enabled, a comment with updated information will be added to existing issues each workflow run. | false |

### Outputs
Outputs are defined in [`action.yml`](action.yml):
Expand All @@ -48,7 +49,7 @@ name: Stale Branches

on:
schedule:
- cron: '30 1 * * *'
- cron: '0 6 * * 1-5'

jobs:
stale_branches:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ inputs:
description: 'Number of days a branch has been inactive before it is deleted. Default: 180.'
required: false
default: '180'
comment-updates:
description: 'If this is enabled, a comment with updated information will be added to existing issues each workflow run.'
required: false
default: false
outputs:
closed-branches:
description: 'List of all closed branches.'
Expand Down
53 changes: 28 additions & 25 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.

1 change: 1 addition & 0 deletions src/functions/get-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const github = getOctokit(repoToken)
export const {owner: owner, repo: repo} = context.repo
export const daysBeforeStale = Number(core.getInput('days-before-stale', {required: false}))
export const daysBeforeDelete = Number(core.getInput('days-before-delete', {required: false}))
export const commentUpdates = core.getBooleanInput('comment-updates', {required: false})
56 changes: 29 additions & 27 deletions src/functions/update-issue.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import * as assert from 'assert'
import * as core from '@actions/core'
import {daysBeforeDelete, github, owner, repo} from './get-context'
import {commentUpdates, daysBeforeDelete, github, owner, repo} from './get-context'

export async function updateIssue(
issueNumber: number,
branch: string,
commitAge: number
): Promise<string> {
let createdAt: string
let createdAt = ''
let commentUrl: string
const daysUntilDelete = Math.abs(commitAge - daysBeforeDelete)
try {
const issueResponse = await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: `${branch} has had no activity for ${commitAge.toString()} days. \r \r This branch will be automatically deleted in ${daysUntilDelete.toString()} days. \r \r This issue was last updated on ${new Date().toString()}`,
labels: [
{
name: 'stale branch 🗑️',
color: 'B60205',
description: 'Used by Stale Branches Action to label issues'
}
]
})

createdAt = issueResponse.data.created_at || ''
commentUrl = issueResponse.data.html_url || ''
assert.ok(createdAt, 'Created At cannot be empty')
core.info(`Issue #${issueNumber}: comment was created at ${createdAt}. ${commentUrl}`)
} catch (err) {
if (err instanceof Error)
core.info(
`No existing issue returned for issue number: ${issueNumber}. Description: ${err.message}`
)
createdAt = ''
}
if (commentUpdates === true) {
try {
const issueResponse = await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: `${branch} has had no activity for ${commitAge.toString()} days. \r \r This branch will be automatically deleted in ${daysUntilDelete.toString()} days. \r \r This issue was last updated on ${new Date().toString()}`,
labels: [
{
name: 'stale branch 🗑️',
color: 'B60205',
description: 'Used by Stale Branches Action to label issues'
}
]
})

createdAt = issueResponse.data.created_at || ''
commentUrl = issueResponse.data.html_url || ''
assert.ok(createdAt, 'Created At cannot be empty')
core.info(`Issue #${issueNumber}: comment was created at ${createdAt}. ${commentUrl}`)
} catch (err) {
if (err instanceof Error)
core.info(
`No existing issue returned for issue number: ${issueNumber}. Description: ${err.message}`
)
createdAt = ''
}
}
return createdAt
}

0 comments on commit ef48ac6

Please sign in to comment.