diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml deleted file mode 100644 index e5429ea..0000000 --- a/.github/workflows/changelog.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Changelog -on: - pull_request: - branches: - - master - workflow_dispatch: - -env: - file_name: CHANGELOG.md - -jobs: - changelog: - name: Generate changelog - runs-on: ubuntu-latest - steps: - - name: Checkout sources - uses: actions/checkout@v3 - - - name: Generate a changelog - uses: orhun/git-cliff-action@v1 - id: git-cliff - with: - config: cliff.toml - args: --verbose - env: - OUTPUT: ${{ env.file_name }} - - - name: Commit changelog - if: ${{ github.event_name != 'workflow_dispatch' }} - uses: EndBug/add-and-commit@v9.1.1 - with: - add: '${{ env.file_name }}' - message: 'Generate ${{ env.file_name }}' - push: 'HEAD:master' diff --git a/.github/workflows/lint-format.yml b/.github/workflows/lint-format.yml index 6806110..4d1e9b3 100644 --- a/.github/workflows/lint-format.yml +++ b/.github/workflows/lint-format.yml @@ -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 @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8e9d79c..ab1df49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Test +name: Test Suite on: pull_request: workflow_dispatch: diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml deleted file mode 100644 index ff187c2..0000000 --- a/.github/workflows/version.yml +++ /dev/null @@ -1,42 +0,0 @@ - -name: Versioning -on: - pull_request: - branches: - - master - workflow_dispatch: - -jobs: - semver-set: - name: Set next version number - runs-on: ubuntu-latest - steps: - - name: Checkout pull request - uses: actions/checkout@v3 - - - name: Determine version number - id: semver - uses: ietf-tools/semver-action@v1 - with: - token: ${{ github.token }} - branch: master - majorList: feat!, feature! fix!, bugfix!, perf!, refactor!, chore! - minorList: feat, feature - patchList: fix, bugfix, perf - - - name: Update Cargo.toml - id: update - uses: ciiiii/toml-editor@1.0.0 - with: - file: "Cargo.toml" - key: "package.version" - value: "${{ steps.semver.outputs.nextStrict }}" - - - name: Commit changes, tag and push - uses: EndBug/add-and-commit@v9.1.1 - with: - add: 'Cargo.toml' - tag: '${{ steps.semver.outputs.next }}' - message: Update Cargo.toml version from ${{ steps.semver.outputs.current }} to ${{ steps.semver.outputs.next }} - default_author: github_actions - push: true diff --git a/.gitignore b/.gitignore index fd13564..b69228e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target .idea .vscode docs +**/*.tmp diff --git a/Makefile.toml b/Makefile.toml index da99619..0704fc3 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -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" diff --git a/cliff.toml b/cliff.toml index 23347d9..ddec062 100644 --- a/cliff.toml +++ b/cliff.toml @@ -42,6 +42,8 @@ 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 @@ -49,4 +51,4 @@ 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" \ No newline at end of file +skip_tags = "v0.1.0-beta.1" diff --git a/deny.toml b/deny.toml index ddb6ef5..e721107 100644 --- a/deny.toml +++ b/deny.toml @@ -19,7 +19,7 @@ ignore = [ ] [bans] -multiple-versions = "allow" # TODO:update this +multiple-versions = "allow" deny = [] skip = [] @@ -56,4 +56,3 @@ expression = "ISC" license-files = [ { path = "LICENSE", hash = 0xbd0eed23 }, ] - diff --git a/scripts/bpsync.sh b/scripts/bpsync.sh new file mode 100755 index 0000000..ccc4b56 --- /dev/null +++ b/scripts/bpsync.sh @@ -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 " + 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 diff --git a/install-git-hooks.sh b/scripts/install-git-hooks.sh similarity index 59% rename from install-git-hooks.sh rename to scripts/install-git-hooks.sh index eab89c7..4d83210 100755 --- a/install-git-hooks.sh +++ b/scripts/install-git-hooks.sh @@ -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 @@ -16,8 +28,8 @@ 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 "========================================" @@ -25,11 +37,11 @@ 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." diff --git a/.pre-commit.sh b/scripts/pre-commit.sh similarity index 95% rename from .pre-commit.sh rename to scripts/pre-commit.sh index 7326daa..d001966 100755 --- a/.pre-commit.sh +++ b/scripts/pre-commit.sh @@ -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 diff --git a/scripts/todo-lint.sh b/scripts/todo-lint.sh new file mode 100755 index 0000000..14f5c4b --- /dev/null +++ b/scripts/todo-lint.sh @@ -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 diff --git a/tarpaulin.toml b/tarpaulin.toml index 9a91b2a..ae105bb 100644 --- a/tarpaulin.toml +++ b/tarpaulin.toml @@ -7,4 +7,4 @@ exclude-files=[ ] fail-under=0 # TODO: update this no-fail-fast=true -skip-clean=false \ No newline at end of file +skip-clean=false