-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run bids-validator #5
Draft
kousu
wants to merge
5
commits into
main
Choose a base branch
from
worker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#!/bin/bash | ||
# ------------------------------------------------------------------- | ||
# Default bids-hook worker script | ||
# ------------------------------------------------------------------- | ||
# | ||
# If bids-hook is started with the environment variable WORKER_SCRIPT | ||
# pointing to this script, then it will be executed by the worker | ||
# once for each accepted job. | ||
# | ||
# In addition to inheriting all environment variables used to launch | ||
# bids-hook, the script will be given the details of the job in the | ||
# following environment variables: | ||
# | ||
# BH_USER, BH_REPO, BH_COMMIT, BH_UUID | ||
# | ||
# Any output generated by the script on stdout (file descriptor 1) | ||
# is saved as a result page, overwriting the output of any previous | ||
# run for the same UUID. The result page is linked from the commit | ||
# status posted on Gitea, and will be visible to anyone who has a | ||
# link. It should be a complete, properly formatted HTML document. | ||
# | ||
# Any output generated by the script on stderr (file descriptor 2) | ||
# is appended to a log file, after the log output of any previous | ||
# run for the same UUID, visible at the filesystem path: | ||
# | ||
# "${WORKER_LOG_PATH}/ab/cd/${BH_UUID}.log" | ||
# | ||
# where "abcd" are the first four characters of BH_UUID. | ||
# | ||
# The exit code of the script is interpreted as follows: | ||
# | ||
# 0 = "success" (green checkmark) | ||
# 1 = "failure" (red "X" mark) | ||
# 2 = "warning" (yellow "!" mark) | ||
# 3+ = "internal error" (red "!" mark, no link to the result page) | ||
# | ||
# ------------------------------------------------------------------- | ||
|
||
# Exit with the "internal error" status if anything goes wrong. | ||
set -o nounset -o pipefail | ||
trap 'exit 3' ERR | ||
|
||
# Log the job metadata to stderr. | ||
(exec 1>&2 | ||
echo '=== job start ===' | ||
date | ||
cat <<EOF | ||
BH_USER=${BH_USER} | ||
BH_REPO=${BH_REPO} | ||
BH_COMMIT=${BH_COMMIT} | ||
BH_UUID=${BH_UUID} | ||
EOF | ||
) | ||
|
||
# Canonicalize the GITEA_REPOSITORY_ROOT before changing directories | ||
# in case it was a relative path. | ||
GITEA_REPO=$(realpath --canonicalize-existing \ | ||
"${GITEA_REPOSITORY_ROOT}/${BH_USER}/${BH_REPO}.git") | ||
echo 1>&2 "GITEA_REPO=${GITEA_REPO}" | ||
|
||
# Do all the work in a private temporary directory. | ||
WORKDIR=$(mktemp --directory --tmpdir bids-hook.XXXXXXXXXX) | ||
echo 1>&2 "WORKDIR=${WORKDIR}" | ||
# We have an ERR trap to account for this: | ||
# shellcheck disable=SC2164 | ||
cd 1>&2 "$WORKDIR" | ||
# We have an EXIT trap which makes this code reachable: | ||
# shellcheck disable=SC2317 | ||
function cleanup() { | ||
echo "# cleaning ${WORKDIR}" | ||
chmod --recursive u+w "$WORKDIR" | ||
rm --recursive "$WORKDIR" | ||
} 1>&2 | ||
trap cleanup EXIT | ||
|
||
# Check out the repository, redirecting all output to stderr. | ||
(exec 1>&2 | ||
# We'd like to do 'git clone -b "$BH_COMMIT" --depth 1 "$GITEA_REPO"' | ||
# but maybe "$BH_COMMIT" is a commit ID instead of a branch name, so | ||
# we do it this way instead. | ||
echo "# cloning ${GITEA_REPO}" | ||
git init | ||
git remote add origin "$GITEA_REPO" | ||
git fetch --depth 1 origin "$BH_COMMIT" | ||
git checkout "$BH_COMMIT" | ||
|
||
# If this is a git-annex repository, we need to get the contents. | ||
if git ls-remote --exit-code origin refs/heads/git-annex >/dev/null; then | ||
echo '# getting git-annexed files' | ||
# this reduces copies; always overrides annex.hardlink even if that is set system-wide | ||
git config annex.thin true | ||
# make sure we don't corrupt origin accidentally | ||
git config remote.origin.annex-readonly true | ||
git config annex.private true # XXX this doesn't do anything until git-annex 10 | ||
git annex init | ||
git annex dead here # this is like annex.private, but has to be run | ||
# grab the git-annex branch (since we did a shallow clone above) | ||
git annex sync --only-annex --no-content | ||
# NB: using 'copy --from origin' and not 'git annex get; to ensure we're | ||
# validating the contents of origin and not any special remotes | ||
git annex copy --from origin | ||
fi | ||
) | ||
|
||
echo 1>&2 '# running bids-validator' | ||
OUTPUT=$(bids-validator .) && STATUS=$? || STATUS=$? | ||
WARNINGS=$(bids-validator . --json | jq '.issues.warnings | length') || true | ||
cat 1>&2 <<EOF | ||
STATUS=${STATUS} | ||
WARNINGS=${WARNINGS} | ||
EOF | ||
|
||
echo 1>&2 '# formatting output' | ||
HTML=$(echo "$OUTPUT" | ansifilter --html --fragment --line-numbers --anchors=self) | ||
|
||
# Produce the HTML result page on stdout. | ||
cat <<EOF | ||
<!DOCTYPE html> | ||
<html lang=en> | ||
<title>bids-validator results for ${BH_USER}/${BH_REPO}@${BH_COMMIT}</title> | ||
<p>Here are the bids-validator results for ${BH_USER}/${BH_REPO}@${BH_COMMIT}:</p> | ||
<pre> | ||
${HTML} | ||
</pre> | ||
</html> | ||
EOF | ||
|
||
# Our exit code means: | ||
# 0 = "success" (green checkmark) | ||
# 1 = "failure" (red "X" mark) | ||
# 2 = "warning" (yellow "!" mark) | ||
# 3+ = "internal error" (red "!" mark, no link to the result page) | ||
case $STATUS in | ||
(0) | ||
if ((WARNINGS)); then | ||
exit 2 | ||
else | ||
exit 0 | ||
fi | ||
;; | ||
(1) | ||
exit 1 | ||
;; | ||
(*) | ||
exit 3 | ||
;; | ||
esac |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we know the repository is local anyway, we could instead check directly for a valid
annex.uuid
in the config: