From b906d470c76d21221ca0006bccc957a5eddb1b82 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 2 Nov 2022 15:20:09 +0000 Subject: [PATCH] ci: Add a CI check for REUSE-compliant licensing/copyright headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This doesn’t enforce licensing/copyright headers to be present on all files, but does check that at least a minimum number of files are correct. This should help avoid new files being added without appropriate licensing information in future. The baseline is set at what `reuse lint` outputs for me at the moment. See https://reuse.software/tutorial/#step-2 for information about how to add REUSE-compliant licensing/copyright to files. Signed-off-by: Philip Withnall Helps: #1415 --- .gitlab-ci.yml | 13 +++++- .gitlab-ci/debian-stable.Dockerfile | 3 ++ .gitlab-ci/run-reuse.sh | 62 +++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100755 .gitlab-ci/run-reuse.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ed867e0b9..2f593c9e5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,7 +13,7 @@ cache: variables: FEDORA_IMAGE: "registry.gitlab.gnome.org/gnome/glib/fedora:v19" COVERITY_IMAGE: "registry.gitlab.gnome.org/gnome/glib/coverity:v7" - DEBIAN_IMAGE: "registry.gitlab.gnome.org/gnome/glib/debian-stable:v13" + DEBIAN_IMAGE: "registry.gitlab.gnome.org/gnome/glib/debian-stable:v14" MINGW_IMAGE: "registry.gitlab.gnome.org/gnome/glib/mingw:v9" MESON_TEST_TIMEOUT_MULTIPLIER: 4 G_MESSAGES_DEBUG: all @@ -72,6 +72,17 @@ sh-and-py-check: - "**/*.py" - "**/*.sh" +style-check-mandatory: + extends: .only-default + image: $DEBIAN_IMAGE + stage: style-check + allow_failure: false + script: + - .gitlab-ci/run-reuse.sh + variables: + # The submodules are needed for `reuse lint` to work properly + GIT_SUBMODULE_STRATEGY: "normal" + fedora-x86_64: extends: - .build-linux diff --git a/.gitlab-ci/debian-stable.Dockerfile b/.gitlab-ci/debian-stable.Dockerfile index 5dcf2d3c2..b2559cad6 100644 --- a/.gitlab-ci/debian-stable.Dockerfile +++ b/.gitlab-ci/debian-stable.Dockerfile @@ -68,6 +68,9 @@ ENV LANG=C.UTF-8 LANGUAGE=C.UTF-8 LC_ALL=C.UTF-8 RUN pip3 install meson==0.60.3 +# FIXME: Once we use Debian Bookworm, we can just install the `reuse` package +RUN pip3 install reuse==1.0.0 + ARG HOST_USER_ID=5555 ENV HOST_USER_ID ${HOST_USER_ID} RUN useradd -u $HOST_USER_ID -ms /bin/bash user diff --git a/.gitlab-ci/run-reuse.sh b/.gitlab-ci/run-reuse.sh new file mode 100755 index 000000000..6d0a44c56 --- /dev/null +++ b/.gitlab-ci/run-reuse.sh @@ -0,0 +1,62 @@ +#!/bin/bash +# +# Copyright 2022 Endless OS Foundation, LLC +# +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# Original author: Philip Withnall + +set -e + +# We need to make sure the submodules are up to date, or `reuse lint` will fail +# when it tries to run `git status` internally +git submodule update --init + +# Run `reuse lint` on the code base and see if the number of files without +# suitable copyright/licensing information has increased from a baseline +# FIXME: Eventually this script can check whether *any* files are missing +# information. But for now, let’s slowly improve the baseline. +files_without_copyright_information_max=407 +files_without_license_information_max=559 + +# The || true is because `reuse lint` will exit with status 1 if the project is not compliant +# FIXME: Once https://github.com/fsfe/reuse-tool/issues/512 or +# https://github.com/fsfe/reuse-tool/issues/183 land, we can check only files +# which have changed in this merge request, and confidently parse structured +# output rather than the current human-readable output. +lint_output="$(reuse lint || true)" + +files_with_copyright_information="$(echo "${lint_output}" | awk '/^\* Files with copyright information: / { print $6 }')" +files_with_license_information="$(echo "${lint_output}" | awk '/^\* Files with license information: / { print $6 }')" +total_files="$(echo "${lint_output}" | awk '/^\* Files with copyright information: / { print $8 }')" +error=0 + +files_without_copyright_information="$(( total_files - files_with_copyright_information ))" +files_without_license_information="$(( total_files - files_with_license_information ))" + +if [ "${files_without_copyright_information}" -gt "${files_without_copyright_information_max}" ] || \ + [ "${files_without_license_information}" -gt "${files_without_license_information_max}" ]; then + echo "${lint_output}" +fi + +if [ "${files_without_copyright_information}" -gt "${files_without_copyright_information_max}" ]; then + echo "" + echo "Error: New files added without REUSE-compliant copyright information" + echo "Please make sure that all files added in this branch/merge request have correct copyright information" + error=1 +fi + +if [ "${files_without_license_information}" -gt "${files_without_license_information_max}" ]; then + echo "" + echo "Error: New files added without REUSE-compliant licensing information" + echo "Please make sure that all files added in this branch/merge request have correct license information" + error=1 +fi + +if [ "${error}" -eq "1" ]; then + echo "" + echo "See https://reuse.software/tutorial/#step-2 for information on how to add REUSE information" + echo "Also see https://gitlab.gnome.org/GNOME/glib/-/issues/1415" +fi + +exit "${error}" \ No newline at end of file