Compare commits

..

6 Commits

Author SHA1 Message Date
Constantin Comendant 8621497c8c
Fail if attempting to execute git commands in a directory that is not a git-repo. (#326)
* Fail (and log message) if attempting to execute git commands in a directory that is not a git-repo.

* Add Test

* Code Formatting

---------

Co-authored-by: Constantin Comendant <constantin@uberagent.com>
Co-authored-by: Stefan Zweifel <stefan@stefanzweifel.dev>
2024-04-11 21:00:36 +02:00
dependabot[bot] b0f4d47f59
Bump bats from 1.10.0 to 1.11.0 (#325)
Bumps [bats](https://github.com/bats-core/bats-core) from 1.10.0 to 1.11.0.
- [Release notes](https://github.com/bats-core/bats-core/releases)
- [Changelog](https://github.com/bats-core/bats-core/blob/master/docs/CHANGELOG.md)
- [Commits](https://github.com/bats-core/bats-core/compare/v1.10.0...v1.11.0)

---
updated-dependencies:
- dependency-name: bats
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-03-26 08:29:07 +01:00
Christian Vermeulen 9b5e5ee10a
Add step id explanation for output in README.md (#324) 2024-03-15 16:06:58 +01:00
Philip Couling e040c596f0
Linux is not UNIX (#321) 2024-02-22 08:26:06 +01:00
dependabot[bot] 4d160c5e4d
Bump release-drafter/release-drafter from 5 to 6 (#319) 2024-02-06 07:16:57 +01:00
Nikita Panuhin 12f68633e4
Clarify `commit_author` input option (#315)
* Clarify `commit_author` input option

* Update README.md

---------

Co-authored-by: Stefan Zweifel <stefanzweifel@users.noreply.github.com>
2023-12-24 07:35:30 +01:00
7 changed files with 162 additions and 48 deletions

View File

@ -15,6 +15,6 @@ jobs:
contents: write
steps:
- uses: release-drafter/release-drafter@v5
- uses: release-drafter/release-drafter@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@ -48,7 +48,7 @@ jobs:
```
> [!NOTE]
> The Action has to be used in a Job that runs on a UNIX system (e.g. `ubuntu-latest`).
> The Action has to be used in a Job that runs on a UNIX-like system (e.g. `ubuntu-latest`).
The following is an extended example with all available options.
@ -59,8 +59,9 @@ The following is an extended example with all available options.
# Defaults to "Apply automatic changes"
commit_message: Automated Change
# Optional. Remote branch name where commit is going to be pushed to.
# Defaults to the current branch.
# Optional. Local and remote branch name where commit is going to be pushed
# to. Defaults to the current branch.
# You might need to set `create_branch: true` if the branch does not exist.
branch: feature-123
# Optional. Options used by `git-commit`.
@ -81,7 +82,7 @@ The following is an extended example with all available options.
# Optional commit user and author settings
commit_user_name: My GitHub Actions Bot # defaults to "github-actions[bot]"
commit_user_email: my-github-actions-bot@example.org # defaults to "41898282+github-actions[bot]@users.noreply.github.com"
commit_author: Author <actions@github.com> # defaults to author of the commit that triggered the run
commit_author: Author <actions@github.com> # defaults to "username <username@users.noreply.github.com>", where "username" belongs to the author of the commit that triggered the run
# Optional. Tag name being created in the local repository and
# pushed to remote repository and defined branch.
@ -101,10 +102,19 @@ The following is an extended example with all available options.
# Optional. Disable dirty check and always try to create a commit and push
skip_dirty_check: true
# Optional. Skip internal call to `git fetch`
skip_fetch: true
# Optional. Skip internal call to `git checkout`
skip_checkout: true
# Optional. Prevents the shell from expanding filenames.
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
disable_globbing: true
# Optional. Create given branch name in local and remote repository.
create_branch: true
```
Please note that the Action depends on `bash`. If you're using the Action in a job in combination with a custom Docker container, make sure that `bash` is installed.
@ -157,9 +167,16 @@ You can use these outputs to trigger other Actions in your Workflow run based on
- `changes_detected`: Returns either "true" or "false" if the repository was dirty and files have changed.
- `commit_hash`: Returns the full hash of the commit if one was created.
**⚠️ When using outputs, the step needs to be given an id. See example below.**
### Example
```yaml
- uses: stefanzweifel/git-auto-commit-action@v5
id: auto-commit-action #mandatory for the output to show up in ${{ steps }}
with:
commit_message: Apply php-cs-fixer changes
- name: "Run if changes have been detected"
if: steps.auto-commit-action.outputs.changes_detected == 'true'
run: echo "Changes!"
@ -343,6 +360,8 @@ If you would like to use this Action to create a commit using [`--amend`](https:
First, you need to extract the previous commit message by using `git log -1 --pretty=%s`.
Then you need to provide this last commit message to the Action through the `commit_message` input option.
By default, the commit author is changed to `username <username@users.noreply.github.com>`, where `username` is the name of the user who triggered the workflow (The [`github.actor`](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context) context is used here). If you want to preserve the name and email of the original author, you must extract them from the last commit and provide them to the Action through the `commit_author` input option.
Finally, you have to use `push_options: '--force'` to overwrite the git history on the GitHub remote repository. (git-auto-commit will not do a `git-rebase` for you!)
The steps in your workflow might look like this:
@ -356,15 +375,18 @@ The steps in your workflow might look like this:
# Other steps in your workflow to trigger a changed file
- name: Get last commit message
id: last-commit-message
id: last-commit
run: |
echo "msg=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
echo "message=$(git log -1 --pretty=%s)" >> $GITHUB_OUTPUT
echo "author=$(git log -1 --pretty=\"%an <%ae>\")" >> $GITHUB_OUTPUT
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: ${{ steps.last-commit-message.outputs.msg }}
commit_author: ${{ steps.last-commit.outputs.author }}
commit_message: ${{ steps.last-commit.outputs.message }}
commit_options: '--amend --no-edit'
push_options: '--force'
skip_fetch: true
```
See discussion in [#159](https://github.com/stefanzweifel/git-auto-commit-action/issues/159#issuecomment-845347950) for details.

View File

@ -56,9 +56,20 @@ inputs:
description: Skip the check if the git repository is dirty and always try to create a commit.
required: false
default: false
skip_fetch:
description: Skip the call to git-fetch.
required: false
default: false
skip_checkout:
description: Skip the call to git-checkout.
required: false
default: false
disable_globbing:
description: Stop the shell from expanding filenames (https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html)
default: false
create_branch:
description: Create new branch with the name of `branch`-input in local and remote repository, if it doesn't exist yet.
default: false
internal_git_binary:
description: Internal use only! Path to git binary used to check if git is available. (Don't change this!)
default: git

View File

@ -35,6 +35,8 @@ _main() {
_set_github_output "changes_detected" "true"
_switch_to_branch
_add_files
# Check dirty state of repo again using git-diff.
@ -80,8 +82,41 @@ _git_is_dirty() {
echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";
read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN";
# capture stderr
gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)";
# shellcheck disable=SC2086
[ -n "$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})" ]
gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})";
if [ $? -ne 0 ]; then
_log "error" "git-status failed with:<$gitStatusMessage>";
exit 1;
fi
[ -n "$gitStatus" ]
}
_switch_to_branch() {
echo "INPUT_BRANCH value: $INPUT_BRANCH";
# Fetch remote to make sure that repo can be switched to the right branch.
if "$INPUT_SKIP_FETCH"; then
_log "debug" "git-fetch will not be executed.";
else
git fetch --depth=1;
fi
# If `skip_checkout`-input is true, skip the entire checkout step.
if "$INPUT_SKIP_CHECKOUT"; then
_log "debug" "git-checkout will not be executed.";
else
# Create new local branch if `create_branch`-input is true
if "$INPUT_CREATE_BRANCH"; then
# shellcheck disable=SC2086
git checkout -B $INPUT_BRANCH --;
else
# Switch to branch from current Workflow run
# shellcheck disable=SC2086
git checkout $INPUT_BRANCH --;
fi
fi
}
_add_files() {
@ -129,8 +164,6 @@ _tag_commit() {
_push_to_github() {
echo "INPUT_BRANCH value: $INPUT_BRANCH";
echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}";
_log "debug" "Apply push options ${INPUT_PUSH_OPTIONS}";

View File

@ -1,6 +1,6 @@
{
"devDependencies": {
"bats": "^1.10.0",
"bats": "^1.11.0",
"bats-assert": "ztombol/bats-assert",
"bats-support": "ztombol/bats-support"
},

View File

@ -8,6 +8,7 @@ setup() {
export FAKE_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/tests_local_repository"
export FAKE_REMOTE="${BATS_TEST_DIRNAME}/tests_remote_repository"
export FAKE_TEMP_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/tests_clone_of_remote_repository"
export FAKE_FOLDER_WITHOUT_GIT_REPO="/tmp/tests_folder_without_git_repo"
# While it is likely the GitHub hosted runners will use master as the default branch,
# locally anyone may change that. So for tests lets grab whatever is currently set
@ -33,7 +34,10 @@ setup() {
export INPUT_TAGGING_MESSAGE=""
export INPUT_PUSH_OPTIONS=""
export INPUT_SKIP_DIRTY_CHECK=false
export INPUT_SKIP_FETCH=false
export INPUT_SKIP_CHECKOUT=false
export INPUT_DISABLE_GLOBBING=false
export INPUT_CREATE_BRANCH=false
export INPUT_INTERNAL_GIT_BINARY=git
# Set GitHub environment variables used by the GitHub Action
@ -55,6 +59,7 @@ teardown() {
rm -rf "${FAKE_LOCAL_REPOSITORY}"
rm -rf "${FAKE_REMOTE}"
rm -rf "${FAKE_TEMP_LOCAL_REPOSITORY}"
rm -rf "${INPUT_REPOSITORY}"
if [ -z ${GITHUB_OUTPUT+x} ]; then
echo "GITHUB_OUTPUT is not set"
@ -187,6 +192,7 @@ cat_github_output() {
assert_failure
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "INPUT_BRANCH value: ${FAKE_DEFAULT_BRANCH}"
assert_line "INPUT_FILE_PATTERN: ."
assert_line "INPUT_COMMIT_OPTIONS: "
assert_line "::debug::Apply commit options "
@ -403,6 +409,32 @@ cat_github_output() {
assert_output --partial refs/tags/v2.0.0
}
@test "If SKIP_FETCH is true git-fetch will not be called" {
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
INPUT_SKIP_FETCH=true
run git_auto_commit
assert_success
assert_line "::debug::git-fetch will not be executed."
}
@test "If SKIP_CHECKOUT is true git-checkout will not be called" {
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
INPUT_SKIP_CHECKOUT=true
run git_auto_commit
assert_success
assert_line "::debug::git-checkout will not be executed."
}
@test "It pushes generated commit and tag to remote and actually updates the commit shas" {
INPUT_BRANCH=""
INPUT_TAGGING_MESSAGE="v2.0.0"
@ -433,6 +465,10 @@ cat_github_output() {
}
@test "It pushes generated commit and tag to remote branch and updates commit sha" {
# Create "a-new-branch"-branch and then immediately switch back to ${FAKE_DEFAULT_BRANCH}
git checkout -b a-new-branch
git checkout ${FAKE_DEFAULT_BRANCH}
INPUT_BRANCH="a-new-branch"
INPUT_TAGGING_MESSAGE="v2.0.0"
@ -455,7 +491,7 @@ cat_github_output() {
assert_output --partial refs/tags/v2.0.0
# Assert that branch "a-new-branch" was updated on remote
current_sha="$(git rev-parse --verify --short ${FAKE_DEFAULT_BRANCH})"
current_sha="$(git rev-parse --verify --short a-new-branch)"
remote_sha="$(git rev-parse --verify --short origin/a-new-branch)"
assert_equal $current_sha $remote_sha
@ -499,6 +535,7 @@ cat_github_output() {
@test "it does not throw an error if not changes are detected and SKIP_DIRTY_CHECK is false" {
INPUT_FILE_PATTERN="."
INPUT_SKIP_DIRTY_CHECK=false
INPUT_SKIP_FETCH=false
run git_auto_commit
@ -541,8 +578,9 @@ cat_github_output() {
assert_line "changes_detected=true"
}
@test "It pushes commit to new branch that does not exist yet" {
@test "script fails to push commit to new branch that does not exist yet" {
INPUT_BRANCH="not-existend-branch"
INPUT_CREATE_BRANCH=false
run git branch
refute_line --partial "not-existend-branch"
@ -554,24 +592,25 @@ cat_github_output() {
run git_auto_commit
assert_success
assert_failure
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "INPUT_BRANCH value: not-existend-branch"
assert_line "fatal: invalid reference: not-existend-branch"
run git branch
assert_line --partial ${FAKE_DEFAULT_BRANCH}
refute_line --partial "not-existend-branch"
run git branch -r
assert_line --partial "origin/not-existend-branch"
refute_line --partial "origin/not-existend-branch"
run cat_github_output
assert_line "changes_detected=true"
}
@test "It does not create new local branch and pushes the commit to a new branch on remote" {
@test "It creates new local branch and pushes the new branch to remote" {
INPUT_BRANCH="not-existend-branch"
INPUT_CREATE_BRANCH=true
run git branch
refute_line --partial "not-existend-branch"
@ -596,12 +635,9 @@ cat_github_output() {
assert_line "::debug::Apply push options "
assert_line "::debug::Push commit to remote branch not-existend-branch"
# Assert that local repo is still on default branch and not on new branch.
run git branch
assert_line --partial ${FAKE_DEFAULT_BRANCH}
refute_line --partial "not-existend-branch"
assert_line --partial "not-existend-branch"
# Assert branch has been created on remote
run git branch -r
assert_line --partial "origin/not-existend-branch"
@ -610,11 +646,13 @@ cat_github_output() {
assert_line -e "commit_hash=[0-9a-f]{40}$"
}
@test "It does not create new local branch if local branch already exists" {
@test "it does not create new local branch if local branch already exists" {
git checkout -b not-existend-remote-branch
git checkout ${FAKE_DEFAULT_BRANCH}
INPUT_BRANCH="not-existend-remote-branch"
INPUT_CREATE_BRANCH=true
run git branch
assert_line --partial "not-existend-remote-branch"
@ -639,11 +677,6 @@ cat_github_output() {
assert_line "::debug::Apply push options "
assert_line "::debug::Push commit to remote branch not-existend-remote-branch"
# Assert checked out branch is still the same.
run git rev-parse --abbrev-ref HEAD
assert_line --partial ${FAKE_DEFAULT_BRANCH}
refute_line --partial "not-existend-remote-branch"
run git branch
assert_line --partial "not-existend-remote-branch"
@ -655,7 +688,8 @@ cat_github_output() {
assert_line -e "commit_hash=[0-9a-f]{40}$"
}
@test "It creates new local branch and pushes branch to remote even if the remote branch already exists" {
@test "it creates new local branch and pushes branch to remote even if the remote branch already exists" {
# Create `existing-remote-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY
git checkout -b "existing-remote-branch"
@ -672,6 +706,7 @@ cat_github_output() {
cd $FAKE_LOCAL_REPOSITORY
INPUT_BRANCH="existing-remote-branch"
INPUT_CREATE_BRANCH=true
run git branch
refute_line --partial "existing-remote-branch"
@ -699,14 +734,13 @@ cat_github_output() {
assert_line "::debug::Push commit to remote branch existing-remote-branch"
run git branch
assert_line --partial ${FAKE_DEFAULT_BRANCH}
refute_line --partial "existing-remote-branch"
assert_line --partial "existing-remote-branch"
run git branch -r
assert_line --partial "origin/existing-remote-branch"
# Assert that branch "existing-remote-branch" was updated on remote
current_sha="$(git rev-parse --verify --short ${FAKE_DEFAULT_BRANCH})"
current_sha="$(git rev-parse --verify --short existing-remote-branch)"
remote_sha="$(git rev-parse --verify --short origin/existing-remote-branch)"
assert_equal $current_sha $remote_sha
@ -716,7 +750,7 @@ cat_github_output() {
assert_line -e "commit_hash=[0-9a-f]{40}$"
}
@test "It fails if local branch is behind remote and when remote has newer commits" {
@test "script fails if new local branch is checked out and push fails as remote has newer commits than local" {
# Create `existing-remote-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY
git checkout -b "existing-remote-branch"
@ -733,6 +767,7 @@ cat_github_output() {
cd $FAKE_LOCAL_REPOSITORY
INPUT_BRANCH="existing-remote-branch"
INPUT_CREATE_BRANCH=true
run git branch
refute_line --partial "existing-remote-branch"
@ -747,24 +782,23 @@ cat_github_output() {
assert_failure
assert_line "hint: Updates were rejected because a pushed branch tip is behind its remote"
assert_line "hint: Updates were rejected because the tip of your current branch is behind"
# Assert that branch exists locally and on remote
run git branch
assert_line --partial ${FAKE_DEFAULT_BRANCH}
refute_line --partial "existing-remote-branch"
assert_line --partial "existing-remote-branch"
run git branch -r
assert_line --partial "origin/existing-remote-branch"
# Assert that branch "existing-remote-branch" was not updated on remote
current_sha="$(git rev-parse --verify --short ${FAKE_DEFAULT_BRANCH})"
current_sha="$(git rev-parse --verify --short existing-remote-branch)"
remote_sha="$(git rev-parse --verify --short origin/existing-remote-branch)"
refute [assert_equal $current_sha $remote_sha]
}
@test "It fails to push commit to remote if branch already exists and local repo is behind its remote counterpart" {
@test "It pushes commit to remote if branch already exists and local repo is behind its remote counterpart" {
# Create `new-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY
@ -784,7 +818,7 @@ cat_github_output() {
INPUT_BRANCH="new-branch"
# Assert that local remote does not have a "new-branch"-branch nor does
# Assert that local remote does not know have "new-branch" locally nor does
# know about the remote branch.
run git branch
refute_line --partial "new-branch"
@ -796,13 +830,16 @@ cat_github_output() {
run git_auto_commit
assert_failure
assert_success
assert_line "INPUT_BRANCH value: new-branch"
assert_line --partial "::debug::Push commit to remote branch new-branch"
assert_line --partial "Updates were rejected because the remote contains work that you do"
assert_line --partial "This is usually caused by another repository pushing"
# Assert that branch "new-branch" was updated on remote
current_sha="$(git rev-parse --verify --short new-branch)"
remote_sha="$(git rev-parse --verify --short origin/new-branch)"
assert_equal $current_sha $remote_sha
}
@test "throws fatal error if file pattern includes files that do not exist" {
@ -967,7 +1004,7 @@ cat_github_output() {
assert_line --partial "Working tree clean. Nothing to commit."
assert_line --partial "new-file-2.txt"
# assert_line --partial "new-file-3.txt"
assert_line --partial "new-file-3.txt"
# Changes are not detected
run cat_github_output
@ -1001,7 +1038,7 @@ cat_github_output() {
assert_line --partial "warning: in the working copy of 'new-file-2.txt', LF will be replaced by CRLF the next time Git touches it"
assert_line --partial "new-file-2.txt"
# assert_line --partial "new-file-3.txt"
assert_line --partial "new-file-3.txt"
# Changes are detected
run cat_github_output
@ -1077,3 +1114,14 @@ END
run git log -n 1
assert_output --partial $COMMIT_MESSAGE
}
@test "It exits with error message if entrypoint.sh is being run not in a git repository" {
INPUT_REPOSITORY="${FAKE_FOLDER_WITHOUT_GIT_REPO}"
mkdir "${INPUT_REPOSITORY}"
run git_auto_commit
assert_failure;
assert_line "::error::git-status failed with:<fatal: not a git repository (or any of the parent directories): .git>"
}

View File

@ -10,7 +10,7 @@ bats-support@ztombol/bats-support:
version "0.3.0"
resolved "https://codeload.github.com/ztombol/bats-support/tar.gz/004e707638eedd62e0481e8cdc9223ad471f12ee"
bats@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/bats/-/bats-1.10.0.tgz#d22cb6e2d88fd39302167da237d710406d1587ce"
integrity sha512-yOQrC7npuCrN+Ic3TyjTjJlzHa0qlK3oEO6VAYPWwFeutx/GmpljIyB6uNSl/UTASyc2w4FgVuA/QMMf9OdsCw==
bats@^1.11.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/bats/-/bats-1.11.0.tgz#40281f021f5befcc10da54ed5674aa5b181f4953"
integrity sha512-qiKdnS4ID3bJ1MaEOKuZe12R4w+t+psJF0ICj+UdkiHBBoObPMHv8xmD3w6F4a5qwUyZUHS+413lxENBNy8xcQ==