Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 2
updates:
# pins of the workflows of this repository and of every shared action
- package-ecosystem: github-actions
directories:
- "/"
- "/*"
- "/*/*"
schedule:
interval: monthly
cooldown:
default-days: 7
open-pull-requests-limit: 10
groups:
github-actions:
patterns:
- "*"
commit-message:
prefix: "fix(deps)"
labels:
- "dependencies"
- "gha"
12 changes: 12 additions & 0 deletions .github/jactionlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/jdx/jactionlint/main/schema/jactionlint-config.json
rules:
unpinned-uses:
policies:
# security workflows are synchronized from security-tools, pinned to its main branch
"centreon/security-tools/*": ref-pin

# names the workflows may read: a typo in vars.* or secrets.* is an error
config-variables:
- RELEASE_APP_CLIENT_ID
config-secrets:
- RELEASE_APP_PRIVATE_KEY
84 changes: 84 additions & 0 deletions .github/scripts/check-actions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# Repository rules for the shared actions. A package is a top-level directory
# released on its own by release-please; it holds one or several actions.
# - every action belongs to a package declared in release-please-config.json,
# whose component is the directory name (it prefixes the release tags);
# - no local "uses: ./...": it resolves in the caller's workspace, not here;
# an action of the same package is called with "uses: $/<package>/...";
# - no path or "$/" reference leaving the package: a change there would not
# release it;
# - every script an action runs exists and is executable;
# - every remote action is pinned to a full commit SHA with a version comment.
# Usage: check-actions.sh [repository root]
set -euo pipefail

cd "${1:-.}"
CONFIG=release-please-config.json
errors=0

error() {
echo "::error file=$1::$2"
errors=$((errors + 1))
}

mapfile -t action_files < <(find . -mindepth 2 -name action.yml -not -path './.*' | sed 's|^\./||' | sort)

