From d1efac9a6f7a9b408d4e8ff663a99c1fbac17b3f Mon Sep 17 00:00:00 2001 From: Rasmus Hedlund Rosted <68533799+Rasmus-Rosted@users.noreply.github.com> Date: Fri, 22 Jan 2021 19:10:35 +0100 Subject: [PATCH] Add possibility to specify owner and repository containing the branches to be deleted (#19) --- README.md | 10 ++++++++++ action.yml | 10 ++++++++++ main.js | 28 +++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 843a43a..f7a6517 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` diff --git a/action.yml b/action.yml index c79c8af..16b04ca 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 diff --git a/main.js b/main.js index a01fdd3..6391346 100644 --- a/main.js +++ b/main.js @@ -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) @@ -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)