From cf3dee952f08b276d104f10d5e65b4d5a3331c23 Mon Sep 17 00:00:00 2001 From: Nils Brugger Date: Wed, 17 Nov 2021 11:56:50 +0100 Subject: [PATCH] feat: add `commit` and `push` inputs test: add test for `push` and `commit` inputs fix: test action commit failing fix: bash condtion fix: tag not found on test test: add echo for debugging to workflow fix: wrong test condition fix: missing log for new option fix: bash scripting syntax fix: commit input being inverted fix: inverted commit input fix: use the branch name instead of HEAD I have no idea why after checkout 'HEAD' sha is not the same as `origin/head_ref` but whatever fix(ci): test script not working on fork fix(ci): checking out non origin branch fix(ci): testing local state in push check Testing the push should only be done on the remote/origin --- .github/workflows/test_action.yml | 57 +++++++++++++++++++++++++++++++ README.md | 3 +- action.yml | 10 +++++- entrypoint.sh | 21 +++++++----- 4 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/test_action.yml diff --git a/.github/workflows/test_action.yml b/.github/workflows/test_action.yml new file mode 100644 index 0000000..b76dc16 --- /dev/null +++ b/.github/workflows/test_action.yml @@ -0,0 +1,57 @@ +on: + pull_request: + types: + - opened + - synchronize + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + fetch-depth: 0 # ensures that tags are fetched, seems to be needed + - name: Capture commit id + id: capture + run: | + COMMIT_ID=$(git rev-parse ${{ github.head_ref }}) + echo "The sha of the starting commit is $COMMIT_ID" + echo "::set-output name=commit::$COMMIT_ID" + - name: create test commit + run: | + touch test_file + git config --global user.email "you@example.com" + git config --global user.name "Your Name" + git add test_file + git commit -m "feat: test feature" + - name: test action + uses: ./ + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + commit: false + push: false + - uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + fetch-depth: 0 # ensures that tags are fetched, seems to be needed + path: new_head + - name: Test push + run: | + cd new_head + last_pushed_commit=$(git rev-parse ${{ github.head_ref }}) + echo "Commit sha on origin : $last_pushed_commit" + if [[ $last_pushed_commit != ${{steps.capture.outputs.commit}} ]]; then + echo "Something got pushed to ${{ github.head_ref }}" + exit 1 + fi + - name: Test commit + run: | + commit_message=$(git log -1 HEAD --pretty=format:%s) + echo "Latest commit: $commit_message" + if [[ $commit_message != "feat: test feature" ]]; then + echo "The latest commit message is not 'feat: test feature'" + exit 1 + fi diff --git a/README.md b/README.md index 9475ac8..fe04d91 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ jobs: | `changelog_increment_filename` | Filename to store the incremented generated changelog. This is different to changelog as it only contains the changes for the just generated version. Example: `body.md` | - | | `git_name` | Name used to configure git (for git operations) | `github-actions[bot]` | | `git_email` | Email address used to configure git (for git operations) | `github-actions[bot]@users.noreply.github.com` | - +| `push` | Define if the changes should be pushed to the branch. | true | +| `commit` | Define if the changes should be committed to the branch. | true | ## Outputs diff --git a/action.yml b/action.yml index 7d2ebfa..219a109 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,14 @@ inputs: dry_run: description: 'Run without creating commit, output to stdout' required: false + commit: + description: 'If true a commit is created containing the bump changes' + required: false + default: "true" + push: + description: 'If true the bump commit is pushed to the remote repository' + required: false + default: "true" prerelease: description: 'Set as prerelease version' required: false @@ -45,4 +53,4 @@ inputs: git_email: description: 'Email address used to configure git (for git operations)' required: false - default: 'github-actions[bot]@users.noreply.github.com' \ No newline at end of file + default: 'github-actions[bot]@users.noreply.github.com' diff --git a/entrypoint.sh b/entrypoint.sh index 4c04b28..dcfed11 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,6 +3,7 @@ if [ $INPUT_DRY_RUN ]; then INPUT_DRY_RUN='--dry-run'; else INPUT_DRY_RUN=''; fi if [ $INPUT_CHANGELOG ]; then INPUT_CHANGELOG='--changelog'; else INPUT_CHANGELOG=''; fi if [ $INPUT_PRERELEASE ]; then INPUT_PRERELEASE="--prerelease $INPUT_PRERELEASE"; else INPUT_PRERELEASE=''; fi +if [ "$INPUT_COMMIT" == 'false' ]; then INPUT_COMMIT='--files-only'; else INPUT_COMMIT=''; fi INPUT_BRANCH=${INPUT_BRANCH:-master} INPUT_EXTRA_REQUIREMENTS=${INPUT_EXTRA_REQUIREMENTS:-''} REPOSITORY=${INPUT_REPOSITORY:-$GITHUB_REPOSITORY} @@ -31,13 +32,13 @@ echo "Git name: $(git config --get user.name)" echo "Git email: $(git config --get user.email)" -echo "Running cz: $INPUT_DRY_RUN $INPUT_CHANGELOG $INPUT_PRERELEASE" +echo "Running cz: $INPUT_DRY_RUN $INPUT_COMMIT $INPUT_CHANGELOG $INPUT_PRERELEASE" if [ $INPUT_CHANGELOG_INCREMENT_FILENAME ]; then - cz bump --yes --changelog-to-stdout $INPUT_DRY_RUN $INPUT_CHANGELOG $INPUT_PRERELEASE > $INPUT_CHANGELOG_INCREMENT_FILENAME; + cz bump --yes --changelog-to-stdout $INPUT_COMMIT $INPUT_DRY_RUN $INPUT_CHANGELOG $INPUT_PRERELEASE > $INPUT_CHANGELOG_INCREMENT_FILENAME; else - cz bump --yes $INPUT_DRY_RUN $INPUT_CHANGELOG $INPUT_PRERELEASE; + cz bump --yes $INPUT_DRY_RUN $INPUT_COMMIT $INPUT_CHANGELOG $INPUT_PRERELEASE; fi export REV=`cz version --project` @@ -45,9 +46,13 @@ echo "REVISION=$REV" >> $GITHUB_ENV echo "::set-output name=version::$REV" -echo "Pushing to branch..." -remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${REPOSITORY}.git" -git pull ${remote_repo} ${INPUT_BRANCH} -git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags --tags; - +if [ "$INPUT_PUSH" == "true" ]; +then + echo "Pushing to branch..." + remote_repo="https://${GITHUB_ACTOR}:${INPUT_GITHUB_TOKEN}@github.com/${REPOSITORY}.git" + git pull ${remote_repo} ${INPUT_BRANCH} + git push "${remote_repo}" HEAD:${INPUT_BRANCH} --follow-tags --tags; +else + echo "Not pushing" +fi echo "Done."