Compare commits

...

6 Commits

Author SHA1 Message Date
Stefan Zweifel
5dd17c3b53 Release v4.11.0 2021-05-03 21:01:59 +02:00
Stefan Zweifel
ebbf8e517a Update README 2021-05-03 20:54:47 +02:00
Stefan Zweifel
76f1ae2f1a Add Credits to README 2021-05-03 20:23:02 +02:00
Stefan Zweifel
b9c536534c Add new *_options to README 2021-05-03 20:22:53 +02:00
Stefan Zweifel
68f0d95687 Merge pull request #156 from funkjedi/status-add-options
Add options for git add/status commands
2021-05-03 17:43:58 +02:00
Tim Robertson
ccad85927e Add options for add/status commands 2021-05-03 11:26:56 -04:00
5 changed files with 108 additions and 36 deletions

View File

@@ -4,11 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.10.0...HEAD)
## [Unreleased](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.11.0...HEAD)
> TBD
## [v4.11.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.10.0...v4.11.0) - 2021-05-03
### Added
- Add `status_options` and `add_options` option [#156](https://github.com/stefanzweifel/git-auto-commit-action/pull/156)
## [v4.10.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.9.2...v4.10.0) - 2021-04-12
### Added

View File

@@ -7,12 +7,9 @@
<img src="https://github.com/stefanzweifel/git-auto-commit-action/workflows/tests/badge.svg" alt="">
</a>
This GitHub Action automatically commits files which have been changed during a Workflow run and pushes the commit back to GitHub.
A GitHub Action to detect changed files during a Workflow run and to commit and push them back to the GitHub repository.
By default, the commit is made in the name of "GitHub Actions" and co-authored by the user that made the last commit.
This Action has been inspired and adapted from the [auto-commit](https://github.com/cds-snc/github-actions/tree/master/auto-commit
)-Action of the Canadian Digital Service and this [commit](https://github.com/elstudio/actions-js-build/blob/41d604d6e73d632e22eac40df8cc69b5added04b/commit/entrypoint.sh)-Action by Eric Johnson.
If you want to learn more how this Action works under the hood, check out [this article](https://michaelheap.com/git-auto-commit/) by Michael Heap.
## Usage
@@ -23,22 +20,21 @@ Add the following step at the end of your job, after other steps that might add
- uses: stefanzweifel/git-auto-commit-action@v4
```
This is a more extended example with all possible options.
The following is an extended example with all possible options available for this Action.
```yaml
- uses: stefanzweifel/git-auto-commit-action@v4
with:
# Optional but recommended
# Optional, but recommended
# Defaults to "Apply automatic changes"
commit_message: Apply automatic changes
commit_message: Automated Change
# Optional branch name where commit should be pushed to
# Defaults to the current branch
# Optional branch name where commit should be pushed to.
# Defaults to the current branch.
branch: feature-123
# Optional options appended to `git-commit`
# See https://git-scm.com/docs/git-commit for a list of available options
# Optional. Used by `git-commit`.
# See https://git-scm.com/docs/git-commit#_options
commit_options: '--no-verify --signoff'
# Optional glob pattern of files which should be added to the commit
@@ -61,21 +57,30 @@ This is a more extended example with all possible options.
# Action will create and push a new tag to the remote repository and the defined branch
tagging_message: 'v1.0.0'
# Optional options appended to `git-push`
# See git-push documentation for details: https://git-scm.com/docs/git-push#_options
# Optional. Used by `git-status`
# See https://git-scm.com/docs/git-status#_options
status_options: '--untracked-files=no'
# Optional. Used by `git-add`
# See https://git-scm.com/docs/git-add#_options
add_options: '-u'
# Optional. Used by `git-push`
# See https://git-scm.com/docs/git-push#_options
push_options: '--force'
# Optional: Disable dirty check and always try to create a commit and push
# Optional. Disable dirty check and always try to create a commit and push
skip_dirty_check: true
# Optional: Skip internal call to `git fetch`
# Optional. Skip internal call to `git fetch`
skip_fetch: true
# Optional: Prevents the shell from expanding filenames. Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
# Optional. Prevents the shell from expanding filenames.
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
disable_globbing: true
```
## Example
## Example Workflow
In this example, we're running `php-cs-fixer` in a PHP project to fix the codestyle automatically, then commit possible changed files back to the repository.
@@ -145,7 +150,7 @@ In non-`push` events, such as `pull_request`, make sure to specify the `ref` to
You have to do this to avoid that the `checkout`-Action clones your repository in a detached state.
### Commits of this Action do not trigger new Workflow runs
### Commits made by this Action do not trigger new Workflow runs
The resulting commit **will not trigger** another GitHub Actions Workflow run.
This is due to [limitations set by GitHub](https://help.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token).
@@ -213,7 +218,7 @@ Here's how the Pull Request will look like:
![Screenshot of a Pull Request from a Fork](https://user-images.githubusercontent.com/1080923/90955964-9c74c080-e482-11ea-8097-aa7f5161f50e.png)
As you can see, your contributors have to go through hoops to make this work. **For Workflows which runter linters and fixers (like the example above) we recommend running them when a push happens on the `master`-branch.**
As you can see, your contributors have to go through hoops to make this work. **For Workflows which run linters and fixers (like the example above) we recommend running them when a push happens on the `main`-branch.**
For more information about running Actions on forks, see [this announcement from GitHub](https://github.blog/2020-08-03-github-actions-improvements-for-fork-and-pull-request-workflows/).
@@ -226,7 +231,7 @@ See [this announcement from GitHub](https://github.blog/2020-08-03-github-action
### Signing Commits & Other Git Command Line Options
Using command lines options needs to be done manually for each workflow which you require the option enabled. So for example signing commits requires you to import the gpg signature each and every time. The following list of actions are worth checking out if you need to automate these tasks regulary
Using command lines options needs to be done manually for each workflow which you require the option enabled. So for example signing commits requires you to import the gpg signature each and every time. The following list of actions are worth checking out if you need to automate these tasks regulary.
- [Import GPG Signature](https://github.com/crazy-max/ghaction-import-gpg) (Suggested by [TGTGamer](https://github.com/tgtgamer))
@@ -245,18 +250,9 @@ Updating the `token` value with a Personal Access Token should fix your issues.
### Push to protected branches
If your repository uses [protected branches](https://help.github.com/en/github/administering-a-repository/configuring-protected-branches) you have to do the following changes to your Workflow for the Action to work properly.
If your repository uses [protected branches](https://help.github.com/en/github/administering-a-repository/configuring-protected-branches) you have to make some changes to your Workflow for the Action to work properly: You need a Personal Access Token and you either have to allow force pushes or the Personal Acess Token needs to belong to an Administrator.
You have to enable force pushes to a protected branch (See [documentation](https://help.github.com/en/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)) and update your Workflow to use force push like this.
```yaml
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply php-cs-fixer changes
push_options: --force
```
In addition, you have to create a new [Personal Access Token (PAT)](https://github.com/settings/tokens/new),
First, you have to create a new [Personal Access Token (PAT)](https://github.com/settings/tokens/new),
store the token as a secret in your repository and pass the new token to the [`actions/checkout`](https://github.com/actions/checkout#usage) Action step.
```yaml
@@ -266,7 +262,17 @@ store the token as a secret in your repository and pass the new token to the [`a
```
You can learn more about Personal Access Token in the [GitHub documentation](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
Note: If you're working in an organisation and you don't want to create the PAT from your personal account, we recommend using a bot-account for such tokens.
**Note:** If you're working in an organisation and you don't want to create the PAT from your personal account, we recommend using a bot-account for such tokens.
If you go the "force pushes" route, you have to enable force pushes to a protected branch (See [documentation](https://help.github.com/en/github/administering-a-repository/enabling-force-pushes-to-a-protected-branch)) and update your Workflow to use force push like this.
```yaml
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply php-cs-fixer changes
push_options: --force
```
### No new workflows are triggered by the commit of this action
@@ -295,6 +301,14 @@ We use [SemVer](http://semver.org/) for versioning. For the versions available,
We also provide major version tags to make it easier to always use the latest release of a major version. For example you can use `stefanzweifel/git-auto-commit-action@v4` to always use the latest release of the current major version.
(More information about this [here](https://help.github.com/en/actions/building-actions/about-actions#versioning-your-action).)
## Credits
* [Stefan Zweifel](https://github.com/stefanzweifel)
* [All Contributors](https://github.com/stefanzweifel/git-auto-commit-action/graphs/contributors)
This Action has been inspired and adapted from the [auto-commit](https://github.com/cds-snc/github-actions/tree/master/auto-commit
)-Action of the Canadian Digital Service and this [commit](https://github.com/elstudio/actions-js-build/blob/41d604d6e73d632e22eac40df8cc69b5added04b/commit/entrypoint.sh)-Action by Eric Johnson.
## License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/stefanzweifel/git-auto-commit-action/blob/master/LICENSE) file for details.

View File

@@ -16,6 +16,14 @@ inputs:
description: Commit options (eg. --no-verify)
required: false
default: ''
add_options:
description: Add options (eg. -u)
required: false
default: ''
status_options:
description: Status options (eg. --untracked-files=no)
required: false
default: ''
file_pattern:
description: File pattern used for `git add`. For example `src/\*.js`
required: false

View File

@@ -37,8 +37,11 @@ _switch_to_repository() {
}
_git_is_dirty() {
echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}";
echo "::debug::Apply status options ${INPUT_STATUS_OPTIONS}";
# shellcheck disable=SC2086
[ -n "$(git status -s -- $INPUT_FILE_PATTERN)" ]
[ -n "$(git status -s $INPUT_STATUS_OPTIONS -- $INPUT_FILE_PATTERN)" ]
}
_switch_to_branch() {
@@ -58,10 +61,13 @@ _switch_to_branch() {
}
_add_files() {
echo "INPUT_ADD_OPTIONS: ${INPUT_ADD_OPTIONS}";
echo "::debug::Apply add options ${INPUT_ADD_OPTIONS}";
echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";
# shellcheck disable=SC2086
git add ${INPUT_FILE_PATTERN};
git add ${INPUT_ADD_OPTIONS} ${INPUT_FILE_PATTERN};
}
_local_commit() {

View File

@@ -14,6 +14,8 @@ setup() {
export INPUT_COMMIT_MESSAGE="Commit Message"
export INPUT_BRANCH="master"
export INPUT_COMMIT_OPTIONS=""
export INPUT_ADD_OPTIONS=""
export INPUT_STATUS_OPTIONS=""
export INPUT_FILE_PATTERN="."
export INPUT_COMMIT_USER_NAME="Test Suite"
export INPUT_COMMIT_USER_EMAIL="test@github.com"
@@ -115,6 +117,20 @@ git_auto_commit() {
assert_line "::debug::Push commit to remote branch master"
}
@test "It applies INPUT_STATUS_OPTIONS when running dirty check" {
INPUT_STATUS_OPTIONS="--untracked-files=no"
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php
run git_auto_commit
assert_success
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::false"
assert_line "Working tree clean. Nothing to commit."
}
@test "It prints a 'Nothing to commit' message in a clean repository" {
run git_auto_commit
@@ -142,6 +158,28 @@ git_auto_commit() {
assert_line "::debug::Apply commit options "
}
@test "It applies INPUT_ADD_OPTIONS when adding files" {
INPUT_FILE_PATTERN=""
INPUT_STATUS_OPTIONS="--untracked-files=no"
INPUT_ADD_OPTIONS="-u"
date > "${FAKE_LOCAL_REPOSITORY}"/remote-files1.txt
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php
run git_auto_commit
assert_success
assert_line "INPUT_STATUS_OPTIONS: --untracked-files=no"
assert_line "INPUT_ADD_OPTIONS: -u"
assert_line "INPUT_FILE_PATTERN: "
assert_line "::debug::Push commit to remote branch master"
# Assert that PHP files have not been added.
run git status
assert_output --partial 'new-file-1.php'
}
@test "It applies INPUT_FILE_PATTERN when creating commit" {
INPUT_FILE_PATTERN="*.txt *.html"