Skip to content

test: add unit tests for git and pr packages, add unit-tests CI job #272

test: add unit tests for git and pr packages, add unit-tests CI job

test: add unit tests for git and pr packages, add unit-tests CI job #272

Workflow file for this run

name: Continuous Integration
on:
pull_request:
push:
branches:
- main
paths-ignore:
- '.github/workflows/**'
- '**/*.md'
- 'test/**'
- 'backup/**/*'
workflow_dispatch:
permissions:
contents: write
jobs:
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup Go
uses: actions/setup-go@v6
with:
go-version: '1.23'
- name: Run Go unit tests with coverage
run: go test ./internal/... -v -cover -coverprofile=coverage.out
- name: Display coverage summary
run: go tool cover -func=coverage.out
build-and-push-docker:
name: Build and Push Docker
needs: unit-tests
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5001:5000
env:
TEST_TAG: localhost:5001/actions/go-git-commit-action:latest
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
token: ${{ secrets.PAT_TOKEN }}
- name: Configure Git Safe Directory
run: git config --global --add safe.directory ${{ github.workspace }}
- name: Setup Docker BuildX
uses: docker/setup-buildx-action@v4
with:
install: true
driver-opts: network=host
- name: Build the Container
uses: docker/build-push-action@v7
with:
context: .
push: true
tags: ${{ env.TEST_TAG }}
- name: Create Test File
run: |
echo "$(date +%s)-$RANDOM" > test/test1.txt
- name: Run the Container
env:
USER_EMAIL: actions@github.com
USER_NAME: GitHub Actions
COMMIT_MESSAGE: test1 ${{ github.run_id }}!
BRANCH: test
REPOSITORY_PATH: test
FILE_PATTERN: .
TAG_NAME: test1-${{ github.run_id }}
TAG_MESSAGE: test1 ${{ github.run_id }}
TAG_REFERENCE: v1.0.1
run: |
docker run \
--env INPUT_USER_EMAIL="${{ env.USER_EMAIL }}" \
--env INPUT_USER_NAME="${{ env.USER_NAME }}" \
--env INPUT_COMMIT_MESSAGE="${{ env.COMMIT_MESSAGE }}" \
--env INPUT_BRANCH="${{ env.BRANCH }}" \
--env INPUT_REPOSITORY_PATH="${{ env.REPOSITORY_PATH }}" \
--env INPUT_FILE_PATTERN="${{ env.FILE_PATTERN }}" \
--env INPUT_TAG_NAME="${{ env.TAG_NAME }}" \
--env INPUT_TAG_MESSAGE="${{ env.TAG_MESSAGE }}" \
--env INPUT_TAG_REFERENCE="${{ env.TAG_REFERENCE }}" \
--env GITHUB_TOKEN="${{ secrets.PAT_TOKEN }}" \
--volume ${{ github.workspace }}:/app \
--rm ${{ env.TEST_TAG }}
- name: Confirm Tag Reference
run: |
git show -1 v1.0.1
git show -1 test1-${{ github.run_id }}
- name: Delete Tag
env:
USER_EMAIL: actions@github.com
USER_NAME: GitHub Actions
BRANCH: main
TAG_NAME: test1-${{ github.run_id }}
TAG_MESSAGE: test1 ${{ github.run_id }}
run: |
docker run \
--env INPUT_USER_EMAIL="${{ env.USER_EMAIL }}" \
--env INPUT_USER_NAME="${{ env.USER_NAME }}" \
--env INPUT_BRANCH="${{ env.BRANCH }}" \
--env INPUT_TAG_NAME="${{ env.TAG_NAME }}" \
--env INPUT_DELETE_TAG="true" \
--env GITHUB_TOKEN="${{ secrets.PAT_TOKEN }}" \
--volume ${{ github.workspace }}:/app \
--rm ${{ env.TEST_TAG }}
test-action-auto-branch-false:
name: Test Auto Branch False
runs-on: ubuntu-latest
needs: build-and-push-docker
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
# token: ${{ secrets.ACCESS_TOKEN }}
# - name: Create Test File
# run: |
# echo "$(date +%s)-$RANDOM" > test/test2.txt
- name: Test Auto Branch Local Action (auto_branch false)
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: test
create_pr: true
auto_branch: false
pr_title: test-pr-title
pr_base: main
pr_branch: test
pr_labels: "test,automated,auto-branch-false-test"
repository_path: test
file_pattern: .
github_token: ${{ secrets.PAT_TOKEN }}
pr_closed: true
test-action-auto-branch-true:
name: Test Auto Branch True
runs-on: ubuntu-latest
needs: [build-and-push-docker, test-action-auto-branch-false]
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
token: ${{ secrets.PAT_TOKEN }}
- name: Create Test File
run: |
echo "$(date +%s)-$RANDOM" > test/test2.txt
- name: Test Auto Branch Local Action (auto_branch true)
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: test
create_pr: true
auto_branch: true
pr_base: main
pr_labels: "test,automated,auto-branch-true-test"
repository_path: test
file_pattern: .
delete_source_branch: true
github_token: ${{ secrets.PAT_TOKEN }}
pr_closed: true
# - name: Create Test File
# run: |
# echo "$(date +%s)-$RANDOM" > test/test3.txt
# - name: Test User Branch Local Action
# uses: ./
# with:
# user_email: actions@github.com
# user_name: GitHub Actions
# create_pr: true
# auto_branch: false
# branch: feature/pr-test-branch
# pr_base: main
# repository_path: test
# file_pattern: .
test-skip-if-empty:
name: Test Skip If Empty
runs-on: ubuntu-latest
needs: [test-action-auto-branch-true]
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
token: ${{ secrets.PAT_TOKEN }}
- name: Test Skip If Empty (should skip - no changes)
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: test
create_pr: true
auto_branch: false
pr_base: main
pr_branch: test
repository_path: test
file_pattern: .
skip_if_empty: true
pr_title: "Test Skip If Empty - No Changes (should skip)"
github_token: ${{ secrets.PAT_TOKEN }}
pr_closed: true
# 변경사항이 있을 때는 skip되지 않는지 확인
- name: Create Test File
run: |
echo "$(date +%s)-$RANDOM" > test/skip-test.txt
- name: Test Skip If Empty (should not skip - with changes)
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: test
create_pr: true
auto_branch: true
pr_base: main
pr_labels: "test,automated,skip-if-empty-test"
repository_path: test
delete_source_branch: true
file_pattern: .
skip_if_empty: true
pr_title: "Test Skip If Empty - With Changes (should not skip)"
github_token: ${{ secrets.PAT_TOKEN }}
pr_closed: true
test-pr-auto-close-pr-body:
name: Test PR Auto Close
runs-on: ubuntu-latest
needs: [test-skip-if-empty]
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
token: ${{ secrets.PAT_TOKEN }}
- name: Configure Git Safe Directory
run: git config --global --add safe.directory ${{ github.workspace }}
- name: Test PR Auto Close
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: test
pr_branch: test
create_pr: true
pr_base: main
repository_path: test
file_pattern: .
pr_closed: true
pr_labels: "test,automated,auto-close-pr-body-test"
pr_title: "Test Auto Close PR"
pr_body: |
## Test Auto Close PR
This PR will be automatically closed after creation.
github_token: ${{ secrets.PAT_TOKEN }}
# test-multi-file-pattern:
# name: Test Multiple File Pattern
# runs-on: ubuntu-latest
# needs: [test-pr-auto-close-pr-body]
# steps:
# - name: Checkout
# uses: actions/checkout@v6
# with:
# fetch-depth: 10
# token: ${{ secrets.PAT_TOKEN }}
# - name: Setup Git User
# run: |
# git config --global user.email "actions@github.com"
# git config --global user.name "GitHub Actions"
# - name: Create Test Files and Add to Git
# run: |
# # 테스트 디렉토리 생성
# mkdir -p test/multi-pattern
# cd test/multi-pattern
# # 테스트 파일 생성
# echo "Test file 1 - $(date +%s)" > file1.txt
# echo "Test file 2 - $(date +%s)" > file2.md
# echo "Test file 3 - $(date +%s)" > file3.json
# echo "Test file 4 - $(date +%s)" > file4.txt
# # 파일 확인
# echo "디렉토리 내용:"
# ls -la
# # 경로 돌아가기
# cd ../..
# - name: Test Commit with Multiple File Pattern (space separated)
# uses: ./
# with:
# user_email: actions@github.com
# user_name: GitHub Actions
# commit_message: "test: commit multiple files with space-separated pattern"
# branch: main # 메인 브랜치 사용
# repository_path: "test/multi-pattern" # 상대 경로 수정
# file_pattern: "file1.txt file2.md"
# - name: Verify Files Added
# run: |
# git log -1 --name-status
# # Verify txt and md files were added but json was not
# if git log -1 --name-status | grep -q "test/multi-pattern/file1.txt" && \
# git log -1 --name-status | grep -q "test/multi-pattern/file2.md" && \
# ! git log -1 --name-status | grep -q "test/multi-pattern/file3.json"; then
# echo "✅ Test passed: Only specified patterns were committed"
# else
# echo "❌ Test failed: Incorrect files were committed"
# exit 1
# fi
# - name: Test Commit with Another Multiple Pattern
# uses: ./
# with:
# user_email: actions@github.com
# user_name: GitHub Actions
# commit_message: "test: commit JSON file with separate pattern"
# branch: main # 메인 브랜치 사용
# repository_path: "test/multi-pattern" # 상대 경로 수정
# file_pattern: "file3.json file4.txt"
# - name: Verify Second Pattern
# run: |
# git log -1 --name-status
# # Verify json and specific txt file were added
# if git log -1 --name-status | grep -q "test/multi-pattern/file3.json" && \
# git log -1 --name-status | grep -q "test/multi-pattern/file4.txt"; then
# echo "✅ Test passed: Second pattern correctly committed files"
# else
# echo "❌ Test failed: Second pattern did not commit correct files"
# exit 1
# fi
test-pr-dry-run:
name: Test PR Dry Run
runs-on: ubuntu-latest
needs: [test-pr-auto-close-pr-body]
# needs: [test-multi-file-pattern]
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 10
token: ${{ secrets.PAT_TOKEN }}
# - name: Create Test File for PR and Add to Git
# run: |
# mkdir -p test
# echo "Test file for PR dry run - $(date +%s)" > test/pr-dry-run-test.txt
# # 파일 확인
# echo "생성된 PR 테스트 파일:"
# ls -la test/
# # 경로 돌아가기
# cd ../..
- name: Test PR Dry Run (auto_branch false)
uses: ./
with:
user_email: actions@github.com
user_name: GitHub Actions
branch: main
create_pr: true
auto_branch: false
pr_title: "Test PR Dry Run"
pr_base: test
pr_branch: main
pr_labels: "test,automated,dry-run-test"
repository_path: "test"
file_pattern: "pr-dry-run-test.txt"
github_token: ${{ secrets.PAT_TOKEN }}
pr_dry_run: true