for action_file in "${action_files[@]}"; do
package=${action_file%%/*}
component=$(jq -r --arg p "$package" '.packages[$p].component // empty' "$CONFIG")
if [[ "$component" != "$package" ]]; then
error "$action_file" "package '${package}' must be declared in ${CONFIG} with \"component\": \"${package}\""
fi

while IFS= read -r line; do
number=${line%%:*}
ref=${line#*uses:}
ref=${ref#"${ref%%[![:space:]]*}"}
ref=${ref#[\"\']}
if [[ "$ref" == ./* ]]; then
error "$action_file" "line ${number}: local action '${ref}' resolves in the caller's workspace; use '\$/${package}/...' instead"
elif [[ "$ref" == \$/* ]]; then
target=${ref#\$/}
target=${target%%[[:space:]\"\']*}
if [[ "$target" != "${package}/"* ]]; then
error "$action_file" "line ${number}: '${ref}' leaves the package '${package}'"
elif [[ ! -f "${target}/action.yml" ]]; then
error "$action_file" "line ${number}: '${ref}' is not an action of this repository"
fi
elif [[ ! "$ref" =~ ^[^@[:space:]]+@[0-9a-f]{40}[\"\']?[[:space:]]+#[[:space:]]*v?[0-9]+(\.[0-9]+)*$ ]]; then
error "$action_file" "line ${number}: '${ref}' must be pinned to a full commit SHA followed by a '# vX.Y.Z' comment"
fi
done < <(grep -nE '^[[:space:]-]*uses:' "$action_file" || true)

while IFS= read -r script; do
script_path="${action_file%/*}/${script}"
if [[ ! -f "$script_path" ]]; then
error "$action_file" "script '${script}' does not exist"
elif [[ ! -x "$script_path" ]]; then
error "$action_file" "script '${script}' is not executable"
fi
done < <(grep -oE 'github\.action_path \}\}/[^[:space:]"]+' "$action_file" | sed 's|^github\.action_path }}/||' | sort -u || true)
done

while IFS= read -r package; do
if [[ -z "$(find "$package" -name action.yml 2> /dev/null)" ]]; then
error "$CONFIG" "package '${package}' holds no action.yml"
continue
fi
while IFS= read -r file; do
directory=${file%/*}
depth=$(awk -F/ '{print NF - 1}' <<< "$directory")
while IFS= read -r match; do
ups=$(grep -o '\.\./' <<< "$match" | wc -l)
if ((ups > depth)); then
error "$file" "'${match}' leaves the package '${package}'"
fi
done < <(grep -oE '(\.\./)+' "$file" | sort -u || true)
done < <(find "$package" -type f \( -name '*.yml' -o -name '*.sh' \) -not -path '*/tests/*')
done < <(jq -r '.packages | keys[]' "$CONFIG")

if ((errors > 0)); then
echo "${errors} error(s) found"
exit 1
fi
echo "${#action_files[@]} action(s) checked: packaged, self-contained and SHA-pinned"
14 changes: 14 additions & 0 deletions .github/scripts/check-pr-title.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# release-please only releases conventional commits, and the pull request
# title becomes the squash commit: reject a title it would silently ignore.
# Usage: check-pr-title.sh "<title>"
set -euo pipefail

TITLE="${1:-}"
TYPES="feat|fix|perf|refactor|revert|docs|test|ci|chore"

if [[ ! "$TITLE" =~ ^(${TYPES})(\([a-z0-9._/-]+\))?!?:\ [^[:space:]] ]]; then
echo "::error::'${TITLE}' is not a conventional commit title: <type>(<scope>)!: <description>, type one of ${TYPES//|/, }"
exit 1
fi
echo "Pull request title is a conventional commit title"
103 changes: 103 additions & 0 deletions .github/scripts/tests/check-actions.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env bats

setup() {
SCRIPT="${BATS_TEST_DIRNAME}/../check-actions.sh"
REPO="${BATS_TEST_TMPDIR}/repo"
mkdir -p "${REPO}/tool/run" "${REPO}/tool/lib"
echo '{"packages": {"tool": {"component": "tool"}}}' > "${REPO}/release-please-config.json"
printf '#!/bin/sh\n' > "${REPO}/tool/lib/run.sh"
chmod +x "${REPO}/tool/lib/run.sh"
action 'uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5' \
'run: ${{ github.action_path }}/../lib/run.sh'
}

# write tool/run/action.yml with the given step lines
action() {
{
printf 'runs:\n using: composite\n steps:\n'
printf ' - %s\n' "$@"
} > "${REPO}/tool/run/action.yml"
}

@test "a packaged, self-contained, pinned action passes" {
run "$SCRIPT" "$REPO"
[ "$status" -eq 0 ]
}

@test "an action outside any declared package fails" {
mkdir -p "${REPO}/other"
cp "${REPO}/tool/run/action.yml" "${REPO}/other/action.yml"
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"package 'other' must be declared"* ]]
}

@test "a component that differs from the directory fails" {
echo '{"packages": {"tool": {"component": "tools"}}}' > "${REPO}/release-please-config.json"
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
}

@test "a local uses fails" {
action 'uses: ./.github/actions/foo'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"resolves in the caller's workspace"* ]]
}

@test "an action of the same package through the self-repository syntax passes" {
mkdir -p "${REPO}/tool/setup"
cp "${REPO}/tool/run/action.yml" "${REPO}/tool/setup/action.yml"
action 'uses: $/tool/setup'
run "$SCRIPT" "$REPO"
[ "$status" -eq 0 ]
}

@test "a self-repository reference to another package fails" {
action 'uses: $/other/setup'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"leaves the package 'tool'"* ]]
}

@test "a self-repository reference to a missing action fails" {
action 'uses: $/tool/missing'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"is not an action of this repository"* ]]
}

@test "a tag-pinned action fails" {
action 'uses: actions/checkout@v4'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"must be pinned to a full commit SHA"* ]]
}

@test "a path leaving the package fails" {
action 'run: ${{ github.action_path }}/../../shared/run.sh'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"leaves the package 'tool'"* ]]
}

@test "a declared package without action fails" {
echo '{"packages": {"tool": {"component": "tool"}, "ghost": {"component": "ghost"}}}' > "${REPO}/release-please-config.json"
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"package 'ghost' holds no action.yml"* ]]
}

@test "a script that does not exist fails" {
action 'run: ${{ github.action_path }}/missing.sh'
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"script 'missing.sh' does not exist"* ]]
}

@test "a script that is not executable fails" {
chmod -x "${REPO}/tool/lib/run.sh"
run "$SCRIPT" "$REPO"
[ "$status" -ne 0 ]
[[ "$output" == *"is not executable"* ]]
}
20 changes: 20 additions & 0 deletions .github/scripts/tests/check-pr-title.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bats

setup() {
SCRIPT="${BATS_TEST_DIRNAME}/../check-pr-title.sh"
}

@test "conventional titles are accepted" {
for title in "feat(pulp): add an input" "fix: handle 503" "feat(pulp)!: rename an input" \
"chore(main): release pulp 1.2.0" "fix(deps): bump actions/cache"; do
run "$SCRIPT" "$title"
[ "$status" -eq 0 ] || { echo "rejected: ${title}"; return 1; }
done
}

@test "other titles are rejected" {
for title in "Update action.yml" "feat add an input" "feature(pulp): x" "fix(pulp):missing space" ""; do
run "$SCRIPT" "$title"
[ "$status" -ne 0 ] || { echo "accepted: ${title}"; return 1; }
done
}
80 changes: 80 additions & 0 deletions .github/scripts/tests/update-major-tags.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bats

SHA=0123456789abcdef0123456789abcdef01234567

setup() {
SCRIPT="${BATS_TEST_DIRNAME}/../update-major-tags.sh"
export GITHUB_REPOSITORY=centreon/github-actions
export GH_CALLS="${BATS_TEST_TMPDIR}/calls"
export RELEASE_SHA="$SHA"
: > "$GH_CALLS"
# gh stand-in: logs its arguments; the tag lookup succeeds when TAG_EXISTS=1
mkdir -p "${BATS_TEST_TMPDIR}/bin"
cat > "${BATS_TEST_TMPDIR}/bin/gh" <<'STUB'
#!/usr/bin/env bash
echo "$*" >> "$GH_CALLS"
case "$2" in
repos/*/git/ref/tags/*) [[ "${TAG_EXISTS:-0}" == 1 ]] ;;
repos/*/commits/tags/missing/*) exit 1 ;;
repos/*/commits/tags/*) echo "$RELEASE_SHA" ;;
esac
STUB
chmod +x "${BATS_TEST_TMPDIR}/bin/gh"
PATH="${BATS_TEST_TMPDIR}/bin:${PATH}"
}

releases() {
jq -cn --arg tag "$1" --arg sha "$SHA" \
'{paths_released: "[\"pulp\"]", "pulp--tag_name": $tag, "pulp--sha": $sha}'
}

@test "first release of a major creates its floating tag" {
RELEASES=$(releases pulp/v1.0.0) run "$SCRIPT"
[ "$status" -eq 0 ]
grep -qF -- "-X POST repos/centreon/github-actions/git/refs -f ref=refs/tags/pulp/v1 -f sha=${SHA}" "$GH_CALLS"
}

@test "later release moves the floating tag" {
RELEASES=$(releases pulp/v1.4.2) TAG_EXISTS=1 run "$SCRIPT"
[ "$status" -eq 0 ]
grep -qF -- "-X PATCH repos/centreon/github-actions/git/refs/tags/pulp/v1 -f sha=${SHA} -F force=true" "$GH_CALLS"
}

@test "new major gets its own floating tag" {
RELEASES=$(releases pulp/v2.0.0) run "$SCRIPT"
[ "$status" -eq 0 ]
grep -qF "ref=refs/tags/pulp/v2 " "$GH_CALLS"
! grep -qF "tags/pulp/v1" "$GH_CALLS"
}

@test "nothing released, nothing moved" {
RELEASES='{"releases_created": "false"}' run "$SCRIPT"
[ "$status" -eq 0 ]
[ ! -s "$GH_CALLS" ]
}

@test "unexpected tag format fails without touching tags" {
RELEASES=$(releases pulp-v1.0.0) run "$SCRIPT"
[ "$status" -ne 0 ]
[ ! -s "$GH_CALLS" ]
}

@test "rollback: an explicit release tag moves its major tag back" {
TAG_EXISTS=1 run "$SCRIPT" pulp/v1.4.1
[ "$status" -eq 0 ]
grep -qF -- "-X PATCH repos/centreon/github-actions/git/refs/tags/pulp/v1 -f sha=${SHA} -F force=true" "$GH_CALLS"
}

@test "rollback: an unknown release tag fails without touching tags" {
run "$SCRIPT" missing/v1.0.0
[ "$status" -ne 0 ]
! grep -qE -- "-X (PATCH|POST)" "$GH_CALLS"
}

@test "rollback: anything but a release tag is refused before any api call" {
for tag in pulp/v1 v1.0.0 "pulp/v1.0.0/../../x" "../pulp/v1.0.0" "Pulp/v1.0.0"; do
run "$SCRIPT" "$tag"
[ "$status" -ne 0 ] || { echo "accepted: ${tag}"; return 1; }
done
[ ! -s "$GH_CALLS" ]
}
56 changes: 56 additions & 0 deletions .github/scripts/update-major-tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Point the floating major tag of a package (<package>/vX) at one of its
# releases (<package>/vX.Y.Z).
# - without argument: every package release-please just released, read from
# RELEASES (JSON outputs of release-please-action);
# - with release tags as arguments: those releases, to roll a major tag back.
# Environment: GITHUB_REPOSITORY, GH_TOKEN.
set -euo pipefail

RELEASE_TAG_PATTERN='^([a-z0-9][a-z0-9-]*)/v([0-9]+)\.[0-9]+\.[0-9]+$'

# print the major tag of a release tag, fail on anything else
major_tag_of() {
if [[ ! "$1" =~ $RELEASE_TAG_PATTERN ]]; then
echo "::error::'$1' is not a release tag (<package>/vX.Y.Z)" >&2
return 1
fi
echo "${BASH_REMATCH[1]}/v${BASH_REMATCH[2]}"
}

move_major_tag() {
local tag=$1 sha=$2 major_tag
major_tag=$(major_tag_of "$tag")
if [[ ! "$sha" =~ ^[0-9a-f]{40}$ ]]; then
echo "::error::unexpected commit '${sha}' for the release ${tag}"
return 1
fi

if gh api "repos/${GITHUB_REPOSITORY}/git/ref/tags/${major_tag}" > /dev/null 2>&1; then
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/git/refs/tags/${major_tag}" -f "sha=${sha}" -F force=true > /dev/null
else
gh api -X POST "repos/${GITHUB_REPOSITORY}/git/refs" -f "ref=refs/tags/${major_tag}" -f "sha=${sha}" > /dev/null
fi
echo "${major_tag} -> ${tag} (${sha})"
echo "- \`${major_tag}\` now points at \`${tag}\`" >> "${GITHUB_STEP_SUMMARY:-/dev/null}"
}

if (($# > 0)); then
for tag in "$@"; do
# validated before it reaches an api path: the tag is typed by a person
major_tag_of "$tag" > /dev/null
if ! sha=$(gh api "repos/${GITHUB_REPOSITORY}/commits/tags/${tag}" --jq .sha); then
echo "::error::release tag '${tag}' does not exist"
exit 1
fi
move_major_tag "$tag" "$sha"
done
exit 0
fi

paths=$(jq -r '(.paths_released // "[]") | fromjson | .[]' <<< "$RELEASES")
for path in $paths; do
tag=$(jq -r --arg key "${path}--tag_name" '.[$key] // empty' <<< "$RELEASES")
sha=$(jq -r --arg key "${path}--sha" '.[$key] // empty' <<< "$RELEASES")
move_major_tag "$tag" "$sha"
done
Loading
Loading