-
Notifications
You must be signed in to change notification settings - Fork 0
96 lines (82 loc) · 4.17 KB
/
Copy pathlicensecheck.yml
File metadata and controls
96 lines (82 loc) · 4.17 KB
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
name: "License Check"
on:
workflow_call:
jobs:
licenseSearch:
if: "!contains(toJson(github.event.commits), '[]')"
uses: ./.github/workflows/licensesearch.yml
secrets: inherit
with:
commits: ${{ toJson(github.event.commits) }}
handleFoundLicenses:
needs: [licenseSearch]
runs-on: ubuntu-latest
outputs:
removal: ${{ steps.remove_license.outputs.removal }}
slack_short_msg: ${{ steps.remove_license.outputs.short_msg }}
slack_blocks: ${{ steps.remove_license.outputs.blocks }}
steps:
- name: Get number commits
id: get_nr_commits
if: ${{ needs.licenseSearch.outputs.filtered_results != '' }}
env:
commits: ${{ toJson(github.event.commits) }}
run: |
NUMBER_COMMITS=$(echo $commits | jq '. | length' )
echo "number_commits=$NUMBER_COMMITS" >> $GITHUB_OUTPUT
- name: Get push type
id: push-type
if: ${{ needs.licenseSearch.outputs.filtered_results != '' }}
env:
COMMITCOUNT: ${{ steps.get_nr_commits.outputs.number_commits }}
run: |
echo "Getting push type"
PUSH_TYPE='commit'
FETCH_DEPTH=$(($COMMITCOUNT + 1))
if ${{ github.event.before == '0000000000000000000000000000000000000000' }}; then
PUSH_TYPE='branch'
FETCH_DEPTH=0
fi
echo "Push type: $PUSH_TYPE"
echo "Fetch depth: $FETCH_DEPTH"
echo "push_type=$PUSH_TYPE" >> $GITHUB_OUTPUT
echo "fetch_depth=$FETCH_DEPTH" >> $GITHUB_OUTPUT
- name: Checkout
if: ${{ needs.licenseSearch.outputs.filtered_results != '' }}
uses: actions/checkout@v4
with:
fetch-depth: ${{ steps.push-type.outputs.fetch_depth }}
- name: Remove commits if licenses found
if: ${{ needs.licenseSearch.outputs.filtered_results != '' }}
id: remove_license
run: |
echo "removal=${{ true }}" >> $GITHUB_OUTPUT
FIRSTBLOCK="{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \":alert: *LICENSES DETECTED* :alert:\"}}"
if ${{ steps.push-type.outputs.push_type == 'commit'}}; then
git reset --hard HEAD~${{ steps.get_nr_commits.outputs.number_commits }}
git push origin ${{ github.ref }} --force
revertcommit="$(git rev-parse HEAD)"
echo "Reverting to commit $revertcommit as newer commits contains licenses"
SHORT_MSG="push denied, reset to '$revertcommit'!"
SECONDBLOCK="{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"reverted to commit '$revertcommit'\"}}"
FOURTHBLOCK="{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"https://github.com/${{ github.repository }}/commits/${{ github.ref }}\"}}"
else
git push origin --delete ${{ github.ref }}
echo "Removing branch ${{ github.ref }} as it contains licenses"
SHORT_MSG="'${{ github.ref }}' was removed!"
SECONDBLOCK="{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"removed branch '${{ github.ref }}'\"}}"
FOURTHBLOCK="{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"https://github.com/${{ github.repository }}/branches\"}}"
fi
info=$(jq -n --argjson data '[${{ needs.licenseSearch.outputs.filtered_results }}]' '"`" + $data[].commit + "`: ```" + $data[].licenselines + "```"')
BLOCKS="[$FIRSTBLOCK, $SECONDBLOCK, {\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": $info}}, $FOURTHBLOCK]"
echo "short_msg=$SHORT_MSG" >> $GITHUB_OUTPUT
echo "blocks=$BLOCKS" >> $GITHUB_OUTPUT
notification_removal:
needs: [handleFoundLicenses]
if: (needs.handleFoundLicenses.outputs.removal == 'true')
uses: ./.github/workflows/sendslackmsgtopusher.yml
secrets: inherit
with:
emailslackuser: ${{ github.event.pusher.email }}
shortmsg: ${{ needs.handleFoundLicenses.outputs.slack_short_msg }}
blocks: ${{ needs.handleFoundLicenses.outputs.slack_blocks }}