|
| 1 | +name: LicenseCheck |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + # 'branches-ignore' or 'branches' can be used to filter specific branches. |
| 6 | + # By default, without any filters, it runs on every push to all branches. |
| 7 | + # To be explicit, you can use: |
| 8 | + branches-ignore: |
| 9 | + - 'develop' |
| 10 | + - 'master' |
| 11 | + - 'rebased/*' |
| 12 | + |
| 13 | +jobs: |
| 14 | + build: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + 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 | +
|
| 33 | + - name: Checkout |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + fetch-depth: ${{ steps.push-type.outputs.fetch_depth }} |
| 37 | + |
| 38 | + - name: Get changed files |
| 39 | + id: changed-files |
| 40 | + run: | |
| 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) |
| 44 | + else |
| 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) |
| 47 | + fi |
| 48 | + for file in $CHANGED_FILES; do |
| 49 | + echo "'$file' was changed" |
| 50 | + done |
| 51 | + echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: Process changed files |
| 54 | + id: process-files |
| 55 | + run: | |
| 56 | + LICENSE_LINES='' |
| 57 | + for file in ${{ steps.changed-files.outputs.changed_files }}; do |
| 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 |
| 70 | + done |
| 71 | + { |
| 72 | + echo 'license_lines<<EOF' |
| 73 | + echo "${LICENSE_LINES}" |
| 74 | + echo EOF |
| 75 | + } >> $GITHUB_OUTPUT |
| 76 | +
|
| 77 | + - name: Remove commit/branch if licenses found |
| 78 | + if: ${{ steps.process-files.outputs.license_lines != '' }} |
| 79 | + id: remove-license |
| 80 | + run: | |
| 81 | + if ${{ steps.push-type.outputs.push_type == 'commit'}}; then |
| 82 | + echo "Removing commit ${{ github.event.after }} as it contains licenses" |
| 83 | + git reset --hard ${{ github.event.before }} |
| 84 | + 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=push denied, reset to '${{ toJSON(github.event.before) }}'!" >> $GITHUB_OUTPUT |
| 87 | + echo "action_type=reverted to" >> $GITHUB_OUTPUT |
| 88 | + echo "msg_code=${{ github.event.before }}" >> $GITHUB_OUTPUT |
| 89 | + echo "xtra_msg=('${{ toJSON(github.event.head_commit.message) }}' denied)" >> $GITHUB_OUTPUT |
| 90 | + else |
| 91 | + echo "Removing branch ${{ github.ref }} as it contains licenses" |
| 92 | + git push origin --delete ${{ github.ref }} |
| 93 | + echo "link=https://github.com/${{ github.repository }}/branches" >> $GITHUB_OUTPUT |
| 94 | + echo "short_msg='${{ github.ref }}' was removed!" >> $GITHUB_OUTPUT |
| 95 | + echo "action_type=removed" >> $GITHUB_OUTPUT |
| 96 | + echo "msg_code=${{ github.ref }}" >> $GITHUB_OUTPUT |
| 97 | + echo "xtra_msg=" >> $GITHUB_OUTPUT |
| 98 | + fi |
| 99 | +
|
| 100 | + - name: Find correspondences |
| 101 | + if: ${{ steps.process-files.outputs.license_lines != '' }} |
| 102 | + id: email |
| 103 | + |
| 104 | + with: |
| 105 | + method: users.lookupByEmail # https://api.slack.com/methods/users.lookupByEmail |
| 106 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 107 | + payload: | |
| 108 | + email: ${{ github.event.pusher.email }} |
| 109 | +
|
| 110 | + - name: Search email detail |
| 111 | + if: ${{ steps.email.outputs.ok }} |
| 112 | + run: | |
| 113 | + SLACK_USER_ID=$(echo '${{ steps.email.outputs.response }}' | jq -r '.user.id') |
| 114 | + echo "SLACK_USER_ID=$SLACK_USER_ID" >> $GITHUB_ENV |
| 115 | +
|
| 116 | + - name: Send a direct message |
| 117 | + if: ${{ steps.email.outputs.ok }} |
| 118 | + |
| 119 | + with: |
| 120 | + errors: true |
| 121 | + method: chat.postMessage # https://api.slack.com/methods/chat.postMessage |
| 122 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 123 | + payload: | |
| 124 | + "channel": "${{ env.SLACK_USER_ID }}", |
| 125 | + "text": "${{ steps.remove-license.outputs.short_msg }}", |
| 126 | + "blocks": [ |
| 127 | + { |
| 128 | + "type": "section", |
| 129 | + "text": { |
| 130 | + "type": "mrkdwn", |
| 131 | + "text": ":alert: *LICENSES DETECTED* :alert:" |
| 132 | + } |
| 133 | + }, |
| 134 | + { |
| 135 | + "type": "section", |
| 136 | + "text": { |
| 137 | + "type": "mrkdwn", |
| 138 | + "text": "${{ steps.remove-license.outputs.action_type}} ${{ steps.push-type.outputs.push_type}} `${{ steps.remove-license.outputs.msg_code }}` ${{ steps.remove-license.outputs.xtra_msg }}" |
| 139 | + } |
| 140 | + }, |
| 141 | + { |
| 142 | + "type": "section", |
| 143 | + "text": { |
| 144 | + "type": "mrkdwn", |
| 145 | + "text": "<${{ steps.remove-license.outputs.link }}>" |
| 146 | + } |
| 147 | + } |
| 148 | + ] |
0 commit comments