Skip to content

Commit

Permalink
Merge pull request #6 from planetscale/empty-commits
Browse files Browse the repository at this point in the history
  • Loading branch information
joemiller authored Apr 14, 2023
2 parents 4d81bb7 + f3bf348 commit cfeb49a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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}}
Expand Down
19 changes: 12 additions & 7 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 }}
24 changes: 16 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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=}"
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[@]}"
25 changes: 23 additions & 2 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ setup() {
local commit_message='msg'
local repo='org/repo'
local branch='main'
local empty='false'
local file_pattern='.'

stub git \
Expand All @@ -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"
}
Expand All @@ -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"
}

0 comments on commit cfeb49a

Please sign in to comment.