Skip to content

Commit

Permalink
Add first version
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Apr 8, 2020
0 parents commit d2f0190
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/remove-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Remove branch

on:
create

jobs:
remove-branch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine:3.11

RUN apk add --no-cache bash curl jq

ADD entrypoint.sh /entrypoint.sh
ADD src /src

ENTRYPOINT ["/entrypoint.sh"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2020 Codely Enseña y Entretiene SL. https://codely.tv

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<p align="center">
<a href="http://codely.tv">
<img src="http://codely.tv/wp-content/uploads/2016/05/cropped-logo-codelyTV.png" width="192px" height="192px"/>
</a>
</p>

<h1 align="center">
🕊 No Branches
</h1>

<p align="center">
<a href="https://github.com/CodelyTV"><img src="https://img.shields.io/badge/CodelyTV-OS-green.svg?style=flat-square" alt="codely.tv"/></a>
<a href="http://pro.codely.tv"><img src="https://img.shields.io/badge/CodelyTV-PRO-black.svg?style=flat-square" alt="CodelyTV Courses"/></a>
<a href="https://github.com/marketplace/actions/no-branches"><img src="https://img.shields.io/github/v/release/CodelyTV/no-branches?style=flat-square" alt="GitHub Action version"></a>
</p>

<p align="center">
Useful if you do <code>trunk-based development</code> or <code>master-only git flow</code>
</pre>

## 🚀 Usage

Create a file named `remove-branch.yml` inside the `.github/workflows` directory and paste:

```yml
name: Remove branch

on:
create

jobs:
remove-branch:
runs-on: ubuntu-latest
steps:
- uses: codelytv/no-branches@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
## ⚖️ License
[MIT](LICENSE)
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'No Branches'
description: 'Automatically remove any branch'
branding:
icon: 'alert-triangle'
color: 'red'
inputs:
GITHUB_TOKEN:
description: 'GitHub token'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.GITHUB_TOKEN }}

16 changes: 16 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -uo pipefail

PROJECT_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

if [ "$PROJECT_HOME" == "/" ]; then
PROJECT_HOME=""
fi

export PROJECT_HOME

source "$PROJECT_HOME/src/main.sh"

main "$@"

exit $?
18 changes: 18 additions & 0 deletions src/ensure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

ensure::env_variable_exist() {
if [[ -z "${!1}" ]]; then
echoerr "The env variable $1 is required."
exit 1
fi
}

ensure::total_args() {
local -r received_args=$(( $# - 1 ))
local -r expected_args=$1

if ((received_args != expected_args)); then
echoerr "Illegal number of parameters, $expected_args expected but $received_args found"
exit 1
fi
}
14 changes: 14 additions & 0 deletions src/github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

GITHUB_API_URI="https://api.github.com"
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"

github::delete_ref() {
local -r ref=$1

curl -sSL \
-H "Authorization: token $GITHUB_TOKEN" \
-H "$GITHUB_API_HEADER" \
-X DELETE \
"$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/git/$ref"
}
13 changes: 13 additions & 0 deletions src/github_actions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

github_actions::get_pr_number() {
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
}

github_actions::commit_sha() {
jq --raw-output .after "$GITHUB_EVENT_PATH"
}

github_actions::print_all_data() {
cat "$GITHUB_EVENT_PATH"
}
29 changes: 29 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

source "$PROJECT_HOME/src/ensure.sh"
source "$PROJECT_HOME/src/github.sh"
source "$PROJECT_HOME/src/github_actions.sh"
source "$PROJECT_HOME/src/misc.sh"

main() {
ensure::env_variable_exist "GITHUB_REPOSITORY"
ensure::env_variable_exist "GITHUB_EVENT_PATH"
ensure::env_variable_exist "GITHUB_REF"
ensure::total_args 1 "$@"

export GITHUB_TOKEN="$1"

local -r branch_name="$GITHUB_REF"

log::message "$branch_name"

if str::contains "$branch_name" "refs/tags"; then
log::message "Tag not removed!"
else
github::delete_ref "$branch_name"

log::message "Branch removed!!"
fi

exit $?
}
28 changes: 28 additions & 0 deletions src/misc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

echoerr() {
echo "$@" 1>&2
}

log::message() {
echo "--------------"
echo "$@"
}

coll::join_by() {
local IFS="$1"
shift
echo "$*"
}

coll::map() {
local -r fn="$1"

for x in $(cat); do
"$fn" "$x"
done
}

str::contains() {
echo "$1" | grep -q "$2"
}

0 comments on commit d2f0190

Please sign in to comment.