├── CHANGELOG.md ├── Dockerfile ├── LICENSE ├── README.md ├── action.yml └── src └── entrypoint.sh /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | v0.1.0: release 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | LABEL author="Lee Suho " 3 | LABEL "com.github.actions.name"="Create PR if merged hotfix in master" 4 | LABEL "com.github.actions.description"="Create PR if merged hotfix in master, for git-flow" 5 | LABEL "com.github.actions.icon"="send" 6 | LABEL "com.github.actions.color"="blue" 7 | RUN apk add bash ca-certificates curl jq 8 | COPY src /action/src 9 | ENTRYPOINT ["/action/src/entrypoint.sh"] 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peoplefund-tech/gitflow-automation/6894d3b9e4a4e0dfab440a723c652c5570ebc7e1/LICENSE -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gitflow automation 2 | gitflow의 여러 정책들을 자동화 해 주는 github action 입니다. 3 | 4 | ## 기능들 5 | 1. 특정 prefix가 붙은 브랜치가 base 브랜치에 머지되면 자동으로 target 브랜치에도 머지합니다. 6 | 7 | ## 사용방법 8 | ```yaml 9 | name: gitflow-automation 10 | on: 11 | pull_request: 12 | types: [closed] 13 | jobs: 14 | create-auto-pr: 15 | name: Gitflow Automation 16 | runs-on: ubuntu-latest 17 | env: 18 | steps: 19 | - name: gitflow-automation 20 | uses: peoplefund-tech/gitflow-automation@v0.1.1 21 | env: 22 | BRANCH_PREFIX: "hotfix/" 23 | TARGET_BRANCH: "develop" 24 | BASE_BRANCH: "master" 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 26 | BOT_TOKEN: ${{ secrets.BOT_TOKEN }} # 선택사항 27 | 28 | ``` 29 | - `BRANCH_PREFIX`: hotfix 브랜치의 prefix를 적어 주세요. 30 | - `TARGET_BRANCH`: automerge되어야 하는 브랜치를 적어주세요. 31 | - `BASE_BRANCH`: event가 발생할 브랜치를 적어주세요. 32 | - `GITHUB_TOKEN`: 특별한 경우가 아닌 이상 예제와 동일하게 적어주시면 됩니다. 33 | - `BOT_TOKEN`: PR이 1명 이상의 assigner가 필요한 경우 assign할 bot의 token을 등록해 주세요. github repository의 secrets를 이용하는 것을 권장합니다. 34 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: "Gitflow automation" 2 | description: "Gitflow automation" 3 | runs: 4 | using: "docker" 5 | image: "Dockerfile" 6 | branding: 7 | icon: "git-pull-request" 8 | color: "blue" 9 | -------------------------------------------------------------------------------- /src/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | echo "Start Creating PR action" 5 | 6 | ##### CONSTANCE 7 | OUTPUT_PATH=".output" 8 | 9 | ##### VARIABLE 10 | IS_NEED_APPROVE="false" 11 | 12 | ##### FUNCTION 13 | function create_pr() 14 | { 15 | TITLE="hotfix auto merged by $(jq -r ".pull_request.head.user.login" "$GITHUB_EVENT_PATH" | head -1)." 16 | REPO_FULLNAME=$(jq -r ".repository.full_name" "$GITHUB_EVENT_PATH") 17 | RESPONSE_CODE=$(curl -o $OUTPUT_PATH -s -w "%{http_code}\n" \ 18 | --data "{\"title\":\"$TITLE\", \"head\": \"$BASE_BRANCH\", \"base\": \"$TARGET_BRANCH\"}" \ 19 | -X POST \ 20 | -H "Authorization: token $GITHUB_TOKEN" \ 21 | -H "Accept: application/vnd.github.v3+json" \ 22 | "https://api.github.com/repos/$REPO_FULLNAME/pulls") 23 | echo "head: $SOURCE_BRANCH, base: $TARGET_BRANCH" 24 | echo "Create PR Response:" 25 | echo "Code : $RESPONSE_CODE" 26 | if [[ "$RESPONSE_CODE" != "201" ]]; 27 | then 28 | exit 1 29 | fi 30 | } 31 | 32 | function merge_pr() 33 | { 34 | REPO_FULLNAME=$(jq -r ".repository.full_name" "$GITHUB_EVENT_PATH") 35 | COMMIT_TITLE="$(jq -r ".title" "$OUTPUT_PATH" | head -1)" 36 | COMMIT_MESSAGE="$(jq -r ".body.head_commit.message" "$OUTPUT_PATH" | head -1)" 37 | HEAD_SHA="$(jq -r ".head.sha" "$OUTPUT_PATH" | head -1)" 38 | MERGE_METHOD="merge" 39 | PULL_NUMBER="$(jq -r ".number" "$OUTPUT_PATH" | head -1)" 40 | RESPONSE_CODE=$(curl -o $OUTPUT_PATH -s -w "%{http_code}\n" \ 41 | --data "{\"commit_title\":\"$COMMIT_TITLE\", \"commit_message\":\"$COMMIT_MESSAGE\", \"sha\": \"$HEAD_SHA\", \"merge_method\": \"$MERGE_METHOD\"}" \ 42 | -X PUT \ 43 | -H "Authorization: token $GITHUB_TOKEN" \ 44 | -H "Accept: application/vnd.github.v3+json" \ 45 | "https://api.github.com/repos/$REPO_FULLNAME/pulls/$PULL_NUMBER/merge") 46 | echo "Merged PR Response:" 47 | echo "Code : $RESPONSE_CODE" 48 | } 49 | 50 | function approve_pr() 51 | { 52 | REPO_FULLNAME=$(jq -r ".repository.full_name" "$GITHUB_EVENT_PATH") 53 | PULL_NUMBER="$(jq -r ".number" "$OUTPUT_PATH" | head -1)" 54 | RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}\n" \ 55 | --data "{\"event\":\"APPROVE\"}" \ 56 | -X POST \ 57 | -H "Authorization: token $BOT_TOKEN" \ 58 | -H "Accept: application/vnd.github.v3+json" \ 59 | "https://api.github.com/repos/$REPO_FULLNAME/pulls/$PULL_NUMBER/reviews") 60 | echo "Approve PR Response:" 61 | echo "Code : $RESPONSE_CODE" 62 | } 63 | 64 | function check_token_is_defined() 65 | { 66 | if [[ -z "$GITHUB_TOKEN" ]]; 67 | then 68 | echo "Undefined GITHUB_TOKEN environment variable." 69 | exit 1 70 | fi 71 | } 72 | 73 | function check_bot_token_is_defined() 74 | { 75 | if [[ "$BOT_TOKEN" != null ]]; 76 | then 77 | echo "Bot Token Avaliable" 78 | IS_NEED_APPROVE=true 79 | else 80 | echo "Bot Token not Avaliable" 81 | fi 82 | } 83 | 84 | function check_is_pr_is_merged() 85 | { 86 | echo "$(jq -r ".pull_request.merged" "$GITHUB_EVENT_PATH")" 87 | if [[ "$(jq -r ".pull_request.merged" "$GITHUB_EVENT_PATH")" == "false" ]]; 88 | then 89 | echo "This PR has not merged event." 90 | exit 0 91 | fi 92 | } 93 | 94 | function check_is_pr_branch_has_prefix() 95 | { 96 | echo "$(jq -r ".pull_request.head.ref" "$GITHUB_EVENT_PATH")" 97 | if [[ "$(jq -r ".pull_request.head.ref" "$GITHUB_EVENT_PATH")" != "$BRANCH_PREFIX"* ]]; 98 | then 99 | echo "This PR head branch do not have prefix." 100 | exit 0 101 | fi 102 | } 103 | 104 | function check_is_merged_base_branch_is_trigger() 105 | { 106 | echo "$(jq -r ".pull_request.base.ref" "$GITHUB_EVENT_PATH")" 107 | if [[ "$(jq -r ".pull_request.base.ref" "$GITHUB_EVENT_PATH")" != "$BASE_BRANCH"* ]]; 108 | then 109 | echo "This PR base branch is not base branch." 110 | exit 0 111 | fi 112 | 113 | } 114 | 115 | function check_validate() 116 | { 117 | check_token_is_defined 118 | check_bot_token_is_defined 119 | check_is_pr_is_merged 120 | check_is_pr_branch_has_prefix 121 | check_is_merged_base_branch_is_trigger 122 | } 123 | 124 | ##### MAIN 125 | function main() 126 | { 127 | check_validate 128 | create_pr 129 | if [[ "$IS_NEED_APPROVE" == "true" ]]; 130 | then 131 | approve_pr 132 | fi 133 | merge_pr 134 | } 135 | 136 | ##### EXECUTE 137 | main 138 | --------------------------------------------------------------------------------