-
Notifications
You must be signed in to change notification settings - Fork 20
Look up all branches on repo by prefix. Days threshold for deleting stale branches #17
base: master
Are you sure you want to change the base?
Changes from all commits
e64edcd
fa87b1b
5d4160b
a13ce81
5505b88
1b77d1f
a9206af
29d534e
f26746c
358e1ef
b51b129
b8afbb2
b22dd85
558dab8
ae1dd1f
8360c77
11b368c
520399f
081a563
ea69222
1ffa5cb
bc59236
fe1e3bd
a8a00e5
9ada54b
9e43595
d203a5e
785e21f
d27ca37
9989639
22ff4f6
18fb6a9
7e2d339
1bc1acf
00c31fe
4528196
a6a0d77
aff4884
845968b
05c69e0
dbef3fe
c31b1e9
79de717
937aea6
69b29a6
ab278b2
f8d364c
1f216dc
246fe32
72db792
66c6b45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Test negative days input | ||
|
||
on: | ||
push: | ||
branches: | ||
- '**' | ||
|
||
jobs: | ||
main: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Create branches | ||
run: | | ||
git checkout -b test-days-1 | ||
git push -f origin test-days-1 | ||
git checkout -b test-days-2 | ||
git push -f origin test-days-2 | ||
git checkout -b test-days-3 | ||
git push -f origin test-days-3 | ||
- name: Test action | ||
uses: ./ | ||
with: | ||
days: -1 | ||
prefix: test-days- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Test positive days input | ||
|
||
on: | ||
push: | ||
branches: | ||
- '**' | ||
|
||
jobs: | ||
main: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Create branches | ||
run: | | ||
git checkout -b positive-test-days-1 | ||
git push -f origin positive-test-days-1 | ||
git checkout -b positive-test-days-2 | ||
git push -f origin positive-test-days-2 | ||
git checkout -b positive-test-days-3 | ||
git push -f origin positive-test-days-3 | ||
- name: Test action | ||
uses: ./ | ||
with: | ||
days: 10 | ||
prefix: positive-test-days- | ||
- name: Delete branches (clean up) | ||
run: | | ||
git push -d origin positive-test-days-1 | ||
git push -d origin positive-test-days-2 | ||
git push -d origin positive-test-days-3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Test prefix | ||
|
||
on: | ||
push: | ||
branches: | ||
- "**" | ||
|
||
jobs: | ||
main: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Create branches | ||
run: | | ||
git checkout -b test-prefix-1 | ||
git push -f origin test-prefix-1 | ||
git checkout -b test-prefix-2 | ||
git push -f origin test-prefix-2 | ||
git checkout -b test-prefix-3 | ||
git push -f origin test-prefix-3 | ||
- name: Test action | ||
uses: ./ | ||
with: | ||
prefix: test-prefix- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,12 @@ inputs: | |
suffix: | ||
description: Additional suffix to append to every branch name | ||
required: false | ||
dry_run: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is any need for this input. Why it was added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows users of the action to see what branches it will delete without deleting it. This is meant to operate like most dry-run command line flags. |
||
description: Runs the action without deleting branhes | ||
required: false | ||
days: | ||
description: Delete branches that are older than the today's date minus number of days | ||
required: false | ||
runs: | ||
using: node12 | ||
main: main.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,21 @@ async function main() { | |
const branches = core.getInput("branches") | ||
const prefix = core.getInput("prefix") | ||
const suffix = core.getInput("suffix") | ||
const dryRun = core.getInput("dry_run") | ||
const days = core.getInput("days") | ||
|
||
const client = github.getOctokit(token) | ||
const repoName = github.context.payload.repository.name; | ||
const ownerName = github.context.payload.repository.owner.name; | ||
|
||
let branchesToDelete = branches ? branches.split(",") : [] | ||
|
||
let dateThreshold = new Date(); | ||
|
||
if (days) { | ||
dateThreshold.setDate(dateThreshold.getDate() - days); | ||
console.log("Branches with commits older than " + dateThreshold.toString() + " will be deleted."); | ||
} | ||
|
||
if (numbers) { | ||
for (const number of numbers.split(",")) { | ||
const pull = await client.pulls.get({ | ||
|
@@ -22,18 +32,57 @@ async function main() { | |
branchesToDelete.push(pull.data.head.ref) | ||
} | ||
} | ||
|
||
|
||
if (prefix) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why you did it like this. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can make suffix be in parity with the prefix. Honestly, I have not experienced repos that name git branches with suffixes, so I left it be |
||
const branchFunc = await client.paginate("GET /repos/{owner}/{repo}/branches", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does octokit not have any standard function for getting branches? Do we need to insert the endpoint here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could not identify a function on octokit that operates like this. That was my first attempt but no luck |
||
owner: ownerName, | ||
repo: repoName | ||
}) | ||
.then((branches) => { | ||
for (let branch of branches) { | ||
if (branch.name.substring(0, prefix.length) == prefix) { | ||
console.log("Adding branch: " + branch.name + " for deletion."); | ||
branchesToDelete.push(branch.name) | ||
} | ||
} | ||
}); | ||
} | ||
|
||
console.log("Starting the branch deletion..."); | ||
for (let branch of branchesToDelete) { | ||
if (prefix) | ||
branch = prefix + branch | ||
|
||
if (suffix) | ||
branch = branch + suffix | ||
console.log("==> Deleting \"" + branch + "\" branch") | ||
await client.git.deleteRef({ | ||
...github.context.repo, | ||
ref: "heads/" + branch | ||
}) | ||
|
||
let canDelete = true; | ||
if (days) { | ||
await client.request("GET /repos/{owner}/{repo}/branches/{branch}", { | ||
owner: ownerName, | ||
repo: repoName, | ||
branch: branch | ||
}) | ||
.then((ghBranch) => { | ||
let branchLastCommitDate = new Date(ghBranch.data.commit.commit.committer.date); | ||
if (branchLastCommitDate > dateThreshold) { | ||
console.log("Branch \"" + branch + "\" last commit date is " + branchLastCommitDate.toString() + ". It does not meet the threshold and will not be deleted."); | ||
canDelete = false; | ||
} | ||
}); | ||
} | ||
|
||
if (!canDelete) | ||
continue; | ||
|
||
console.log("==> Deleting \"" + branch + "\" branch"); | ||
|
||
if (!dryRun) { | ||
await client.git.deleteRef({ | ||
...github.context.repo, | ||
ref: "heads/" + branch | ||
}) | ||
} | ||
} | ||
console.log("Ending the branch deletion..."); | ||
} catch (error) { | ||
core.setFailed(error.message) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why removing prefix here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous prefix was replaced with lookups against all the repo branches. The previous implementation users of the action had to explicitly know the branch name.