Skip to content

Commit

Permalink
ci: Added a shell script that checks if the files in the repository c…
Browse files Browse the repository at this point in the history
…onform to the file naming convention as discussed in ElektraInitiative#1615. This script is executed at the cirrus CI and the Jenkins CI.
  • Loading branch information
max-thoma authored and flo91 committed Dec 17, 2021
1 parent bbd9a73 commit 6162d2a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
36 changes: 36 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,42 @@ task:
false
fi
task:
matrix:

- name: 📄 Check
container:
cpu: 2
memory: 4G
dockerfile: scripts/docker/cirrus/arch/Dockerfile

clone_script: |
if [ -z "$CIRRUS_PR" ]; then
git clone --branch=$CIRRUS_BRANCH https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git \
$CIRRUS_WORKING_DIR
git reset --hard $CIRRUS_CHANGE_IN_REPO
else
git clone https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
git fetch origin pull/$CIRRUS_PR/head:pull/$CIRRUS_PR
git config user.email "[email protected]"
git config user.name "nobody"
git merge --no-commit --no-ff $CIRRUS_CHANGE_IN_REPO
fi
script:
- *generate
- scripts/dev/filename-checker 2> bad_file_names.txt > /dev/null

tests_script:
- |
if test -s bad_file_names.txt; then
printf >&2 '—————————————\n'
cat >&2 bad_file_names.txt
printf >&2 '—————————————\n'
false
fi
task:
matrix:

Expand Down
1 change: 1 addition & 0 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ _(Michael Tucek)_
- <<TODO>>
- Added more test cases for the keyCopy function _(@muskater)_
- add exception tests for key C++ bindings _(Ivaylo Ivanov)_
- Added a shell script and a task that checks whether the filenames of newly added files are compliant with the convention. It is executed by the cirrus CI as well as the Jenkins CI _(@muskater)_

## Packaging

Expand Down
50 changes: 50 additions & 0 deletions scripts/dev/filename-checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
#
# @author muskater
# @brief Checks that only files exist in the repository that conform to the naming convention: [/.a-zA-Z0-9_-]*
# Exceptions to this rule can be specified in tests/filename.whitelist.
# The current directory will be used as base directory.
# @date 18.11.2021
# @tags validation

if [ $# -ne 0 ]; then
echo "Usage: filename-checker"
exit
fi

# Check if tests/allow-list exists.
ALLOW_LIST_FILE=./tests/filename.whitelist
if [ ! -f "$ALLOW_LIST_FILE" ]; then
echo >&2 "$ALLOW_LIST_FILE dose not exists."
exit 1
fi

# Store the content of the allow-list into ALLOW_LIST.
# Ignore every line that starts with '#' or spaces.
ALLOW_LIST=$(grep -Ev '^(#|[[:space:]]*$)' "./tests/filename.whitelist")

# Find any file that is non-compliant.
# Save them to the variable FORBIDDEN_NAMES.
FORBIDDEN_NAMES=$(find . | grep -v '^[-_/.a-zA-Z0-9]*$')

# All file names that offend the rule are saved into BANNED_NAMES.
BANNED_NAMES=$(echo "$FORBIDDEN_NAMES" | while read -r test_line; do
# Set the delimiter to newline to compare every line.
# Import the variable test_line as l.
# Compare line by line the allow-list with test_line.
CHECK=$(echo "$ALLOW_LIST" | awk -F "\n" -v l="$test_line" '$1 == l { print $1 }')
# When CHECK is empty it means that that no entry in the white list was found.
if [ -z "$CHECK" ]; then
echo "$test_line"
fi
done)
if [ -n "$BANNED_NAMES" ]; then
echo >&2 "These file names do not comply with the naming convention: [/.a-zA-Z0-9_-]*"
echo >&2 "Please change them accordingly or add them to tests/filename.whitelist"
echo >&2 "$BANNED_NAMES"
exit 1
else
echo "All file names comply!"
exit 0
fi
18 changes: 17 additions & 1 deletion scripts/jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def dockerInit() {
}

/* Generate basic check stages
*
*
* Runs basic checks that do not to build the code
*/
def generateBasicCheckStages() {
Expand All @@ -493,6 +493,9 @@ def generateBasicCheckStages() {
// Check if release notes have been updated
tasks << buildCheckReleaseNotes()

// Check if all file names comply with the file name convention
tasks << buildCheckFileNames()

// Check for reuse compliancy
tasks << buildCheckReuseCompliant()

Expand Down Expand Up @@ -841,6 +844,19 @@ def buildCheckReleaseNotes() {
}]
}

/* Stage checking if release notes have been updated */
def buildCheckFileNames() {
def stageName = "check-file-names"
return [(stageName): {
maybeStage(stageName, !isMaster()) {
withDockerEnv(DOCKER_IMAGES.bullseye, [DockerOpts.MOUNT_MIRROR]) {
sh "scripts/dev/filename-checker"
deleteDir()
}
}
}]
}

/* Stage checking if for reuse compliancy */
def buildCheckReuseCompliant() {
def stageName = "check-reuse-compliant"
Expand Down
2 changes: 2 additions & 0 deletions tests/filename.whitelist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Whitelist for the filename-checker
./.idea/fileTemplates/includes/C File Header.h

0 comments on commit 6162d2a

Please sign in to comment.