-
Notifications
You must be signed in to change notification settings - Fork 0
98 lines (82 loc) · 3.46 KB
/
main.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: Should run reuseable workflow
on:
workflow_call:
inputs:
projects:
description: The projects to match against
required: true
type: string
diff_filter:
description: Filter to use on git diff
required: false
type: string
default: ''
outputs:
result:
description: JSON result. One boolean property for each input project
value: ${{ jobs.main.outputs.result }}
jobs:
main:
runs-on: ubuntu-latest
outputs:
result: ${{ toJSON(steps.changes.outputs) }}
defaults:
run:
shell: bash
steps:
- name: Initializing git repository
run: |
git init --initial-branch=main
git remote add origin ${{ github.event.repository.html_url }}
- name: Fetching base and head commit (push)
if: github.event_name == 'push'
env:
PUSHED_COMMITS: ${{ toJSON(github.event.commits) }}
run: |
if [[ ${{ github.event.before }} == 0000000000000000000000000000000000000000 ]]
then
before=$(echo "$PUSHED_COMMITS" | jq .[0].id --raw-output)
git fetch --no-tags --prune --no-recurse-submodules --depth=2 origin $before ${{ github.event.after }}
echo "BASE=$(git rev-parse $before~1)" >> $GITHUB_ENV
else
git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin ${{ github.event.before }} ${{ github.event.after }}
echo "BASE=${{ github.event.before }}" >> $GITHUB_ENV
fi
echo "HEAD=${{ github.event.after }}" >> $GITHUB_ENV
- name: Fetching base and head commit (pull_request)
if: github.event_name == 'pull_request'
run: |
git fetch --no-tags --prune --no-recurse-submodules --depth=$((${{ github.event.pull_request.commits }} + 1)) origin ${{ github.event.pull_request.head.sha }}
git fetch --no-tags --prune --no-recurse-submodules --depth=10 origin ${{ github.event.pull_request.base.sha }}
git checkout --progress --force ${{ github.event.pull_request.head.sha }}
while [[ -n $(git rev-list shallow ^${{ github.event.pull_request.base.sha }}) ]]
do
git fetch --no-tags --prune --no-recurse-submodules --deepen=10 origin ${{ github.event.pull_request.base.sha }}
done
base=$(git rev-list ${{ github.event.pull_request.head.sha }} ^${{ github.event.pull_request.base.sha }} | tail --lines 1 | xargs -I {} git rev-parse {}~1)
echo "BASE=$base" >> $GITHUB_ENV
echo "HEAD=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
- name: Determining changes
id: changes
run: |
shopt -s extglob
declare -r inputs="${{ inputs.projects }}"
declare -r projects=${inputs//+([ ,])/|}
declare -r pattern="^($projects)/.*"
declare -r old_IFS=$IFS
IFS="|"
for project in $projects
do
echo "$project=false" >> $GITHUB_OUTPUT
done
IFS=$old_IFS
echo "The pattern is: $pattern"
while read -r line
do
echo "Git diff: $line"
if [[ $line =~ $pattern ]]
then
echo "Match found! The match was: ${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[1]}=true" >> $GITHUB_OUTPUT
fi
done < <(git diff --name-only --diff-filter=${{ inputs.diff_filter }} ${{ env.BASE }} ${{ env.HEAD }})