diff --git a/.github/workflows/negative-days-test.yml b/.github/workflows/negative-days-test.yml new file mode 100644 index 0000000..98ce684 --- /dev/null +++ b/.github/workflows/negative-days-test.yml @@ -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- diff --git a/.github/workflows/positive-days-test.yml b/.github/workflows/positive-days-test.yml new file mode 100644 index 0000000..739dc66 --- /dev/null +++ b/.github/workflows/positive-days-test.yml @@ -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 diff --git a/.github/workflows/prefix-test.yml b/.github/workflows/prefix-test.yml new file mode 100644 index 0000000..5c0bc63 --- /dev/null +++ b/.github/workflows/prefix-test.yml @@ -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- diff --git a/.github/workflows/test.yml b/.github/workflows/suffix-test.yml similarity index 50% rename from .github/workflows/test.yml rename to .github/workflows/suffix-test.yml index d09483e..ca923fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/suffix-test.yml @@ -1,9 +1,9 @@ -name: Test action +name: Suffix Test action on: push: branches: - - master + - "**" jobs: main: @@ -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 \ No newline at end of file + suffix: -suffix diff --git a/README.md b/README.md index 843a43a..39e9f4b 100644 --- a/README.md +++ b/README.md @@ -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. +Optionally one can provide a `suffix` strings that would be appended or prepended to every branch name. ## Usage @@ -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 ``` diff --git a/action.yml b/action.yml index c79c8af..11b9c90 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,12 @@ inputs: suffix: description: Additional suffix to append to every branch name required: false + dry_run: + 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 diff --git a/main.js b/main.js index a01fdd3..9e6e03a 100644 --- a/main.js +++ b/main.js @@ -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) { + const branchFunc = await client.paginate("GET /repos/{owner}/{repo}/branches", { + 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) }