Skip to content

Commit 4aa8e9f

Browse files
Merge pull request #21 from step-security/auto-cherry-pick
chore: Cherry-picked changes from upstream
2 parents 2c7b1de + d4db8fd commit 4aa8e9f

File tree

6 files changed

+153
-143
lines changed

6 files changed

+153
-143
lines changed

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ The following is an extended example with all available options.
5959
# Defaults to "Apply automatic changes"
6060
commit_message: Automated Change
6161
62-
# Optional. Local and remote branch name where commit is going to be pushed
63-
# to. Defaults to the current branch.
64-
# You might need to set `create_branch: true` if the branch does not exist.
62+
# Optional. Remote branch name where commit is going to be pushed to.
63+
# Defaults to the current branch.
6564
branch: feature-123
6665
6766
# Optional. Options used by `git-commit`.
@@ -102,20 +101,11 @@ The following is an extended example with all available options.
102101

103102
# Optional. Disable dirty check and always try to create a commit and push
104103
skip_dirty_check: true
105-
106-
# Optional. Skip internal call to `git fetch`
107-
skip_fetch: true
108-
109-
# Optional. Skip internal call to `git checkout`
110-
skip_checkout: true
111104

112105
# Optional. Prevents the shell from expanding filenames.
113106
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
114107
disable_globbing: true
115108

