Skip to content

Commit d2f0190

Browse files
committed
Add first version
0 parents  commit d2f0190

File tree

11 files changed

+217
-0
lines changed

11 files changed

+217
-0
lines changed

.github/workflows/remove-branch.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Remove branch
2+
3+
on:
4+
create
5+
6+
jobs:
7+
remove-branch:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v1
11+
- uses: ./
12+
with:
13+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM alpine:3.11
2+
3+
RUN apk add --no-cache bash curl jq
4+
5+
ADD entrypoint.sh /entrypoint.sh
6+
ADD src /src
7+
8+
ENTRYPOINT ["/entrypoint.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2020 Codely Enseña y Entretiene SL. https://codely.tv
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p align="center">
2+
<a href="http://codely.tv">
3+
<img src="http://codely.tv/wp-content/uploads/2016/05/cropped-logo-codelyTV.png" width="192px" height="192px"/>
4+
</a>
5+
</p>
6+
7+
<h1 align="center">
8+
🕊 No Branches
9+
</h1>
10+
11+
<p align="center">
12+
<a href="https://github.com/CodelyTV"><img src="https://img.shields.io/badge/CodelyTV-OS-green.svg?style=flat-square" alt="codely.tv"/></a>
13+
<a href="http://pro.codely.tv"><img src="https://img.shields.io/badge/CodelyTV-PRO-black.svg?style=flat-square" alt="CodelyTV Courses"/></a>
14+
<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>
15+
</p>
16+
17+
<p align="center">
18+
Useful if you do <code>trunk-based development</code> or <code>master-only git flow</code>
19+
</pre>
20+
21+
## 🚀 Usage
22+
23+
Create a file named `remove-branch.yml` inside the `.github/workflows` directory and paste:
24+
25+
```yml
26+
name: Remove branch
27+
28+
on:
29+
create
30+
31+
jobs:
32+
remove-branch:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: codelytv/no-branches@v1
36+
with:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
```
39+
40+
## ⚖️ License
41+
42+
[MIT](LICENSE)

action.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: 'No Branches'
2+
description: 'Automatically remove any branch'
3+
branding:
4+
icon: 'alert-triangle'
5+
color: 'red'
6+
inputs:
7+
GITHUB_TOKEN:
8+
description: 'GitHub token'
9+
required: true
10+
runs:
11+
using: 'docker'
12+
image: 'Dockerfile'
13+
args:
14+
- ${{ inputs.GITHUB_TOKEN }}
15+

entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -uo pipefail
3+
4+
PROJECT_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
5+
6+
if [ "$PROJECT_HOME" == "/" ]; then
7+
PROJECT_HOME=""
8+
fi
9+
10+
export PROJECT_HOME
11+
12+
source "$PROJECT_HOME/src/main.sh"
13+
14+
main "$@"
15+
16+
exit $?

src/ensure.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
ensure::env_variable_exist() {
4+
if [[ -z "${!1}" ]]; then
5+
echoerr "The env variable $1 is required."
6+
exit 1
7+
fi
8+
}
9+
10+
ensure::total_args() {
11+
local -r received_args=$(( $# - 1 ))
12+
local -r expected_args=$1
13+
14+
if ((received_args != expected_args)); then
15+
echoerr "Illegal number of parameters, $expected_args expected but $received_args found"
16+
exit 1
17+
fi
18+
}

src/github.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
GITHUB_API_URI="https://api.github.com"
4+
GITHUB_API_HEADER="Accept: application/vnd.github.v3+json"
5+
6+
github::delete_ref() {
7+
local -r ref=$1
8+
9+
curl -sSL \
10+
-H "Authorization: token $GITHUB_TOKEN" \
11+
-H "$GITHUB_API_HEADER" \
12+
-X DELETE \
13+
"$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/git/$ref"
14+
}

src/github_actions.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
github_actions::get_pr_number() {
4+
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
5+
}
6+
7+
github_actions::commit_sha() {
8+
jq --raw-output .after "$GITHUB_EVENT_PATH"
9+
}
10+
11+
github_actions::print_all_data() {
12+
cat "$GITHUB_EVENT_PATH"
13+
}

src/main.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
source "$PROJECT_HOME/src/ensure.sh"
4+
source "$PROJECT_HOME/src/github.sh"
5+
source "$PROJECT_HOME/src/github_actions.sh"
6+
source "$PROJECT_HOME/src/misc.sh"
7+
8+
main() {
9+
ensure::env_variable_exist "GITHUB_REPOSITORY"
10+
ensure::env_variable_exist "GITHUB_EVENT_PATH"
11+
ensure::env_variable_exist "GITHUB_REF"
12+
ensure::total_args 1 "$@"
13+
14+
export GITHUB_TOKEN="$1"
15+
16+
local -r branch_name="$GITHUB_REF"
17+
18+
log::message "$branch_name"
19+
20+
if str::contains "$branch_name" "refs/tags"; then
21+
log::message "Tag not removed!"
22+
else
23+
github::delete_ref "$branch_name"
24+
25+
log::message "Branch removed!!"
26+
fi
27+
28+
exit $?
29+
}

src/misc.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
echoerr() {
4+
echo "$@" 1>&2
5+
}
6+
7+
log::message() {
8+
echo "--------------"
9+
echo "$@"
10+
}
11+
12+
coll::join_by() {
13+
local IFS="$1"
14+
shift
15+
echo "$*"
16+
}
17+
18+
coll::map() {
19+
local -r fn="$1"
20+
21+
for x in $(cat); do
22+
"$fn" "$x"
23+
done
24+
}
25+
26+
str::contains() {
27+
echo "$1" | grep -q "$2"
28+
}

0 commit comments

Comments
 (0)