Skip to content

Commit d48996e

Browse files
committed
Add CI workflow to check the license file
Whenever one of the recognized license file names are modified in the repository, the workflow runs to check whether the license can be recognized and whether it is of the expected type. GitHub has a useful automated license detection system that determines the license type used by a repository, and surfaces that information in the repository home page, the search web interface, and the GitHub API. This license detection system requires that the license be defined by a dedicated file with one of several standardized filenames and paths. GitHub's license detection system uses the popular licensee tool, so this file also serves to define the license type for any other usages of licensee, as well as to human readers of the file. For this reason, and to ensure it remains a valid legal instrument, it's important that there be no non-standard modifications to the license file or collisions with other supported license files. This workflow ensures that any changes which would change the license type or which license file is used by the detection are caught automatically.
1 parent 509068e commit d48996e

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

.github/workflows/check-license.yml

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-license.md
2+
name: Check License
3+
4+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
5+
on:
6+
create:
7+
push:
8+
paths:
9+
- ".github/workflows/check-license.ya?ml"
10+
# See: https://github.com/licensee/licensee/blob/master/docs/what-we-look-at.md#detecting-the-license-file
11+
- "[cC][oO][pP][yY][iI][nN][gG]*"
12+
- "[cC][oO][pP][yY][rR][iI][gG][hH][tH]*"
13+
- "[lL][iI][cC][eE][nN][cCsS][eE]*"
14+
- "[oO][fF][lL]*"
15+
- "[pP][aA][tT][eE][nN][tT][sS]*"
16+
pull_request:
17+
paths:
18+
- ".github/workflows/check-license.ya?ml"
19+
- "[cC][oO][pP][yY][iI][nN][gG]*"
20+
- "[cC][oO][pP][yY][rR][iI][gG][hH][tH]*"
21+
- "[lL][iI][cC][eE][nN][cCsS][eE]*"
22+
- "[oO][fF][lL]*"
23+
- "[pP][aA][tT][eE][nN][tT][sS]*"
24+
schedule:
25+
# Run periodically to catch breakage caused by external changes.
26+
- cron: "0 6 * * WED"
27+
workflow_dispatch:
28+
repository_dispatch:
29+
30+
jobs:
31+
run-determination:
32+
runs-on: ubuntu-latest
33+
permissions: {}
34+
outputs:
35+
result: ${{ steps.determination.outputs.result }}
36+
steps:
37+
- name: Determine if the rest of the workflow should run
38+
id: determination
39+
run: |
40+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
41+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
42+
if [[
43+
"${{ github.event_name }}" != "create" ||
44+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
45+
]]; then
46+
# Run the other jobs.
47+
RESULT="true"
48+
else
49+
# There is no need to run the other jobs.
50+
RESULT="false"
51+
fi
52+
53+
echo "result=$RESULT" >> $GITHUB_OUTPUT
54+
55+
check-license:
56+
name: ${{ matrix.check-license.path }}
57+
needs: run-determination
58+
if: needs.run-determination.outputs.result == 'true'
59+
runs-on: ubuntu-latest
60+
permissions:
61+
contents: read
62+
63+
strategy:
64+
fail-fast: false
65+
66+
matrix:
67+
check-license:
68+
- path: ./
69+
# TODO: Define the project's license file name here:
70+
expected-filename: LICENSE.txt
71+
# SPDX identifier: https://spdx.org/licenses/
72+
expected-type: Apache-2.0
73+
74+
steps:
75+
- name: Checkout repository
76+
uses: actions/checkout@v4
77+
78+
- name: Install Ruby
79+
uses: ruby/setup-ruby@v1
80+
with:
81+
ruby-version: ruby # Install latest version
82+
83+
- name: Install licensee
84+
run: gem install licensee
85+
86+
- name: Check license file for ${{ matrix.check-license.path }}
87+
run: |
88+
EXIT_STATUS=0
89+
90+
# Go into folder path
91+
cd ./${{ matrix.check-license.path }}
92+
93+
# See: https://github.com/licensee/licensee
94+
LICENSEE_OUTPUT="$(licensee detect --json --confidence=100)"
95+
96+
DETECTED_LICENSE_FILE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].filename | tr --delete '\r')"
97+
echo "Detected license file: $DETECTED_LICENSE_FILE"
98+
if [ "$DETECTED_LICENSE_FILE" != "\"${{ matrix.check-license.expected-filename }}\"" ]; then
99+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license file $DETECTED_LICENSE_FILE doesn't match expected: ${{ matrix.check-license.expected-filename }}"
100+
EXIT_STATUS=1
101+
fi
102+
103+
DETECTED_LICENSE_TYPE="$(echo "$LICENSEE_OUTPUT" | jq .matched_files[0].matched_license | tr --delete '\r')"
104+
echo "Detected license type: $DETECTED_LICENSE_TYPE"
105+
if [ "$DETECTED_LICENSE_TYPE" != "\"${{ matrix.check-license.expected-type }}\"" ]; then
106+
echo "::error file=${DETECTED_LICENSE_FILE}::detected license type $DETECTED_LICENSE_TYPE doesn't match expected \"${{ matrix.check-license.expected-type }}\""
107+
EXIT_STATUS=1
108+
fi
109+
110+
exit $EXIT_STATUS

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Tests Status](https://github.com/arduino/arduino-lint-action/actions/workflows/test-javascript-jest-task.yml/badge.svg)](https://github.com/arduino/arduino-lint-action/actions/workflows/test-javascript-jest-task.yml)
44
[![Integration Tests Status](https://github.com/arduino/arduino-lint-action/workflows/Integration%20Tests/badge.svg)](https://github.com/arduino/arduino-lint-action/actions?workflow=Integration+Tests)
5+
[![Check License status](https://github.com/arduino/arduino-lint-action/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/arduino-lint-action/actions/workflows/check-license.yml)
56
[![Check Packaging status](https://github.com/arduino/arduino-lint-action/actions/workflows/check-packaging-ncc-typescript-npm.yml/badge.svg)](https://github.com/arduino/arduino-lint-action/actions/workflows/check-packaging-ncc-typescript-npm.yml)
67
[![Check Prettier Formatting status](https://github.com/arduino/arduino-lint-action/actions/workflows/check-prettier-formatting-task.yml/badge.svg)](https://github.com/arduino/arduino-lint-action/actions/workflows/check-prettier-formatting-task.yml)
78
[![Check TypeScript Configuration status](https://github.com/arduino/arduino-lint-action/actions/workflows/check-tsconfig-task.yml/badge.svg)](https://github.com/arduino/arduino-lint-action/actions/workflows/check-tsconfig-task.yml)

0 commit comments

Comments
 (0)