@@ -15,57 +15,86 @@ jobs:
1515 runs-on : ubuntu-latest
1616
1717 steps :
18+ - name : Get push type
19+ id : push-type
20+ run : |
21+ echo "Getting push type"
22+ PUSH_TYPE='commit'
23+ FETCH_DEPTH=10
24+ if ${{ github.event.forced }} || ${{ github.event.before == '0000000000000000000000000000000000000000' }}; then
25+ PUSH_TYPE='branch'
26+ FETCH_DEPTH=0
27+ fi
28+ echo "Push type: $PUSH_TYPE"
29+ echo "Fetch depth: $FETCH_DEPTH"
30+ echo "push_type=$PUSH_TYPE" >> $GITHUB_OUTPUT
31+ echo "fetch_depth=$FETCH_DEPTH" >> $GITHUB_OUTPUT
32+
1833 - name : Checkout
1934 uses : actions/checkout@v4
2035 with :
21- fetch-depth : 2
22-
23- - name : Get changes
36+ fetch-depth : ${{ steps.push-type.outputs.fetch_depth }}
37+
38+ - name : Get changed files
2439 id : changed-files
2540 run : |
26- echo "Getting changed files from ${{ github.event.before }} to ${{ github.event.after }}"
27- if ${{ github.event.before == '0000000000000000000000000000000000000000' }}; then
28- git fetch origin develop
29- echo "changed_files=$(git diff --name-only remotes/origin/develop ${{ github.event.after }} | xargs)" >> $GITHUB_OUTPUT
41+ if ${{ steps.push-type.outputs.push_type == 'branch'}}; then
42+ echo "First commit on feature branch or force push - getting all changed files compared to 'develop'"
43+ CHANGED_FILES=$(git diff --name-only remotes/origin/develop ${{ github.event.after }} | xargs)
3044 else
31- echo "changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | xargs)" >> $GITHUB_OUTPUT
45+ echo "Getting changed files from ${{ github.event.before }} to ${{ github.event.after }}"
46+ CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | xargs)
3247 fi
48+ for file in $CHANGED_FILES; do
49+ echo "'$file' was changed"
50+ done
51+ echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT
52+
3353 - name : Process changed files
34- id : license-lines
54+ id : process-files
3555 run : |
3656 LICENSE_LINES=''
3757 for file in ${{ steps.changed-files.outputs.changed_files }}; do
38- echo "$file was changed"
39- LICENSE_LINE="<$file>
40- $(cat $file | grep -Pzo '(<|")licensee("| )(\n|.)*(}|</licensee>)' | xargs)
41- "
42- LICENSE_LINES="$LICENSE_LINES
43- $LICENSE_LINE"
58+ echo "Processing '$file'..."
59+ LICENSE_MATCH=$(cat $file | grep -Pzo '(<|")licensee("| )(\n|.)*(}|</licensee>)' | xargs)
60+ if [ -z "$LICENSE_MATCH" ]; then
61+ echo "...no licenses found"
62+ else
63+ echo "license found!"
64+ LICENSE_LINE="<$file>
65+ $LICENSE_MATCH
66+ "
67+ LICENSE_LINES="$LICENSE_LINES
68+ $LICENSE_LINE"
69+ fi
4470 done
4571 {
4672 echo 'license_lines<<EOF'
4773 echo "${LICENSE_LINES}"
4874 echo EOF
4975 } >> $GITHUB_OUTPUT
50- - name : List licenses found
51- run : echo "${{ steps.license-lines.outputs.license_lines }}"
5276
53- - name : Remove commit if licenses found
54- id : remove-commit
77+ - name : Remove commit/branch if licenses found
78+ if : ${{ steps.process-files.outputs.license_lines != '' }}
79+ id : remove-license
5580 run : |
56- BAD_COMMIT=''
57- if ${{ steps.license-lines.outputs.license_lines != '' }}; then
58- BAD_COMMIT="${{ github.event.after }}"
59- echo "Removing commit $BAD_COMMIT as it contains licenses"
81+ if ${{ steps.push-type.outputs.push_type == 'commit'}}; then
82+ echo "Removing commit ${{ github.event.after }} as it contains licenses"
6083 git reset --hard ${{ github.event.before }}
6184 git push origin ${{ github.ref }} --force-with-lease
85+ echo "link=https://github.com/${{ github.repository }}/commits/${{ github.ref }}" >> $GITHUB_OUTPUT
86+ echo "shortMsg=${{ github.event.after }} was removed!" >> $GITHUB_OUTPUT
87+ echo "msg=Commit with message '${{ toJSON(github.event.head_commit.message) }}' `${{ github.event.after }}` was removed!" >> $GITHUB_OUTPUT
6288 else
63- echo "NOT Removing commit"
89+ echo "Removing branch ${{ github.ref }} as it contains licenses"
90+ git push origin --delete ${{ github.ref }}
91+ echo "link=https://github.com/${{ github.repository }}/branches" >> $GITHUB_OUTPUT
92+ echo "shortMsg='${{ github.ref }}' was removed!" >> $GITHUB_OUTPUT
93+ echo "msg=Branch '${{ github.ref }}' was removed!" >> $GITHUB_OUTPUT
6494 fi
65- echo "bad_commit=$BAD_COMMIT" >> $GITHUB_OUTPUT
6695
6796 - name : Find correspondences
68- if : ${{ steps.license-lines .outputs.license_lines != '' }}
97+ if : ${{ steps.process-files .outputs.license_lines != '' }}
6998 id : email
709971100 with :
@@ -89,20 +118,20 @@ jobs:
89118 token : ${{ secrets.SLACK_BOT_TOKEN }}
90119 payload : |
91120 "channel": "${{ env.SLACK_USER_ID }}",
92- "text": "Commit ${{ steps.remove-commit .outputs.bad_commit }} was removed! ",
121+ "text": "${{ steps.remove-license .outputs.shortMsg }}",
93122 "blocks": [
94123 {
95124 "type": "section",
96125 "text": {
97126 "type": "mrkdwn",
98- "text": ":alert-beam: <https://github.com/ ${{ github.repository }}/commits/${{ github.ref }}|*${{ github.repository }}>${{ github.ref }}*>: `${{ github.sha }}` "
127+ "text": ":alert: ${{ steps.remove-license.outputs.msg }} :alert: "
99128 }
100129 },
101130 {
102131 "type": "section",
103132 "text": {
104133 "type": "mrkdwn",
105- "text": ${{ toJSON(github.event.head_commit.message) }}
134+ "text": "< ${{ steps.remove-license.outputs.link }}>"
106135 }
107136 }
108137 ]
0 commit comments