Skip to content

Commit 7ad020e

Browse files
committed
feat: add option to overwrite existing milestone
1 parent 64779bf commit 7ad020e

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

.github/workflows/add-milestone-close.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v2
13-
- uses: ./
13+
- uses: ./
14+
with:
15+
overwrite: true # remove this line to keep existing milestone

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ on:
1616
types: [opened]
1717
branches: [master]
1818
pull_request:
19-
types: [opened]
19+
types: [closed]
2020
branches: [master]
2121
jobs:
2222
add:
@@ -25,6 +25,7 @@ jobs:
2525
- uses: benelan/[email protected]
2626
with:
2727
farthest: true # remove this line to add the current milestone
28+
overwrite: true # remove this line to keep an existing milestone
2829
```
2930
3031

action.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ inputs:
1010
default: ${{ github.token }}
1111
required: false
1212
farthest:
13-
description: 'Set this boolean to `true` if you want to add the milestone with the farthest due date. Defaults to `false` which adds the current milestone.'
13+
description: 'Set this boolean to `true` if you want to add the milestone with the farthest due date. Defaults to `false`, which adds the current milestone.'
1414
required: false
15-
default: false
15+
default: 'false'
16+
overwrite:
17+
description: 'Set this boolean to `true` if you want the action to overwrite existing milestones on issues. Defaults to `false`, which ends the run if the issue already has a milestone.'
18+
required: false
19+
default: 'false'
1620
runs:
1721
using: 'node12'
1822
main: 'dist/index.js'

dist/index.js

Lines changed: 10 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,37 @@ async function run(): Promise<void> {
1010
} = context
1111

1212
// https://github.blog/changelog/2021-02-19-github-actions-workflows-triggered-by-dependabot-prs-will-run-with-read-only-permissions/
13-
if (sender && sender.login === 'dependabot[bot]') {
13+
if (sender?.login === 'dependabot[bot]') {
1414
console.log('Dependabot created the pull request, ending run.')
1515
return
1616
}
1717

18-
if (!issue && !pull_request) {
19-
console.log(
20-
'The event that triggered this action was not a pull request or issue, ending run.'
21-
)
22-
return
23-
}
18+
const farthest = getBooleanInput('farthest')
19+
const overwrite = getBooleanInput('overwrite')
20+
const token = getInput('repo-token')
21+
const octokit = getOctokit(token)
2422

25-
if (!!issue?.milestone || !!pull_request?.milestone) {
23+
if (!overwrite && (issue?.milestone || pull_request?.milestone)) {
2624
console.log(
27-
'The issue or pull request already has a milestone, ending run.'
25+
'The `overwrite` option is not enabled and the issue or pull request already has a milestone, ending run.'
2826
)
2927
return
3028
}
3129

32-
const farthest = getBooleanInput('farthest')
33-
const token = getInput('repo-token')
34-
const octokit = getOctokit(token)
35-
3630
const {data: milestones} = await octokit.rest.issues.listMilestones({
3731
...repo,
3832
state: 'open',
3933
sort: 'due_on',
34+
per_page: 100,
4035
direction: farthest ? 'desc' : 'asc'
4136
})
4237

43-
if (milestones.length === 0) {
38+
if (!milestones.length) {
4439
console.log('There are no open milestones in this repo, ending run.')
4540
return
4641
}
4742

48-
const currentDate = new Date()
43+
const currentDate = new Date(Date.now())
4944
for (const milestone of milestones) {
5045
if (milestone.due_on && new Date(milestone.due_on) > currentDate) {
5146
await octokit.rest.issues.update({
@@ -57,6 +52,7 @@ async function run(): Promise<void> {
5752
return
5853
}
5954
}
55+
console.log('No matching milestone was found or added, ending run.')
6056
} catch (e) {
6157
if (e instanceof Error) {
6258
setFailed(e.message)

0 commit comments

Comments
 (0)