Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Look up all branches on repo by prefix. Days threshold for deleting stale branches #17

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
e64edcd
Look up all the branches by prefix and load them up for deletion
bryanjebyrd Dec 19, 2020
fa87b1b
Update action.yml
bryanjebyrd Dec 19, 2020
5d4160b
add safeguards for owner and repo to be present
bryanjebyrd Dec 19, 2020
a13ce81
Create test-prefix-owner-repo.yml
bryanjebyrd Dec 19, 2020
5505b88
Update test-prefix-owner-repo.yml
bryanjebyrd Dec 19, 2020
1b77d1f
different starts with function call
bryanjebyrd Dec 19, 2020
a9206af
fixing the string function on an object :facepalm:
bryanjebyrd Dec 19, 2020
29d534e
removing log statement
bryanjebyrd Dec 19, 2020
f26746c
sleep it 30 to not clash with other action
bryanjebyrd Dec 19, 2020
358e1ef
make test branch names completely different
bryanjebyrd Dec 19, 2020
b51b129
adding some logging for more visual
bryanjebyrd Dec 19, 2020
b8afbb2
forgot the await :facepalm:
bryanjebyrd Dec 19, 2020
b22dd85
use standard prefix + branch when owner and repo is not present
bryanjebyrd Dec 19, 2020
558dab8
Update README.md
bryanjebyrd Dec 19, 2020
ae1dd1f
adding test branches
bryanjebyrd Dec 19, 2020
8360c77
add dry-run capability
bryanjebyrd Dec 20, 2020
11b368c
fixing syntax error
bryanjebyrd Dec 20, 2020
520399f
attempting to use github context
bryanjebyrd Dec 20, 2020
081a563
fixing the call to the github context
bryanjebyrd Dec 20, 2020
ea69222
removing owner and repo input, got it from the ctx obj
bryanjebyrd Dec 20, 2020
1ffa5cb
fixing another syntax error
bryanjebyrd Dec 20, 2020
bc59236
adding dry_run input
bryanjebyrd Dec 20, 2020
fe1e3bd
Removing repo and owner from readme
bryanjebyrd Dec 20, 2020
a8a00e5
removing prefix check in the delete loop
bryanjebyrd Dec 20, 2020
9ada54b
removing unneeded inputs
bryanjebyrd Dec 20, 2020
9e43595
fixing dry_run input
bryanjebyrd Dec 20, 2020
d203a5e
Update and rename test.yml to suffix-test.yml
bryanjebyrd Dec 20, 2020
785e21f
Rename test-prefix-owner-repo.yml to prefix-test.yml
bryanjebyrd Dec 20, 2020
d27ca37
Update action.yml
bryanjebyrd Dec 20, 2020
9989639
adding days input
bryanjebyrd Dec 20, 2020
22ff4f6
completed days functionality...now to test
bryanjebyrd Dec 20, 2020
18fb6a9
Create days-test.yml
bryanjebyrd Dec 20, 2020
7e2d339
Update prefix-test.yml
bryanjebyrd Dec 20, 2020
1bc1acf
target all branches for action
bryanjebyrd Dec 20, 2020
00c31fe
creating test to validate days input
bryanjebyrd Dec 20, 2020
4528196
updating test
bryanjebyrd Dec 20, 2020
a6a0d77
add logging for visual aid
bryanjebyrd Dec 20, 2020
aff4884
fixing the chaining in the json return obj
bryanjebyrd Dec 20, 2020
845968b
remove logging
bryanjebyrd Dec 20, 2020
05c69e0
Update and rename days-test.yml to negative-days-test.yml
bryanjebyrd Dec 20, 2020
dbef3fe
Create positive-days-test.yml
bryanjebyrd Dec 20, 2020
c31b1e9
adding logging for visual
bryanjebyrd Dec 20, 2020
79de717
run on all branches
bryanjebyrd Dec 20, 2020
937aea6
run on all branches
bryanjebyrd Dec 20, 2020
69b29a6
more visual aid with logs
bryanjebyrd Dec 20, 2020
ab278b2
convert the string date to date for compare
bryanjebyrd Dec 20, 2020
f8d364c
logging coming up.
bryanjebyrd Dec 20, 2020
1f216dc
fixing the date comparison
bryanjebyrd Dec 20, 2020
246fe32
moving around the logging to make the output cleaner
bryanjebyrd Dec 20, 2020
72db792
adding days example
bryanjebyrd Dec 20, 2020
66c6b45
Merge pull request #1 from bryanjebyrd/days-input
bryanjebyrd Dec 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/negative-days-test.yml
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-
31 changes: 31 additions & 0 deletions .github/workflows/positive-days-test.yml
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
25 changes: 25 additions & 0 deletions .github/workflows/prefix-test.yml
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-
15 changes: 7 additions & 8 deletions .github/workflows/test.yml → .github/workflows/suffix-test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Test action
name: Suffix Test action

on:
push:
branches:
- master
- "**"

jobs:
main:
Expand All @@ -13,13 +13,12 @@ jobs:
uses: actions/checkout@v2
- name: Create branches
run: |
git checkout -b prefix-test1-suffix
git push -f origin prefix-test1-suffix
git checkout -b prefix-test2-suffix
git push -f origin prefix-test2-suffix
git checkout -b test1-suffix
git push -f origin test1-suffix
git checkout -b test2-suffix
git push -f origin test2-suffix
- name: Test action
uses: ./
with:
branches: test1,test2
prefix: prefix-
suffix: -suffix
suffix: -suffix
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Delete multiple branches GitHub Action

An action that deletes multiple branches from repository.
Optionally one can provide a `prefix` or `suffix` strings that would be appended or prepended to every branch name.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing prefix here?

Copy link
Author

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.

Optionally one can provide a `suffix` strings that would be appended or prepended to every branch name.

## Usage

Expand All @@ -25,4 +25,9 @@ Optionally one can provide a `prefix` or `suffix` strings that would be appended
github_token: ${{github.token}}
branches: test
suffix: -done
- name: Delete branches older than 10 days
uses: dawidd6/action-delete-branch@v3
with:
github_token: ${{github.token}}
days: 10
```
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ inputs:
suffix:
description: Additional suffix to append to every branch name
required: false
dry_run:
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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
67 changes: 58 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -22,18 +32,57 @@ async function main() {
branchesToDelete.push(pull.data.head.ref)
}
}


if (prefix) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why you did it like this. Why if prefix here and not later, how it was done before? Why prefix is handled different than suffix?

Copy link
Author

Choose a reason for hiding this comment

The 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", {
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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)
}
Expand Down