116-
# Optional. Create given branch name in local and remote repository.
117-
create_branch: true
118-
119109
# Optional. Creates a new tag and pushes it to remote without creating a commit.
120110
# Skips dirty check and changed files. Must be used with `tagging_message`.
121111
create_git_tag_only: false
@@ -416,7 +406,6 @@ The steps in your workflow might look like this:
416406
commit_message: ${{ steps.last-commit.outputs.message }}
417407
commit_options: '--amend --no-edit'
418408
push_options: '--force'
419-
skip_fetch: true
420409
```
421410
422411
@@ -452,10 +441,12 @@ If you create a fine-grained personal access token, apply the `Contents`-permiss
452441
```yaml
453442
- uses: actions/checkout@v4
454443
with:
455-
token: ${{ secrets.PAT }}
444+
# We pass the "PAT" secret to the checkout action; if no PAT secret is available to the workflow runner (eg. Dependabot) we fall back to the default "GITHUB_TOKEN".
445+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
456446
```
457447
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).
458448
449+
459450
> [!TIP]
460451
> 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.
461452

action.yml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Git Auto Commit
22
description: 'Automatically commits files which have been changed during the workflow run and push changes back to remote repository.'
33

4+
author: step-security
5+
46
inputs:
57
commit_message:
68
description: Commit message
@@ -54,27 +56,28 @@ inputs:
5456
description: Skip the check if the git repository is dirty and always try to create a commit.
5557
required: false
5658
default: false
57-
skip_fetch:
58-
description: Skip the call to git-fetch.
59-
required: false
60-
default: false
61-
skip_checkout:
62-
description: Skip the call to git-checkout.
63-
required: false
64-
default: false
6559
disable_globbing:
6660
description: Stop the shell from expanding filenames (https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html)
6761
default: false
68-
create_branch:
69-
description: Create new branch with the name of `branch`-input in local and remote repository, if it doesn't exist yet.
70-
default: false
7162
create_git_tag_only:
7263
description: Perform a clean git tag and push, without commiting anything
7364
required: false
7465
default: false
7566
internal_git_binary:
7667
description: Internal use only! Path to git binary used to check if git is available. (Don't change this!)
7768
default: git
69+
skip_fetch:
70+
description: "Deprecated: skip_fetch has been removed in v6. It does not have any effect anymore."
71+
required: false
72+
default: false
73+
skip_checkout:
74+
description: "Deprecated: skip_checkout has been removed in v6. It does not have any effect anymore."
75+
required: false
76+
default: false
77+
create_branch:
78+
description: "Deprecated: create_branch has been removed in v6. It does not have any effect anymore."
79+
default: false
80+
7881

7982
outputs:
8083
changes_detected:

dist/entrypoint.sh

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,26 @@ _log() {
2727
}
2828

2929
_main() {
30+
if "$INPUT_SKIP_FETCH"; then
31+
_log "warning" "git-auto-commit: skip_fetch has been removed in v6. It does not have any effect anymore.";
32+
fi
33+
34+
if "$INPUT_SKIP_CHECKOUT"; then
35+
_log "warning" "git-auto-commit: skip_checkout has been removed in v6. It does not have any effect anymore.";
36+
fi
37+
38+
if "$INPUT_CREATE_BRANCH"; then
39+
_log "warning" "git-auto-commit: create_branch has been removed in v6. It does not have any effect anymore.";
40+
fi
41+
3042
_check_if_git_is_available
3143

3244
_switch_to_repository
45+
46+
_check_if_is_git_repository
47+
48+
_check_if_repository_is_in_detached_state
49+
3350
if "$INPUT_CREATE_GIT_TAG_ONLY"; then
3451
_log "debug" "Create git tag only";
3552
_set_github_output "create_git_tag_only" "true"
@@ -39,8 +56,6 @@ _main() {
3956

4057
_set_github_output "changes_detected" "true"
4158

42-
_switch_to_branch
43-
4459
_add_files
4560

4661
# Check dirty state of repo again using git-diff.
@@ -90,36 +105,25 @@ _git_is_dirty() {
90105
gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)";
91106
# shellcheck disable=SC2086
92107
gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})";
93-
if [ $? -ne 0 ]; then
94-
_log "error" "git-status failed with:<$gitStatusMessage>";
95-
exit 1;
96-
fi
97108
[ -n "$gitStatus" ]
98109
}
99110
100-
_switch_to_branch() {
101-
echo "INPUT_BRANCH value: $INPUT_BRANCH";
102-
103-
# Fetch remote to make sure that repo can be switched to the right branch.
104-
if "$INPUT_SKIP_FETCH"; then
105-
_log "debug" "git-fetch will not be executed.";
111+
_check_if_is_git_repository() {
112+
if [ -d ".git" ]; then
113+
_log "debug" "Repository found.";
106114
else
107-
git fetch --depth=1;
115+
_log "error" "Not a git repository. Please make sure to run this action in a git repository. Adjust the `repository` input if necessary.";
116+
exit 1;
108117
fi
118+
}
109119
110-
# If `skip_checkout`-input is true, skip the entire checkout step.
111-
if "$INPUT_SKIP_CHECKOUT"; then
112-
_log "debug" "git-checkout will not be executed.";
120+
_check_if_repository_is_in_detached_state() {
121+
if [ -z "$(git symbolic-ref HEAD)" ]
122+
then
123+
_log "error" "Repository is in detached HEAD state. Please make sure you check out a branch. Adjust the `ref` input accordingly.";
124+
exit 1;
113125
else
114-
# Create new local branch if `create_branch`-input is true
115-
if "$INPUT_CREATE_BRANCH"; then
116-
# shellcheck disable=SC2086
117-
git checkout -B $INPUT_BRANCH --;
118-
else
119-
# Switch to branch from current Workflow run
120-
# shellcheck disable=SC2086
121-
git checkout $INPUT_BRANCH --;
122-
fi
126+
_log "debug" "Repository is on a branch.";
123127
fi
124128
}
125129
@@ -168,6 +172,8 @@ _tag_commit() {
168172
169173
_push_to_github() {
170174
175+
echo "INPUT_BRANCH value: $INPUT_BRANCH";
176+
171177
echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}";
172178
_log "debug" "Apply push options ${INPUT_PUSH_OPTIONS}";
173179

entrypoint.sh

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,26 @@ _log() {
2727
}
2828

2929
_main() {
30+
if "$INPUT_SKIP_FETCH"; then
31+
_log "warning" "git-auto-commit: skip_fetch has been removed in v6. It does not have any effect anymore.";
32+
fi
33+
34+
if "$INPUT_SKIP_CHECKOUT"; then
35+
_log "warning" "git-auto-commit: skip_checkout has been removed in v6. It does not have any effect anymore.";
36+
fi
37+
38+
if "$INPUT_CREATE_BRANCH"; then
39+
_log "warning" "git-auto-commit: create_branch has been removed in v6. It does not have any effect anymore.";
40+
fi
41+
3042
_check_if_git_is_available
3143

3244
_switch_to_repository
45+
46+
_check_if_is_git_repository
47+
48+
_check_if_repository_is_in_detached_state
49+
3350
if "$INPUT_CREATE_GIT_TAG_ONLY"; then
3451
_log "debug" "Create git tag only";
3552
_set_github_output "create_git_tag_only" "true"
@@ -39,8 +56,6 @@ _main() {
3956

4057
_set_github_output "changes_detected" "true"
4158

42-
_switch_to_branch
43-
4459
_add_files
4560

4661
# Check dirty state of repo again using git-diff.
@@ -90,36 +105,25 @@ _git_is_dirty() {
90105
gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)";
91106
# shellcheck disable=SC2086
92107
gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})";
93-
if [ $? -ne 0 ]; then
94-
_log "error" "git-status failed with:<$gitStatusMessage>";
95-
exit 1;
96-
fi
97108
[ -n "$gitStatus" ]
98109
}
99110
100-
_switch_to_branch() {
101-
echo "INPUT_BRANCH value: $INPUT_BRANCH";
102-
103-
# Fetch remote to make sure that repo can be switched to the right branch.
104-
if "$INPUT_SKIP_FETCH"; then
105-
_log "debug" "git-fetch will not be executed.";
111+
_check_if_is_git_repository() {
112+
if [ -d ".git" ]; then
113+
_log "debug" "Repository found.";
106114
else
107-
git fetch --depth=1;
115+
_log "error" "Not a git repository. Please make sure to run this action in a git repository. Adjust the `repository` input if necessary.";
116+
exit 1;
108117
fi
118+
}
109119
110-
# If `skip_checkout`-input is true, skip the entire checkout step.
111-
if "$INPUT_SKIP_CHECKOUT"; then
112-
_log "debug" "git-checkout will not be executed.";
120+
_check_if_repository_is_in_detached_state() {
121+
if [ -z "$(git symbolic-ref HEAD)" ]
122+
then
123+
_log "error" "Repository is in detached HEAD state. Please make sure you check out a branch. Adjust the `ref` input accordingly.";
124+
exit 1;
113125
else
114-
# Create new local branch if `create_branch`-input is true
115-
if "$INPUT_CREATE_BRANCH"; then
116-
# shellcheck disable=SC2086
117-
git checkout -B $INPUT_BRANCH --;
118-
else
119-
# Switch to branch from current Workflow run
120-
# shellcheck disable=SC2086
121-
git checkout $INPUT_BRANCH --;
122-
fi
126+
_log "debug" "Repository is on a branch.";
123127
fi
124128
}
125129
@@ -168,6 +172,8 @@ _tag_commit() {
168172
169173
_push_to_github() {
170174
175+
echo "INPUT_BRANCH value: $INPUT_BRANCH";
176+
171177
echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}";
172178
_log "debug" "Apply push options ${INPUT_PUSH_OPTIONS}";
173179

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)