Skip to content

Commit

Permalink
feat: add option to overwrite existing milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
benelan committed Apr 26, 2022
1 parent 64779bf commit 7ad020e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/add-milestone-close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
- uses: ./
with:
overwrite: true # remove this line to keep existing milestone
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
types: [opened]
branches: [master]
pull_request:
types: [opened]
types: [closed]
branches: [master]
jobs:
add:
Expand All @@ -25,6 +25,7 @@ jobs:
- uses: benelan/[email protected]
with:
farthest: true # remove this line to add the current milestone
overwrite: true # remove this line to keep an existing milestone
```
Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ inputs:
default: ${{ github.token }}
required: false
farthest:
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.'
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.'
required: false
default: false
default: 'false'
overwrite:
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.'
required: false
default: 'false'
runs:
using: 'node12'
main: 'dist/index.js'
22 changes: 10 additions & 12 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.

26 changes: 11 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,37 @@ async function run(): Promise<void> {
} = context

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

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

if (!!issue?.milestone || !!pull_request?.milestone) {
if (!overwrite && (issue?.milestone || pull_request?.milestone)) {
console.log(
'The issue or pull request already has a milestone, ending run.'
'The `overwrite` option is not enabled and the issue or pull request already has a milestone, ending run.'
)
return
}

const farthest = getBooleanInput('farthest')
const token = getInput('repo-token')
const octokit = getOctokit(token)

const {data: milestones} = await octokit.rest.issues.listMilestones({
...repo,
state: 'open',
sort: 'due_on',
per_page: 100,
direction: farthest ? 'desc' : 'asc'
})

if (milestones.length === 0) {
if (!milestones.length) {
console.log('There are no open milestones in this repo, ending run.')
return
}

const currentDate = new Date()
const currentDate = new Date(Date.now())
for (const milestone of milestones) {
if (milestone.due_on && new Date(milestone.due_on) > currentDate) {
await octokit.rest.issues.update({
Expand All @@ -57,6 +52,7 @@ async function run(): Promise<void> {
return
}
}
console.log('No matching milestone was found or added, ending run.')
} catch (e) {
if (e instanceof Error) {
setFailed(e.message)
Expand Down

0 comments on commit 7ad020e

Please sign in to comment.