Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ Example showing all options:
branch: ${{ github.head_ref || github.ref_name }}
empty: true
file_pattern: '*.txt *.md *.json *.hcl'
repository: 'path/to/subdir'
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
```

The `repository` input is optional. When set, it must be a relative path under
`$GITHUB_WORKSPACE` pointing to the git repository to operate on. Defaults to
the root of the repository (`.`).

See [`action.yaml`](./action.yaml) for current list of supported inputs.

## Releasing
Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
description: File pattern used for `git add`. For example `src/*.js`
required: false
default: "."
repository:
description: Relative file path under $GITHUB_WORKSPACE to the git repository. Defaults to the root of the repository (`.`).
required: false
default: "."

outputs:
commit-url:
Expand All @@ -39,3 +43,4 @@ runs:
- ${{ inputs.branch }}
- ${{ inputs.empty }}
- ${{ inputs.file_pattern }}
- ${{ inputs.repository }}
8 changes: 7 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ REPO="${2:?Missing repo input}"
BRANCH="${3:?Missing branch input}"
EMPTY="${4:-false}"
read -r -a FILE_PATTERNS <<<"${5:?Missing file_pattern input}"
REPOSITORY="${6:-.}"

git config --global --add safe.directory "$GITHUB_WORKSPACE"
REPOSITORY_PATH="$GITHUB_WORKSPACE/$REPOSITORY"
echo "Repository path: $REPOSITORY_PATH"

git config --global --add safe.directory "$REPOSITORY_PATH"

cd "$REPOSITORY_PATH"

adds=()
deletes=()
Expand Down
36 changes: 31 additions & 5 deletions tests/entrypoint.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
# export GHCOMMIT_STUB_DEBUG=/dev/tty

setup() {
export GITHUB_WORKSPACE=/tmp
export GITHUB_WORKSPACE="$BATS_TEST_DIRNAME/.."
#export DEBUG=1
}

Expand All @@ -25,7 +25,7 @@ setup() {
# to modify and prevent cat from removing the leading space on lines/entries since that is a part
# of the git status output.
stub git \
"config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \
"config --global --add safe.directory $GITHUB_WORKSPACE/. : echo stubbed" \
"status -s --porcelain=v1 -z -- . : cat ./tests/fixtures/git-status.out-1 | tr '\n' '\0'"

stub ghcommit \
Expand All @@ -46,7 +46,7 @@ setup() {
local file_pattern='.'

stub git \
"config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \
"config --global --add safe.directory $GITHUB_WORKSPACE/. : echo stubbed" \
"status -s --porcelain=v1 -z -- . : echo"

run ./entrypoint.sh "$commit_message" "$repo" "$branch" "$empty" "$file_pattern"
Expand All @@ -64,7 +64,7 @@ setup() {
export GITHUB_OUTPUT="$BATS_TEST_TMPDIR/github-output"

stub git \
"config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \
"config --global --add safe.directory $GITHUB_WORKSPACE/. : echo stubbed" \
"status -s --porcelain=v1 -z -- . : echo"

stub ghcommit \
Expand All @@ -77,6 +77,32 @@ setup() {
assert_file_contains "$GITHUB_OUTPUT" "commit-url=https://localhost/foo"
}

@test "switches to repository subdirectory when repository input is set" {
local commit_message='msg'
local repo='org/repo'
local branch='main'
local empty='false'
local file_pattern='.'
local repository='tests/fixtures'

export GITHUB_OUTPUT="$BATS_TEST_TMPDIR/github-output"

stub git \
"config --global --add safe.directory $GITHUB_WORKSPACE/tests/fixtures : echo stubbed" \
"status -s --porcelain=v1 -z -- . : echo"

stub ghcommit \
'-b main -r org/repo -m msg --empty : echo Success. New commit: https://localhost/sub'

# Use --empty to force ghcommit to run even without staged changes, so we can
# verify the working directory change took effect (otherwise the stub for
# ghcommit would not be exercised).
run ./entrypoint.sh "$commit_message" "$repo" "$branch" "true" "$file_pattern" "$repository"
assert_success
assert_output --partial "Repository path: $GITHUB_WORKSPACE/tests/fixtures"
assert_output --partial "Success"
}

@test "handles untracked files" {
local commit_message='msg'
local repo='org/repo'
Expand All @@ -87,7 +113,7 @@ setup() {
export GITHUB_OUTPUT="$BATS_TEST_TMPDIR/github-output"

stub git \
"config --global --add safe.directory $GITHUB_WORKSPACE : echo stubbed" \
"config --global --add safe.directory $GITHUB_WORKSPACE/. : echo stubbed" \
"status -s --porcelain=v1 -z -- . : cat ./tests/fixtures/git-status.out-2 | tr '\n' '\0'"

stub ghcommit \
Expand Down