mirror of
https://github.com/stefanzweifel/git-auto-commit-action.git
synced 2025-07-31 23:46:11 +08:00
Compare commits
113 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
268ec0c240 | ||
|
9abc4c41e5 | ||
|
1aee6e6b46 | ||
|
94941cff88 | ||
|
7507738fca | ||
|
53438d58ce | ||
|
8056699617 | ||
|
520c63c171 | ||
|
a6006229e0 | ||
|
40b55695d1 | ||
|
8f33b6cc31 | ||
|
c4ab3ecb7e | ||
|
350754fe3a | ||
|
75802d269e | ||
|
5b85132a3c | ||
|
292ae30da6 | ||
|
b53a2f8baf | ||
|
a9021d5359 | ||
|
fafea853e8 | ||
|
6576a06e26 | ||
|
b104287145 | ||
|
b8b8026fe4 | ||
|
fae0e424e9 | ||
|
a1604ef1d5 | ||
|
152b8c00c6 | ||
|
8edcd8b718 | ||
|
8f9132651a | ||
|
6c72ab7799 | ||
|
983b799941 | ||
|
bee2ad60b0 | ||
|
ac4585b8d5 | ||
|
4e7742676e | ||
|
2e61cc5f41 | ||
|
021e855056 | ||
|
7cec9acee6 | ||
|
bbd291750d | ||
|
114a5e6044 | ||
|
4a75a2e723 | ||
|
1e826be61c | ||
|
4bab898d25 | ||
|
45872c3c23 | ||
|
9b7c21583c | ||
|
4cc90c7784 | ||
|
367938329d | ||
|
25cac34d8d | ||
|
30cd17bff0 | ||
|
8f5b58c89c | ||
|
65928d2898 | ||
|
6b4ef1444c | ||
|
55291ea168 | ||
|
2b5c86365b | ||
|
ff28943460 | ||
|
e3610414be | ||
|
042c3265d5 | ||
|
90cedba8bc | ||
|
b833fe0e9d | ||
|
653ea719ea | ||
|
47c724214a | ||
|
1ba48c6d7f | ||
|
b9e4cea0ef | ||
|
cfdb7e609e | ||
|
849613a3ad | ||
|
0ee9fb7263 | ||
|
6456430b6b | ||
|
7f67b0b49b | ||
|
8700c3bc1c | ||
|
872d72f1ae | ||
|
f905f00706 | ||
|
9c1e5d1c6a | ||
|
18a94e4f78 | ||
|
44aeb24da7 | ||
|
5c9bfe7477 | ||
|
814219db13 | ||
|
a204abce16 | ||
|
fc18df1417 | ||
|
8f243393e7 | ||
|
fc5b9bf8cf | ||
|
a818c89e49 | ||
|
8bf1b5695a | ||
|
ea30556d53 | ||
|
cd7c7f1a8a | ||
|
4e16840be4 | ||
|
b0295737db | ||
|
0d6d4a4b22 | ||
|
091b400419 | ||
|
f7e362220d | ||
|
df97d6fbd8 | ||
|
4f0fc14d0c | ||
|
cdf7b74cbf | ||
|
862aa0e1e1 | ||
|
a9d1ac4f06 | ||
|
ebf8a401ee | ||
|
4b8393320b | ||
|
60c96d8c0d | ||
|
16d2bf88fd | ||
|
54d78531cc | ||
|
f922a52bf1 | ||
|
0bf17c328a | ||
|
240b9ad1fd | ||
|
57054d8e7e | ||
|
613ea337db | ||
|
686020fb2a | ||
|
90b474f549 | ||
|
8c26230a11 | ||
|
961d68003e | ||
|
cbc3d50cea | ||
|
9e4e2e76fa | ||
|
09aaf2d8fa | ||
|
c7ad0bdb15 | ||
|
9cb711878d | ||
|
b3fc6f8a17 | ||
|
d51384f592 | ||
|
4e7ddef1e0 |
19
.github/workflows/linter.yml
vendored
Normal file
19
.github/workflows/linter.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Lint Code Base
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Lint Code Base
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Lint Code Base
|
||||
uses: github/super-linter@v3
|
||||
env:
|
||||
VALIDATE_ALL_CODEBASE: false
|
||||
VALIDATE_MARKDOWN: false
|
||||
DEFAULT_BRANCH: master
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
12
.github/workflows/shellcheck.yml
vendored
12
.github/workflows/shellcheck.yml
vendored
@@ -1,12 +0,0 @@
|
||||
name: Shellcheck
|
||||
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
shellcheck:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
|
||||
- name: shellcheck
|
||||
uses: ludeeus/action-shellcheck@0.1.0
|
17
.github/workflows/tests.yml
vendored
Normal file
17
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
name: tests
|
||||
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Install testing dependencies
|
||||
run: yarn install
|
||||
|
||||
- name: Run Tests
|
||||
run: yarn test
|
||||
|
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
tests/test_fake_local_repository
|
||||
tests/test_fake_remote_repository
|
||||
tests/test_fake_temp_local_repository
|
||||
yarn.lock
|
51
CHANGELOG.md
51
CHANGELOG.md
@@ -4,11 +4,60 @@ 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.5.0...HEAD)
|
||||
## [Unreleased](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.9.0...HEAD)
|
||||
|
||||
> TBD
|
||||
|
||||
|
||||
## [v4.9.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.8.0...v4.9.0) - 2021-02-20
|
||||
|
||||
### Changed
|
||||
- Automatically create btanch if a branch with the given name does not exist [#140](https://github.com/stefanzweifel/git-auto-commit-action/pull/140)
|
||||
|
||||
|
||||
## [v4.8.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.7.2...v4.8.0) - 2020-12-15
|
||||
|
||||
### Added
|
||||
- Add `skip_fetch` option to skip the execution of `git-fetch` [#131](https://github.com/stefanzweifel/git-auto-commit-action/pull/131)
|
||||
|
||||
### Changed
|
||||
- Update `git fetch` to only fetch the latest commit from remote [#131](https://github.com/stefanzweifel/git-auto-commit-action/pull/131)
|
||||
|
||||
|
||||
## [v4.7.2](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.7.1...v4.7.2) - 2020-10-24
|
||||
|
||||
This release basically reverts the Action to the state of `v4.6.0`.
|
||||
|
||||
### Changed
|
||||
- Revert change to fix git-checkout with special characters in branch names [#122](https://github.com/stefanzweifel/git-auto-commit-action/pull/122)
|
||||
|
||||
|
||||
## [v4.7.1](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.7.0...v4.7.1) - 2020-10-22
|
||||
|
||||
### Removed
|
||||
- Remove `checkout_options` as it broke everything [#120](https://github.com/stefanzweifel/git-auto-commit-action/pull/120/)
|
||||
|
||||
|
||||
## [v4.7.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.6.0...v4.7.0) - 2020-10-21
|
||||
|
||||
### Added
|
||||
- Add `checkout_options` [#115](https://github.com/stefanzweifel/git-auto-commit-action/pull/115)
|
||||
|
||||
|
||||
## [v4.6.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.5.1...v4.6.0) - 2020-10-11
|
||||
|
||||
### Changed
|
||||
- Make "commit_message" optional and set default commit message to "Apply automatic changes" [#103](https://github.com/stefanzweifel/git-auto-commit-action/issues/103), [#110](https://github.com/stefanzweifel/git-auto-commit-action/pull/110)
|
||||
|
||||
### Fixes
|
||||
- Better support for branch names with special characters in them [#108](https://github.com/stefanzweifel/git-auto-commit-action/pull/108)
|
||||
|
||||
|
||||
## [v4.5.1](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.5.0...v4.5.1) - 2020-09-11
|
||||
|
||||
### Removed
|
||||
- Remove orphan branch feature added in #95 which broke stuff [#98](https://github.com/stefanzweifel/git-auto-commit-action/pull/98)
|
||||
|
||||
## [v4.5.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.4.1...v4.5.0) - 2020-09-10
|
||||
|
||||
### Added
|
||||
|
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Stefan Zweifel
|
||||
Copyright (c) 2021 Stefan Zweifel
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
80
README.md
80
README.md
@@ -2,23 +2,39 @@
|
||||
|
||||
> The GitHub Action for committing files for the 80% use case.
|
||||
|
||||
<a href="https://github.com/search?o=desc&q=stefanzweifel%2Fgit-auto-commit-action+path%3A.github%2Fworkflows+language%3AYAML&s=&type=Code" target="_blank" title="Public workflows that use this action."><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fapi-git-master.endbug.vercel.app%2Fapi%2Fgithub-actions%2Fused-by%3Faction%3Dstefanzweifel%2Fgit-auto-commit-action%26badge%3Dtrue" alt="Public workflows that use this action."></a>
|
||||
<a href="https://github.com/stefanzweifel/git-auto-commit-action/actions?query=workflow%3Atests">
|
||||
<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.
|
||||
The default committer is "GitHub Actions <actions@github.com>", and the default author of the commit is "Your GitHub Username <github_username@users.noreply.github.com>".
|
||||
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
|
||||
|
||||
Add the following step at the end of your job, after other steps that might add or change files.
|
||||
|
||||
```yaml
|
||||
- uses: stefanzweifel/git-auto-commit-action@v4
|
||||
```
|
||||
|
||||
|
||||
This is a more extended example with all possible options.
|
||||
|
||||
```yaml
|
||||
- uses: stefanzweifel/git-auto-commit-action@v4
|
||||
with:
|
||||
# Required
|
||||
# Optional but recommended
|
||||
# Defaults to "Apply automatic changes"
|
||||
commit_message: Apply automatic changes
|
||||
|
||||
# Optional branch to push 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`
|
||||
@@ -26,18 +42,20 @@ Add the following step at the end of your job, after other steps that might add
|
||||
commit_options: '--no-verify --signoff'
|
||||
|
||||
# Optional glob pattern of files which should be added to the commit
|
||||
# Defaults to all (.)
|
||||
# See the `pathspec`-documentation for git
|
||||
# - https://git-scm.com/docs/git-add#Documentation/git-add.txt-ltpathspecgt82308203
|
||||
# - https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
|
||||
file_pattern: src/*.js tests/*.js *.php
|
||||
|
||||
# Optional local file path to the repository
|
||||
# Defaults to the root of the repository
|
||||
repository: .
|
||||
|
||||
# Optional commit user and author settings
|
||||
commit_user_name: My GitHub Actions Bot
|
||||
commit_user_email: my-github-actions-bot@example.org
|
||||
commit_author: Author <actions@github.com>
|
||||
commit_user_name: My GitHub Actions Bot # defaults to "GitHub Actions"
|
||||
commit_user_email: my-github-actions-bot@example.org # defaults to "actions@github.com"
|
||||
commit_author: Author <actions@github.com> # defaults to author of the commit that triggered the run
|
||||
|
||||
# Optional tag message
|
||||
# Action will create and push a new tag to the remote repository and the defined branch
|
||||
@@ -48,7 +66,10 @@ Add the following step at the end of your job, after other steps that might add
|
||||
push_options: '--force'
|
||||
|
||||
# Optional: Disable dirty check and always try to create a commit and push
|
||||
skip_dirty_check: true
|
||||
skip_dirty_check: true
|
||||
|
||||
# Optional: Skip internal call to `git fetch`
|
||||
skip_fetch: true
|
||||
```
|
||||
|
||||
## Example
|
||||
@@ -65,7 +86,7 @@ on:
|
||||
pull_request:
|
||||
push:
|
||||
branches:
|
||||
- "master"
|
||||
- "main"
|
||||
|
||||
jobs:
|
||||
php-cs-fixer:
|
||||
@@ -94,6 +115,18 @@ 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.
|
||||
|
||||
### Example
|
||||
|
||||
```yaml
|
||||
- name: "Run if changes have been detected"
|
||||
if: steps.auto-commit-action.outputs.changes_detected == 'true'
|
||||
run: echo "Changes!"
|
||||
|
||||
- name: "Run if no changes have been detected"
|
||||
if: steps.auto-commit-action.outputs.changes_detected == 'false'
|
||||
run: echo "No Changes!"
|
||||
```
|
||||
|
||||
## Limitations & Gotchas
|
||||
|
||||
### Checkout the correct branch
|
||||
@@ -107,12 +140,12 @@ In non-`push` events, such as `pull_request`, make sure to specify the `ref` to
|
||||
ref: ${{ github.head_ref }}
|
||||
```
|
||||
|
||||
You have to do this do avoid that the `checkout`-Action clones your repository in a detached state.
|
||||
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
|
||||
|
||||
The resulting commit **will not trigger** another GitHub Actions Workflow run.
|
||||
This is due to [limititations set by GitHub](https://help.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token).
|
||||
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).
|
||||
|
||||
> When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs.
|
||||
|
||||
@@ -130,6 +163,11 @@ If you work in an organization and don't want to create a PAT from your personal
|
||||
|
||||
### Using the Action in forks from public repositories
|
||||
|
||||
**☝️ Important Notice**: This Action technically works with forks. However, please note that the combination of triggers and their options can cause issues. Please read [the documentation](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows) on which triggers GitHub Actions support.\
|
||||
If you use this Action in combination with a linter/fixer, it's easier if you run the Action on `push` on your `main`-branch.
|
||||
|
||||
---
|
||||
|
||||
By default, this Action will not run on Pull Requests which have been opened by forks. (This is a limitation by GitHub, not by us.)
|
||||
|
||||
If you want that a Workflow using this Action runs on Pull Requests opened by forks, 2 things have to be changed:
|
||||
@@ -149,15 +187,13 @@ The following workflow runs `php-cs-fixer` (a code linter and fixer for PHP) whe
|
||||
```yaml
|
||||
name: Format PHP
|
||||
|
||||
on: [pull_request, pull_request_target]
|
||||
on: [push, pull_request, pull_request_target]
|
||||
|
||||
jobs:
|
||||
php-cs-fixer:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
|
||||
- name: Run php-cs-fixer
|
||||
uses: docker://oskarstark/php-cs-fixer-ga
|
||||
@@ -174,7 +210,7 @@ Here's how the Pull Request will look like:
|
||||

|
||||
|
||||
|
||||
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 runter linters and fixers (like the example above) we recommend running them when a push happens on the `master`-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/).
|
||||
@@ -233,6 +269,22 @@ Note: If you're working in an organisation and you don't want to create the PAT
|
||||
|
||||
This is due to limitations set up by GitHub, [commits of this Action do not trigger new Workflow runs](#commits-of-this-action-do-not-trigger-new-workflow-runs).
|
||||
|
||||
## Running the tests
|
||||
|
||||
The package has tests written in [bats](https://github.com/bats-core/bats-core). Before you can run the test suite locally, you have to install the dependencies with `npm` or `yarn`.
|
||||
|
||||
```shell
|
||||
npm install
|
||||
yarn
|
||||
```
|
||||
|
||||
You can run the test suite with `npm` or `yarn`.
|
||||
|
||||
```shell
|
||||
npm run test
|
||||
yarn test
|
||||
```
|
||||
|
||||
## Versioning
|
||||
|
||||
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/stefanzweifel/git-auto-commit-action/tags).
|
||||
|
@@ -6,7 +6,8 @@ author: Stefan Zweifel <hello@stefanzweifel.io>
|
||||
inputs:
|
||||
commit_message:
|
||||
description: Commit message
|
||||
required: true
|
||||
required: false
|
||||
default: Apply automatic changes
|
||||
branch:
|
||||
description: Git branch name, where changes should be pushed too. Required if Action is used on the `pull_request` event
|
||||
required: false
|
||||
@@ -47,6 +48,10 @@ 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
|
||||
|
||||
outputs:
|
||||
changes_detected:
|
||||
|
@@ -1,7 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
set -x
|
||||
|
||||
_main() {
|
||||
_switch_to_repository
|
||||
@@ -30,27 +29,53 @@ _main() {
|
||||
|
||||
_switch_to_repository() {
|
||||
echo "INPUT_REPOSITORY value: $INPUT_REPOSITORY";
|
||||
cd $INPUT_REPOSITORY;
|
||||
cd "$INPUT_REPOSITORY";
|
||||
}
|
||||
|
||||
_git_is_dirty() {
|
||||
# shellcheck disable=SC2086
|
||||
[ -n "$(git status -s -- $INPUT_FILE_PATTERN)" ]
|
||||
}
|
||||
|
||||
_switch_to_branch() {
|
||||
echo "INPUT_BRANCH value: $INPUT_BRANCH";
|
||||
|
||||
if git show-ref --verify --quiet refs/heads/$INPUT_BRANCH; then
|
||||
# Switch to branch from current Workflow run
|
||||
git checkout $INPUT_BRANCH;
|
||||
else
|
||||
git checkout --orphan $INPUT_BRANCH
|
||||
git rm -rf .
|
||||
# Fetch remote to make sure that repo can be switched to the right branch.
|
||||
|
||||
if "$INPUT_SKIP_FETCH"; then
|
||||
echo "::debug::git-fetch has not been executed";
|
||||
else
|
||||
git fetch --depth=1;
|
||||
fi
|
||||
|
||||
|
||||
# If INPUT_BRANCH is empty, just run `git checkout`
|
||||
if [ -z "$INPUT_BRANCH" ]
|
||||
then
|
||||
# shellcheck disable=SC2086
|
||||
git checkout $INPUT_BRANCH;
|
||||
else
|
||||
# If the branch which we should checkout already exists, just
|
||||
# run `git checkout $INPUT_BRANCH`
|
||||
# Otherwhise create a new branch by adding the `-b` option to
|
||||
# `git-checkout`
|
||||
# shellcheck disable=SC2086
|
||||
if [ -n "$(git branch --list $INPUT_BRANCH)" ]
|
||||
then
|
||||
# shellcheck disable=SC2086
|
||||
git checkout $INPUT_BRANCH;
|
||||
else
|
||||
# shellcheck disable=SC2086
|
||||
git checkout -b $INPUT_BRANCH;
|
||||
fi
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
_add_files() {
|
||||
echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
git add ${INPUT_FILE_PATTERN};
|
||||
}
|
||||
|
||||
@@ -58,8 +83,14 @@ _local_commit() {
|
||||
echo "INPUT_COMMIT_OPTIONS: ${INPUT_COMMIT_OPTIONS}";
|
||||
echo "::debug::Apply commit options ${INPUT_COMMIT_OPTIONS}";
|
||||
|
||||
# shellcheck disable=SC2206
|
||||
INPUT_COMMIT_OPTIONS_ARRAY=( $INPUT_COMMIT_OPTIONS );
|
||||
|
||||
echo "INPUT_COMMIT_USER_NAME: ${INPUT_COMMIT_USER_NAME}";
|
||||
echo "INPUT_COMMIT_USER_EMAIL: ${INPUT_COMMIT_USER_EMAIL}";
|
||||
echo "INPUT_COMMIT_MESSAGE: ${INPUT_COMMIT_MESSAGE}";
|
||||
echo "INPUT_COMMIT_AUTHOR: ${INPUT_COMMIT_AUTHOR}";
|
||||
|
||||
git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" \
|
||||
commit -m "$INPUT_COMMIT_MESSAGE" \
|
||||
--author="$INPUT_COMMIT_AUTHOR" \
|
||||
@@ -74,7 +105,7 @@ _tag_commit() {
|
||||
echo "::debug::Create tag $INPUT_TAGGING_MESSAGE";
|
||||
git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE";
|
||||
else
|
||||
echo " No tagging message supplied. No tag will be added.";
|
||||
echo "No tagging message supplied. No tag will be added.";
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -83,6 +114,7 @@ _push_to_github() {
|
||||
echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}";
|
||||
echo "::debug::Apply push options ${INPUT_PUSH_OPTIONS}";
|
||||
|
||||
# shellcheck disable=SC2206
|
||||
INPUT_PUSH_OPTIONS_ARRAY=( $INPUT_PUSH_OPTIONS );
|
||||
|
||||
if [ -z "$INPUT_BRANCH" ]
|
||||
|
10
package.json
Normal file
10
package.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"bats": "^1.1.0",
|
||||
"bats-assert": "ztombol/bats-assert",
|
||||
"bats-support": "ztombol/bats-support"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bats tests"
|
||||
}
|
||||
}
|
355
tests/git-auto-commit.bats
Normal file
355
tests/git-auto-commit.bats
Normal file
@@ -0,0 +1,355 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load '../node_modules/bats-support/load'
|
||||
load '../node_modules/bats-assert/load'
|
||||
|
||||
setup() {
|
||||
# Define Paths for local repository used during tests
|
||||
export FAKE_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/test_fake_local_repository"
|
||||
export FAKE_REMOTE="${BATS_TEST_DIRNAME}/test_fake_remote_repository"
|
||||
export FAKE_TEMP_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/test_fake_temp_local_repository"
|
||||
|
||||
# Set default INPUT variables used by the GitHub Action
|
||||
export INPUT_REPOSITORY="${FAKE_LOCAL_REPOSITORY}"
|
||||
export INPUT_COMMIT_MESSAGE="Commit Message"
|
||||
export INPUT_BRANCH="master"
|
||||
export INPUT_COMMIT_OPTIONS=""
|
||||
export INPUT_FILE_PATTERN="."
|
||||
export INPUT_COMMIT_USER_NAME="Test Suite"
|
||||
export INPUT_COMMIT_USER_EMAIL="test@github.com"
|
||||
export INPUT_COMMIT_AUTHOR="Test Suite <test@users.noreply.github.com>"
|
||||
export INPUT_TAGGING_MESSAGE=""
|
||||
export INPUT_PUSH_OPTIONS=""
|
||||
export INPUT_SKIP_DIRTY_CHECK=false
|
||||
export INPUT_SKIP_FETCH=false
|
||||
|
||||
# Configure Git
|
||||
if [[ -z $(git config user.name) ]]; then
|
||||
git config --global user.name "Test Suite"
|
||||
git config --global user.email "test@github.com"
|
||||
fi
|
||||
|
||||
# Create and setup some fake repositories for testing
|
||||
_setup_fake_remote_repository
|
||||
_setup_local_repository
|
||||
}
|
||||
|
||||
teardown() {
|
||||
rm -rf "${FAKE_LOCAL_REPOSITORY}"
|
||||
rm -rf "${FAKE_REMOTE}"
|
||||
rm -rf "${FAKE_TEMP_LOCAL_REPOSITORY}"
|
||||
}
|
||||
|
||||
# Create a fake remote repository which tests can push against
|
||||
_setup_fake_remote_repository() {
|
||||
# Create the bare repository, which will act as our remote/origin
|
||||
rm -rf "${FAKE_REMOTE}";
|
||||
mkdir "${FAKE_REMOTE}";
|
||||
cd "${FAKE_REMOTE}";
|
||||
git init --bare;
|
||||
|
||||
# Clone the remote repository to a temporary location.
|
||||
rm -rf "${FAKE_TEMP_LOCAL_REPOSITORY}"
|
||||
git clone "${FAKE_REMOTE}" "${FAKE_TEMP_LOCAL_REPOSITORY}"
|
||||
|
||||
# Create some files, commit them and push them to the remote repository
|
||||
touch "${FAKE_TEMP_LOCAL_REPOSITORY}"/remote-files{1,2,3}.txt
|
||||
cd "${FAKE_TEMP_LOCAL_REPOSITORY}";
|
||||
git add .;
|
||||
git commit --quiet -m "Init Remote Repository";
|
||||
git push origin master;
|
||||
}
|
||||
|
||||
# Clone our fake remote repository and set it up for testing
|
||||
_setup_local_repository() {
|
||||
# Clone remote repository. In this repository we will do our testing
|
||||
rm -rf "${FAKE_LOCAL_REPOSITORY}"
|
||||
git clone "${FAKE_REMOTE}" "${FAKE_LOCAL_REPOSITORY}"
|
||||
|
||||
cd "${FAKE_LOCAL_REPOSITORY}";
|
||||
}
|
||||
|
||||
# Run the main code related to this GitHub Action
|
||||
git_auto_commit() {
|
||||
bash "${BATS_TEST_DIRNAME}"/../entrypoint.sh
|
||||
}
|
||||
|
||||
@test "It detects changes, commits them and pushes them to the remote repository" {
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
|
||||
assert_line "::set-output name=changes_detected::true"
|
||||
assert_line "INPUT_BRANCH value: master"
|
||||
assert_line "INPUT_FILE_PATTERN: ."
|
||||
assert_line "INPUT_COMMIT_OPTIONS: "
|
||||
assert_line "::debug::Apply commit options "
|
||||
assert_line "INPUT_TAGGING_MESSAGE: "
|
||||
assert_line "No tagging message supplied. No tag will be added."
|
||||
assert_line "INPUT_PUSH_OPTIONS: "
|
||||
assert_line "::debug::Apply push options "
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
}
|
||||
|
||||
@test "It detects when files have been deleted, commits changes and pushes them to the remote repository" {
|
||||
rm -rf "${FAKE_LOCAL_REPOSITORY}"/remote-files1.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
|
||||
assert_line "::set-output name=changes_detected::true"
|
||||
assert_line "INPUT_BRANCH value: master"
|
||||
assert_line "INPUT_FILE_PATTERN: ."
|
||||
assert_line "INPUT_COMMIT_OPTIONS: "
|
||||
assert_line "::debug::Apply commit options "
|
||||
assert_line "INPUT_TAGGING_MESSAGE: "
|
||||
assert_line "No tagging message supplied. No tag will be added."
|
||||
assert_line "INPUT_PUSH_OPTIONS: "
|
||||
assert_line "::debug::Apply push options "
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
}
|
||||
|
||||
@test "It prints a 'Nothing to commit' message in a clean repository" {
|
||||
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 "If SKIP_DIRTY_CHECK is set to true on a clean repo it fails to push" {
|
||||
INPUT_SKIP_DIRTY_CHECK=true
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_failure
|
||||
|
||||
assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
|
||||
assert_line "::set-output name=changes_detected::true"
|
||||
|
||||
assert_line "::set-output name=changes_detected::true"
|
||||
assert_line "INPUT_BRANCH value: master"
|
||||
assert_line "INPUT_FILE_PATTERN: ."
|
||||
assert_line "INPUT_COMMIT_OPTIONS: "
|
||||
assert_line "::debug::Apply commit options "
|
||||
}
|
||||
|
||||
@test "It applies INPUT_FILE_PATTERN when creating commit" {
|
||||
INPUT_FILE_PATTERN="*.txt *.html"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.html
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_FILE_PATTERN: *.txt *.html"
|
||||
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_COMMIT_OPTIONS when creating commit" {
|
||||
INPUT_COMMIT_OPTIONS="--no-verify --signoff"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_COMMIT_OPTIONS: --no-verify --signoff"
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
|
||||
# Assert last commit was signed off
|
||||
run git log -n 1
|
||||
assert_output --partial "Signed-off-by:"
|
||||
}
|
||||
|
||||
@test "It applies commit user and author settings" {
|
||||
INPUT_COMMIT_USER_NAME="A Single Test"
|
||||
INPUT_COMMIT_USER_EMAIL="single-test@github.com"
|
||||
INPUT_COMMIT_AUTHOR="A Single Test <single@users.noreply.github.com>"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_COMMIT_USER_NAME: A Single Test";
|
||||
assert_line "INPUT_COMMIT_USER_EMAIL: single-test@github.com";
|
||||
assert_line "INPUT_COMMIT_AUTHOR: A Single Test <single@users.noreply.github.com>";
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
|
||||
# Asser last commit was made by the defined user/author
|
||||
run git log -1 --pretty=format:'%ae'
|
||||
assert_output --partial "single@users.noreply.github.com"
|
||||
|
||||
run git log -1 --pretty=format:'%an'
|
||||
assert_output --partial "A Single Test"
|
||||
|
||||
run git log -1 --pretty=format:'%cn'
|
||||
assert_output --partial "A Single Test"
|
||||
|
||||
run git log -1 --pretty=format:'%ce'
|
||||
assert_output --partial "single-test@github.com"
|
||||
}
|
||||
|
||||
@test "It creates a tag with the commit" {
|
||||
INPUT_TAGGING_MESSAGE="v1.0.0"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_TAGGING_MESSAGE: v1.0.0"
|
||||
assert_line "::debug::Create tag v1.0.0"
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
|
||||
# Assert a tag v1.0.0 has been created
|
||||
run git tag
|
||||
assert_output v1.0.0
|
||||
|
||||
run git ls-remote --tags --refs
|
||||
assert_output --partial refs/tags/v1.0.0
|
||||
}
|
||||
|
||||
@test "It applies INPUT_PUSH_OPTIONS when pushing commit to remote" {
|
||||
|
||||
touch "${FAKE_TEMP_LOCAL_REPOSITORY}"/newer-remote-files{1,2,3}.txt
|
||||
cd "${FAKE_TEMP_LOCAL_REPOSITORY}";
|
||||
git add .;
|
||||
git commit --quiet -m "Add more remote files";
|
||||
git push origin master;
|
||||
|
||||
|
||||
INPUT_PUSH_OPTIONS="--force"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_PUSH_OPTIONS: --force"
|
||||
assert_line "::debug::Apply push options --force"
|
||||
assert_line "::debug::Push commit to remote branch master"
|
||||
|
||||
# Assert that the commit has been pushed with --force and
|
||||
# sha values are equal on local and remote
|
||||
current_sha="$(git rev-parse --verify --short master)"
|
||||
remote_sha="$(git rev-parse --verify --short origin/master)"
|
||||
|
||||
assert_equal $current_sha $remote_sha
|
||||
}
|
||||
|
||||
@test "It can checkout a different branch" {
|
||||
# Create foo-branch and then immediately switch back to master
|
||||
git checkout -b foo
|
||||
git checkout master
|
||||
|
||||
INPUT_BRANCH="foo"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_BRANCH value: foo"
|
||||
assert_line "::debug::Push commit to remote branch foo"
|
||||
|
||||
# Assert a new branch "foo" exists on remote
|
||||
run git ls-remote --heads
|
||||
assert_output --partial refs/heads/foo
|
||||
}
|
||||
|
||||
@test "It uses existing branch name when pushing when INPUT_BRANCH is empty" {
|
||||
INPUT_BRANCH=""
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_BRANCH value: "
|
||||
assert_line --partial "::debug::git push origin"
|
||||
|
||||
# Assert that branch "master" was updated on remote
|
||||
current_sha="$(git rev-parse --verify --short master)"
|
||||
remote_sha="$(git rev-parse --verify --short origin/master)"
|
||||
|
||||
assert_equal $current_sha $remote_sha
|
||||
}
|
||||
|
||||
@test "It uses existing branch when INPUT_BRANCH is empty and INPUT_TAGGING_MESSAGE is set" {
|
||||
INPUT_BRANCH=""
|
||||
INPUT_TAGGING_MESSAGE="v2.0.0"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
|
||||
assert_line "::debug::Create tag v2.0.0"
|
||||
assert_line "::debug::git push origin --tags"
|
||||
|
||||
# Assert a tag v2.0.0 has been created
|
||||
run git tag
|
||||
assert_output v2.0.0
|
||||
|
||||
# Assert tag v2.0.0 has been pushed to remote
|
||||
run git ls-remote --tags --refs
|
||||
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 has not been executed"
|
||||
}
|
||||
|
||||
@test "If INPUT_BRANCH is set and the branch does not exist it creates one" {
|
||||
INPUT_BRANCH="new-branch"
|
||||
|
||||
run git branch
|
||||
refute_line "new-branch"
|
||||
|
||||
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
|
||||
|
||||
run git_auto_commit
|
||||
|
||||
assert_success
|
||||
|
||||
assert_line "INPUT_BRANCH value: new-branch"
|
||||
assert_line --partial "::debug::Push commit to remote branch new-branch"
|
||||
|
||||
# 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
|
||||
}
|
16
yarn.lock
Normal file
16
yarn.lock
Normal file
@@ -0,0 +1,16 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
bats-assert@ztombol/bats-assert:
|
||||
version "0.3.0"
|
||||
resolved "https://codeload.github.com/ztombol/bats-assert/tar.gz/9f88b4207da750093baabc4e3f41bf68f0dd3630"
|
||||
|
||||
bats-support@ztombol/bats-support:
|
||||
version "0.3.0"
|
||||
resolved "https://codeload.github.com/ztombol/bats-support/tar.gz/004e707638eedd62e0481e8cdc9223ad471f12ee"
|
||||
|
||||
bats@^1.1.0:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/bats/-/bats-1.2.1.tgz#c15aecc3eb331aedf28678880fab2be0b73eba43"
|
||||
integrity sha512-2fcPDRQa/Kvh6j1IcCqsHpT5b9ObMzRzw6abC7Bg298PX8Qdh9VRkvO2WJUEhdyfjq2rLBCOAWdcv0tS4+xTUA==
|
Reference in New Issue
Block a user