Skip to content

Commit e00a26c

Browse files
committed
Tweaking 1
1 parent bcd226e commit e00a26c

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

.github/workflows/licensecheck.yml

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,88 @@ 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 "short_msg='${{ toJSON(github.event.after) }}' was removed!" >> $GITHUB_OUTPUT
87+
echo "xtra_msg= (${{ toJSON(github.event.head_commit.message) }})" >> $GITHUB_OUTPUT
88+
echo "msg_code=${{ github.event.after }}" >> $GITHUB_OUTPUT
6289
else
63-
echo "NOT Removing commit"
90+
echo "Removing branch ${{ github.ref }} as it contains licenses"
91+
git push origin --delete ${{ github.ref }}
92+
echo "link=https://github.com/${{ github.repository }}/branches" >> $GITHUB_OUTPUT
93+
echo "short_msg='${{ github.ref }}' was removed!" >> $GITHUB_OUTPUT
94+
echo "xtra_msg=" >> $GITHUB_OUTPUT
95+
echo "msg_code=${{ github.ref }}" >> $GITHUB_OUTPUT
6496
fi
65-
echo "bad_commit=$BAD_COMMIT" >> $GITHUB_OUTPUT
6697
6798
- name: Find correspondences
68-
if: ${{ steps.license-lines.outputs.license_lines != '' }}
99+
if: ${{ steps.process-files.outputs.license_lines != '' }}
69100
id: email
70101
uses: slackapi/[email protected]
71102
with:
@@ -89,20 +120,27 @@ jobs:
89120
token: ${{ secrets.SLACK_BOT_TOKEN }}
90121
payload: |
91122
"channel": "${{ env.SLACK_USER_ID }}",
92-
"text": "Commit ${{ steps.remove-commit.outputs.bad_commit }} was removed!",
123+
"text": "${{ steps.remove-license.outputs.short_msg }}",
93124
"blocks": [
94125
{
95126
"type": "section",
96127
"text": {
97128
"type": "mrkdwn",
98-
"text": ":alert-beam: <https://github.com/${{ github.repository }}/commits/${{ github.ref }}|*${{ github.repository }}>${{ github.ref }}*>: `${{ github.sha }}`"
129+
"text": ":alert: *LICENSES DETECTED* :alert:"
130+
}
131+
},
132+
{
133+
"type": "section",
134+
"text": {
135+
"type": "mrkdwn",
136+
"text": "${{ steps.push-type.outputs.push_type}} removed: `${{ steps.remove-license.outputs.msg_code }}`${{ steps.remove-license.outputs.xtra_msg }}"
99137
}
100138
},
101139
{
102140
"type": "section",
103141
"text": {
104142
"type": "mrkdwn",
105-
"text": ${{ toJSON(github.event.head_commit.message) }}
143+
"text": "<${{ steps.remove-license.outputs.link }}>"
106144
}
107145
}
108146
]

0 commit comments

Comments
 (0)