diff --git a/Dockerfile b/Dockerfile index 1959896..5cb92a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ghcr.io/planetscale/ghcommit:v0.0.9 AS ghcommit +FROM ghcr.io/planetscale/ghcommit:v0.1.0 AS ghcommit FROM alpine:3.17 AS base diff --git a/README.md b/README.md index 10f99e3..81dbb46 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ jobs: # Run steps that make changes to the local repo here. # Commit all changed files back to the repository - - uses: planetscale/ghcommit-action@v0.0.6 + - uses: planetscale/ghcommit-action@v0.1.0 with: commit_message: "🤖 fmt" repo: ${{ github.repository }} @@ -55,13 +55,13 @@ jobs: Example showing all options: - ```yaml - - uses: planetscale/ghcommit-action@v0.0.6 + - uses: planetscale/ghcommit-action@v0.1.0 with: commit_message: "🤖 fmt" repo: ${{ github.repository }} branch: ${{ github.head_ref || github.ref_name }} + empty: true file_pattern: '*.txt *.md *.json *.hcl' env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/action.yaml b/action.yaml index 98ab3ec..14aa41c 100644 --- a/action.yaml +++ b/action.yaml @@ -1,9 +1,9 @@ -name: 'Commit git changes' -description: 'Commit git changes directly to GitHub using the GraphQL API' +name: "Commit git changes" +description: "Commit git changes directly to GitHub using the GraphQL API" branding: - color: 'orange' - icon: 'git-commit' + color: "orange" + icon: "git-commit" inputs: commit_message: @@ -15,16 +15,21 @@ inputs: branch: description: The name of the branch to commit to. required: true + empty: + description: Allow making an empty commit if there are no changes. + required: false + default: "false" file_pattern: description: File pattern used for `git add`. For example `src/*.js` required: false - default: '.' + default: "." runs: - using: 'docker' - image: 'Dockerfile' + using: "docker" + image: "Dockerfile" args: - ${{ inputs.commit_message }} - ${{ inputs.repo }} - ${{ inputs.branch }} + - ${{ inputs.empty }} - ${{ inputs.file_pattern }} diff --git a/entrypoint.sh b/entrypoint.sh index 7ed51f0..d13817a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -5,7 +5,8 @@ set -euo pipefail COMMIT_MESSAGE="${1:?Missing commit_message input}" REPO="${2:?Missing repo input}" BRANCH="${3:?Missing branch input}" -FILE_PATTERN="${4:?Missing file_pattern input}" +EMPTY="${4:-false}" +FILE_PATTERN="${5:?Missing file_pattern input}" git config --global --add safe.directory "$GITHUB_WORKSPACE" @@ -37,14 +38,21 @@ while IFS= read -r -d $'\0' line; do # 'R main.sh -> main.sh.new' done < <(git status -s --porcelain=v1 -z -- $FILE_PATTERN) -if [[ "${#adds[@]}" -eq 0 && "${#deletes[@]}" -eq 0 ]]; then +if [[ "${#adds[@]}" -eq 0 && "${#deletes[@]}" -eq 0 && "$EMPTY" == "false" ]]; then echo "No changes detected, exiting" exit 0 fi -ghcommit \ - -b "$BRANCH" \ - -r "$REPO" \ - -m "$COMMIT_MESSAGE" \ - "${adds[@]/#/--add=}" \ - "${deletes[@]/#/--delete=}" \ No newline at end of file +ghcommit_args=() +ghcommit_args+=(-b "$BRANCH") +ghcommit_args+=(-r "$REPO") +ghcommit_args+=(-m "$COMMIT_MESSAGE") + +if [[ "$EMPTY" =~ ^(true|1|yes)$ ]]; then + ghcommit_args+=(--empty) +fi + +ghcommit_args+=("${adds[@]/#/--add=}") +ghcommit_args+=("${deletes[@]/#/--delete=}") + +ghcommit "${ghcommit_args[@]}" \ No newline at end of file diff --git a/tests/entrypoint.bats b/tests/entrypoint.bats index cbd07db..34c0b9d 100644 --- a/tests/entrypoint.bats +++ b/tests/entrypoint.bats @@ -14,6 +14,7 @@ setup() { local commit_message='msg' local repo='org/repo' local branch='main' + local empty='false' local file_pattern='.' stub git \ @@ -23,7 +24,7 @@ setup() { stub ghcommit \ '-b main -r org/repo -m msg --add=README.md --add=foo.txt --delete=\""a path with spaces oh joy/file.txt\"" : echo Success' - run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$file_pattern" + run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$empty" "$file_pattern" assert_success assert_output --partial "Success" } @@ -32,13 +33,33 @@ setup() { local commit_message='msg' local repo='org/repo' local branch='main' + local empty='false' local file_pattern='.' stub git \ "config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \ "status -s --porcelain=v1 -z -- . : echo" - run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$file_pattern" + run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$empty" "$file_pattern" assert_success assert_output --partial "No changes detected" } + +@test "no changes with --empty flag creates empty commit" { + local commit_message='msg' + local repo='org/repo' + local branch='main' + local empty='true' + local file_pattern='.' + + stub git \ + "config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \ + "status -s --porcelain=v1 -z -- . : echo" + + stub ghcommit \ + '-b main -r org/repo -m msg --empty : echo Success' + + run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$empty" "$file_pattern" + assert_success + assert_output --partial "Success" +}