Skip to content

Commit

Permalink
Update CI/scripts to match template repo (apollodao#127)
Browse files Browse the repository at this point in the history
* ci: Update to match template repo

Updates the scripts and workflows to match
those in the `apollo-template` repo.

Signed-off-by: Bobby <[email protected]>

* ci: Add todo script to Makefile.toml

Signed-off-by: Bobby <[email protected]>

* ci: Fix discrepancy in clippy usage

Fixes a discrepancy between how clippy is used
in Makefile.toml and in the lint-format.yml workflow

Signed-off-by: Bobby <[email protected]>

* ci: updates to template

---------

Signed-off-by: Bobby <[email protected]>
Co-authored-by: Sturdy <[email protected]>
  • Loading branch information
apollo-bobby and apollo-sturdy authored Feb 13, 2023
1 parent 97b88f5 commit 1c2bdcd
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 92 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/changelog.yml

This file was deleted.

9 changes: 6 additions & 3 deletions .github/workflows/lint-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:

jobs:
lint-and-format:
name: Lint and format
name: Lint and check formatting
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand All @@ -32,16 +32,19 @@ jobs:
with:
toolchain: nightly
command: clippy
args: --fix -- -D warnings
args: --all-features -- -D warnings

- name: Run cargo fmt
uses: actions-rs/cargo@v1
with:
toolchain: nightly
command: fmt
args: --all -- --check
args: --all -- --check --verbose

- name: Run cargo machete
uses: actions-rs/cargo@v1
with:
command: machete

- name: Lint todo comments
run: ./scripts/todo-lint.sh
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Test Suite
on:
pull_request:
workflow_dispatch:
Expand Down
42 changes: 0 additions & 42 deletions .github/workflows/version.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target
.idea
.vscode
docs
**/*.tmp
5 changes: 5 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ command = "cargo"
args = ["check"]

[tasks.clippy-check]
toolchain = "nightly"
command = "cargo"
args = ["clippy","--all-features","--","-D","warnings"]
[tasks.clippy-fix]
toolchain = "nightly"
command = "cargo"
args = ["clippy","--fix","--allow-staged","--","-D","warnings"]

[tasks.todo-check]
script = { file = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/scripts/todo-lint.sh", absolute_path = true }

# This task requires the `cargo-tarpaulin` package: https://crates.io/crates/cargo-tarpaulin
[tasks.cov]
command = "cargo"
Expand Down
4 changes: 3 additions & 1 deletion cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ commit_parsers = [
{ message = "^test*", group = "Testing" },
{ message = "^chore\\(release\\): prepare for*", skip = true },
{ message = "^chore*", group = "Miscellaneous Tasks" },
{ message = "^ci*", group = "Continuous Integration/Deployment" },
{ message = "^build*", group = "Building, Automation and Tooling" },
{ body = ".*security", group = "Security" },
]
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
# regex for skipping tags
skip_tags = "v0.1.0-beta.1"
skip_tags = "v0.1.0-beta.1"
3 changes: 1 addition & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ignore = [
]

[bans]
multiple-versions = "allow" # TODO:update this
multiple-versions = "allow"
deny = []
skip = []

Expand Down Expand Up @@ -56,4 +56,3 @@ expression = "ISC"
license-files = [
{ path = "LICENSE", hash = 0xbd0eed23 },
]

98 changes: 98 additions & 0 deletions scripts/bpsync.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash
# Script for syncing branch protection rules between repos.
# By default copies rules from the `apollo-template` repo to specified branch in the current repo.

if [[ -z $1 || -z $2 ]]; then
echo "Usage: ./bpsync.sh <GitHub PAT> <target repo> <target branch>"
exit 1
fi

# Dependency check
DEPS=(jq curl)
for dep in $DEPS; do
if [ ! $(which $dep) ]; then
echo "'$dep' is not installed. Exiting."
exit 1
fi
done

# personal access token
PAT=$1
# source/target branch for copying protection rules
BRANCH=$2

SRC_OWNER=apollodao
SRC_REPO=apollo-template
SRC_BRANCH=$BRANCH
SRC_PAT=$PAT

TGT_OWNER=apollodao
TGT_REPO=$(basename $(git rev-parse --show-toplevel))
TGT_BRANCH=$BRANCH
TGT_PAT=$PAT

# GET branch protection rules
HTTP_RESP=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${SRC_PAT}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
-w '%{http_code}' \
-o GET_response.tmp \
https://api.github.com/repos/$SRC_OWNER/$SRC_REPO/branches/$SRC_BRANCH/protection \
2>/dev/null
)

if [ $HTTP_RESP != 200 ]; then
echo "Failed to get branch protection rules!"
printf "HTTP Response %s\n" $HTTP_RESP
exit 1
fi

echo "Successfully fetched branch protection rules!"

# Prepare branch protection rules for PUT request
PAYLOAD=$(
cat GET_response.tmp \
| jq -c \
'del(
.required_signatures, # Delete "required_signatures"
..| # Recurse..
.url?, # ..and delete "url",
.contexts_url?, # "contexts_url" and "contexts"
.contexts? # fields in the JSON
)
| walk( # Recurse again and flatten
# objects with one field
if type == "object" and length == 1 then
.[]
else
.
end
)
| .restrictions |= null # Add "restrictions" field'
)

# Try updating branch protection with shiny new JSON payload
HTTP_RESP=$(curl \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${TGT_PAT}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
-w '%{http_code}' \
-o PUT_response.tmp \
https://api.github.com/repos/$TGT_OWNER/$TGT_REPO/branches/$TGT_BRANCH/protection \
-d "${PAYLOAD}" \
2>/dev/null
)

if [ $HTTP_RESP != 200 ]; then
echo "Failed to copy branch protection rules!"
echo "HTTP Response ${HTTP_RESP}"
echo "ERROR MESSAGE: $(cat PUT_response.tmp | jq '.message')"
exit 1
fi

echo "Successfully copied branch protection rules!"
# Clean up responses if successful
rm GET_response.tmp PUT_response.tmp
exit 0
26 changes: 19 additions & 7 deletions install-git-hooks.sh → scripts/install-git-hooks.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#!/bin/sh

hooks="${PWD}/.git/hooks"
if [ ! $(which git) ]; then
echo "'git' is not installed. Exiting."
exit 1
fi

if [ $(git rev-parse --is-inside-work-tree) ]; then
GIT_ROOT=$(git rev-parse --show-toplevel)
else
echo "You are not inside a git repository. Exiting."
exit 1
fi

HOOKS="$GIT_ROOT/.git/hooks"

## Pre-commit
if ! cargo make --version 2&>/dev/null; then
Expand All @@ -16,20 +28,20 @@ fi
echo "============================"
echo "=== Copy pre-commit hook ==="
echo "============================\n"
cp .pre-commit.sh "${hooks}/pre-commit"
chmod u+x "${hooks}/pre-commit"
cp $GIT_ROOT/pre-commit.sh "$HOOKS/pre-commit"
chmod u+x "$HOOKS/pre-commit"

## Commit-msg
echo "========================================"
echo "=== Installing sailr commit-msg hook ==="
echo "========================================\n"

# sailr requires `jq` to be installed
script_file="https://raw.githubusercontent.com/apollodao/sailr/master/sailr.sh"
SCRIPT_FILE="https://raw.githubusercontent.com/apollodao/sailr/master/sailr.sh"

if curl $script_file -o "${hooks}/commit-msg"; then
chmod u+x "${hooks}/commit-msg"
echo "\nInstalled Sailr as commit-msg hook in $hooks."
if curl $SCRIPT_FILE -o "$HOOKS/commit-msg"; then
chmod u+x "$HOOKS/commit-msg"
echo "\nInstalled Sailr as commit-msg hook in $HOOKS."
echo "For usage see https://github.com/apollodao/sailr#usage\n"
else
echo "\nCould not install Sailr."
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit.sh → scripts/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if ! cargo make --version 2&>/dev/null; then
fi

makefile="$PWD/Makefile.toml"
steps="clippy-check format-check machete-check"
steps="clippy-check format-check machete-check todo-check"

# TODO: save staged and non-staged files here and automatically
# `git add` any new modified files between steps to avoid having the user
Expand Down
35 changes: 35 additions & 0 deletions scripts/todo-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

# Escape codes
RESET="\x1b[0m"
RED="\x1b[31;49m"
GREEN="\x1b[32;49m"
BD="\x1b[39;49;1m"
IT="\x1b[39;49;3m"
UL="\x1b[39;49;4m"

# if path is supplied as argument
if [[ ! -z $1 && -d $1 ]]; then
GREP_DIR=$1
echo "${BD}Searching in '$RESET$UL$GREP_DIR$RESET$BD'...$RESET"
else
GREP_DIR="."
echo "${BD}No path supplied. Defaulting to current working directory...$RESET"
fi

# Regex
LINT="todo[^!]"
FORMAT="s/\.\/([a-zA-Z0-9_/.-]+):([0-9]+):(.+)/$UL\1$RESET ${BD}@ line \2:$RESET\n\t$IT$RED\3$RESET/"

N=$(grep -riIo --include=*.{rs,toml,md,ts,js} -E $LINT $GREP_DIR | wc -l | xargs)


if [ $N -gt 0 ]; then
echo "${BD}Found $UL$RED$N$RESET$BD occurrences matching pattern '$RESET$IT$LINT$RESET$BD':$RESET"
echo "------------------------------------------------"
grep -rniI --include=*.{rs,toml,md,ts,js} -E $LINT $GREP_DIR | sed -E "$FORMAT"
exit 1
fi

echo "${GREEN}No occurrences of pattern '$IT$LINT$RESET$GREEN' found!$RESET"
exit 0
2 changes: 1 addition & 1 deletion tarpaulin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ exclude-files=[
]
fail-under=0 # TODO: update this
no-fail-fast=true
skip-clean=false
skip-clean=false

0 comments on commit 1c2bdcd

Please sign in to comment.