Skip to content
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

Add action that checks git rev deps for orphan likeliness #56

Merged
merged 7 commits into from
Aug 30, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
| ---- | ---- | ----------- |
| [rust-cache] | Action | Caches dependencies, install artifacts, and build artifacts in Rust projects. |
| [rust-set-rust-version] | Workflow | Updates the rust-version in Rust crates to the latest stable version. |
| [rust-check-git-rev-deps] | Workflow | Check that git rev dependencies do not reference revisions likely to be orphaned. |

#### Releasing / Publishing

Expand Down
18 changes: 18 additions & 0 deletions rust-check-git-rev-deps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# rust-check-git-rev-deps Action

The rust-check-git-rev-deps action checks that `git` dependencies that are
versioned by a commit sha are not referring to a commit that is likely to become
orphaned.

Commits are accepted by the check if they can be found in one of:
- The history of the default branch of the repository.
- The history of a tag of the repository.

## Usage

```yml
jobs:
my_job:
steps:
- uses: stellar/actions/rust-check-git-rev-deps@main
```
7 changes: 7 additions & 0 deletions rust-check-git-rev-deps/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Rust Check Git Rev Deps'
description: 'Check that git revision dependencies are unlikely to be orphaned'
runs:
using: "composite"
steps:
- shell: bash
run: ${{ github.action_path }}/check.sh
41 changes: 41 additions & 0 deletions rust-check-git-rev-deps/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

# Get repo and sha's of all dependencies in the current cargo workspace that are
# git references containing a revision/commit sha.
cargo metadata --format-version 1 --no-deps \
| jq -r '[.packages[].dependencies] | flatten | [.[] | .source | select(. != null) | select(. | startswith("git+")) | select(. | contains("?rev=")) | sub("^git\\+"; "") | split("?rev=")] | unique | sort | .[] | @tsv' \
| {
fails=0
while IFS=$'\t' read -r repo sha; do
# For each, clone the repo, checkout the default branch and tags, and check
# that the commit can be found in at least one of them.
temp=$(mktemp -d)
echo -e "\033[1;34mChecking "$repo" @ "$sha"\033[0m"
git clone "$repo" "$temp"
pushd "$temp"
found=0
for ref in . $(git tag); do
git -c advice.detachedHead=false checkout "$ref"
desc="$(git describe --all)"
echo -e "\033[1;33mChecking is in "$desc"\033[0m"
if git merge-base --is-ancestor "$sha" HEAD; then
echo -e "\033[1;32mCommit is in the history of $desc.\033[0m"
found=1
else
echo -e "Commit is NOT in the history of $desc."
fi
done
if (( $found == 0 )); then
fails=1
fi
popd
done
if (( $fails > 0 )); then
echo -e "\033[1;31mDependency revisions must reference a version of the dependency that is on the default branch, or a tag, so that they do not become orphaned.\033[0m"
exit 1
fi
}