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

Commit

Permalink
Add possibility to specify owner and repository containing the branch…
Browse files Browse the repository at this point in the history
…es to be deleted (#19)
  • Loading branch information
Rasmus-Rosted authored Jan 22, 2021
1 parent 58dce56 commit d1efac9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.
If it is needed to specify which owner and repository the branches are located in, then the `owner` and `repository` can be provided as well.
If setting the `soft_fail` flag to `true` a warning will be written to the console, and the action will continue, instead of the action failing. The default is `false`.

## Usage

Expand All @@ -25,4 +27,12 @@ Optionally one can provide a `prefix` or `suffix` strings that would be appended
github_token: ${{github.token}}
branches: test
suffix: -done
- name: Delete branch in specific repository with a specific owner
uses: dawidd6/action-delete-branch@v3
with:
github_token: ${{github.token}}
owner: specific-owner
repository: specific-repository
branches: test
suffix: -done
```
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ inputs:
description: GitHub token
required: false
default: ${{github.token}}
owner:
description: Owner of the repository. The owner will be deducted from env vars if it is not set
required: false
repository:
description: The repository containing the branch(es) to be deleted. The repository name will be deducted from env vars if it is not set
required: false
branches:
description: Branches to delete (comma separated)
required: false
Expand All @@ -21,6 +27,10 @@ inputs:
suffix:
description: Additional suffix to append to every branch name
required: false
soft_fail:
description: If set to `true` the workflow will continue if a branch reference is not found
required: false
default: false
runs:
using: node12
main: main.js
28 changes: 23 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ async function main() {
try {
const token = core.getInput("github_token", { required: true })
const numbers = core.getInput("numbers")
const owner = core.getInput("owner")
const repository = core.getInput("repository")
const branches = core.getInput("branches")
const prefix = core.getInput("prefix")
const suffix = core.getInput("suffix")
const soft_fail = core.getInput("soft_fail")

const client = github.getOctokit(token)

Expand All @@ -23,16 +26,31 @@ async function main() {
}
}

let ownerOfRepository = owner ? owner : github.context.repo.owner
let repositoryContainingBranches = repository ? repository : github.context.repo.repo

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
})

console.log("==> Deleting \"" + ownerOfRepository + "/" + repositoryContainingBranches + "/" + branch + "\" branch")

try {
await client.git.deleteRef({
owner: ownerOfRepository,
repo: repositoryContainingBranches,
ref: "heads/" + branch
})
} catch (error) {
const shouldFailSoftly = (soft_fail === 'true');

if(shouldFailSoftly)
core.warning(error.message)
else
core.setFailed(error.message)
}
}
} catch (error) {
core.setFailed(error.message)
Expand Down

0 comments on commit d1efac9

Please sign in to comment.