Skip to content

Commit 311fe19

Browse files
committed
[WIP] Bootstrap the GitHub Action skeleton and first WIP implementation
0 parents  commit 311fe19

File tree

10 files changed

+241
-0
lines changed

10 files changed

+241
-0
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM alpine:3.10
2+
3+
RUN apk add --no-cache bash curl jq yq
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
👤 Assign Pull Request by modified path
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/pull-request-assign-by-path"><img src="https://img.shields.io/github/v/release/CodelyTV/assign-by-path?style=flat-square" alt="GitHub Action version"></a>
15+
</p>
16+
17+
<p align="center">
18+
Assign Pull Requests to your GitHub teams or individual users based on the paths of the modified files
19+
</p>
20+
21+
## 🚀 Usage
22+
23+
☝️ Create a file named `assign-by-path.yml` inside the `.github/workflows` directory and paste:
24+
25+
```yml
26+
name: assign-by-path
27+
28+
on: [push]
29+
30+
jobs:
31+
assigner:
32+
runs-on: ubuntu-latest
33+
name: 👤 Assign by path
34+
steps:
35+
- uses: codelytv/assign-by-path@v1
36+
with:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
```
39+
40+
✌️ Create a file named `assign_by_path_mappings.yml` inside the `.github` directory.
41+
42+
This file must contain the association between your GitHub teams or people to assign PRs (mapping keys) and paths.
43+
44+
In the following example, we will be assigning the PRs that modifies files inside the `src/mooc` folder to the `mooc` team:
45+
46+
```yml
47+
assign:
48+
mooc: ["src/mooc", "src/shared"]
49+
retention: ["src/retention"]
50+
backoffice: ["src/backoffice", "src/analytics"]
51+
```
52+
53+
## 💡 Use case examples
54+
55+
You can combine this action with others such as:
56+
57+
* Notify to the Slack channel `#mooc-team` when a PR has been assigned to the `mooc` GitHub team
58+
* Send an email to `[email protected]` when a PR has been assigned to the `mooc` GitHub team
59+
* You name it! Composition over Inheritance FTW! 😊
60+
61+
## ⚖️ License
62+
63+
[MIT](LICENSE)

action.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: '👤 Assign by path'
2+
description: 'Assign Pull Requests based on the paths of the modified files'
3+
inputs:
4+
GITHUB_TOKEN:
5+
description: 'GitHub token'
6+
required: true
7+
runs:
8+
using: 'docker'
9+
image: 'Dockerfile'
10+
args:
11+
- ${{ inputs.GITHUB_TOKEN }}

entrypoint.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+
set -euo pipefail
3+
4+
ASSIGN_BY_PATH_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
5+
6+
if [ "$ASSIGN_BY_PATH_HOME" == "/" ]; then
7+
ASSIGN_BY_PATH_HOME=""
8+
fi
9+
10+
export ASSIGN_BY_PATH_HOME
11+
12+
bash --version
13+
14+
source "$ASSIGN_BY_PATH_HOME/src/main.sh"
15+
16+
main "$@"
17+
18+
exit $?

src/assigner.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
assigner::directory_has_been_modified() {
4+
local -r directory=$1
5+
local -r modified_directories=$(github::get_commit_modified_files $GITHUB_SHA | grep "src/" | awk -F '/' '{print $2}' | uniq)
6+
7+
log::message "modified_directories: "
8+
log::message "$modified_directories"
9+
10+
[[ $modified_directories == *"$directory"* ]]
11+
}
12+
13+
assigner::team_has_been_modified() {
14+
local -r IFS=$'\n'
15+
local -r team_directories=($1)
16+
local i
17+
for (( i=0; i<${#team_directories[@]}; i++ )) ; do
18+
local -r directory_without_yaml=$(echo "${team_directories[$i]//'- '}")
19+
20+
if assigner::directory_has_been_modified "$directory_without_yaml" ; then
21+
log::message "👍 $directory_without_yaml directory HAS been modified"
22+
else
23+
log::message "👎 $directory_without_yaml directory has NOT been modified"
24+
fi
25+
done
26+
}
27+
28+
assigner::assign_to() {
29+
local IFS=$'\n'
30+
local teams_mappings=($1)
31+
local i
32+
for (( i=0; i<${#teams_mappings[@]}; i++ )) ; do
33+
log::message "Team $i: ${teams_mappings[$i]}"
34+
35+
log::message "---------------------------------"
36+
local -r team_directories=$(yq r -X monorepo_assign.yml "assign.${teams_mappings[$i]}")
37+
38+
assigner::team_has_been_modified "$team_directories"
39+
40+
log::message "---------------------------------"
41+
done
42+
}
43+
44+
assigner::assign() {
45+
local -r teams_mappings=$(yq r monorepo_assign.yml assign | awk -F":" '{print $1}')
46+
assigner::assign_to "$teams_mappings"
47+
}

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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::get_commit_modified_files() {
7+
local -r commit_ref=$1
8+
curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/commits/$commit_ref" | jq .files | jq -r ".[] | .filename"
9+
}

src/main.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
source "$ASSIGN_BY_PATH_HOME/src/assigner.sh"
4+
source "$ASSIGN_BY_PATH_HOME/src/ensure.sh"
5+
source "$ASSIGN_BY_PATH_HOME/src/github.sh"
6+
source "$ASSIGN_BY_PATH_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_SHA"
12+
ensure::total_args 1 "$@"
13+
14+
export GITHUB_TOKEN="$1"
15+
16+
assigner::assign
17+
18+
exit $?
19+
}

src/misc.sh

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

0 commit comments

Comments
 (0)