-
Notifications
You must be signed in to change notification settings - Fork 4
172 lines (141 loc) ยท 6.63 KB
/
Copy pathautobranch.yml
File metadata and controls
172 lines (141 loc) ยท 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
name: Auto Create Branch
on:
issues:
types: [opened]
jobs:
create-branch:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Extract branch name from issue form
id: extract
run: |
# Issue body๋ฅผ ํ์ผ๋ก ์ ์ฅ (ํน์๋ฌธ์ ์ฒ๋ฆฌ)
cat << 'EOF' > issue_body.txt
${{ github.event.issue.body }}
EOF
# ์ด์ ์ ๋ชฉ ์ ์ฅ
ISSUE_TITLE="${{ github.event.issue.title }}"
echo "ํ์ฌ ์ด์ ์ ๋ชฉ: $ISSUE_TITLE"
# ๋ธ๋์น๋ช
์ถ์ถ ("๋ธ๋์น๋ช
" ๋๋ "Branch" ์น์
)
BRANCH_NAME=$(grep -A 2 "๋ธ๋์น๋ช
\|Branch" issue_body.txt | tail -1 | xargs)
# ๋ธ๋์น๋ช
์ฌ์ถ์ถ (๋น ์ค์ด๋ ํค๋๊ฐ ์ถ์ถ๋ ๊ฒฝ์ฐ)
if [ -z "$BRANCH_NAME" ] || [[ "$BRANCH_NAME" == "###"* ]] || [[ "$BRANCH_NAME" == "๐ณ"* ]]; then
BRANCH_NAME=$(sed -n '/Branch/,/###/{/Branch/n; /###/!p;}' issue_body.txt | grep -v "^$" | head -1 | xargs)
fi
# ๋ธ๋์น ํ์
์ถ์ถ ("๋ธ๋์น ํ์
" ๋๋ "branchType" ์น์
)
BRANCH_TYPE=$(grep -A 2 "๋ธ๋์น ํ์
\|branchType" issue_body.txt | tail -1 | xargs)
# ๋ธ๋์น ํ์
์ฌ์ถ์ถ (๋น ์ค์ด๋ ํค๋๊ฐ ์ถ์ถ๋ ๊ฒฝ์ฐ)
if [ -z "$BRANCH_TYPE" ] || [[ "$BRANCH_TYPE" == "###"* ]] || [[ "$BRANCH_TYPE" == "๐"* ]]; then
BRANCH_TYPE=$(sed -n '/๋ธ๋์น ํ์
/,/###/{/๋ธ๋์น ํ์
/n; /###/!p;}' issue_body.txt | grep -v "^$" | head -1 | xargs)
fi
echo "์ถ์ถ๋ ๋ธ๋์น ํ์
: $BRANCH_TYPE"
# ์ด์ ์ ๋ชฉ ์์ฑ ([Type] #๋ฒํธ - ์ ๋ชฉ ํ์)
if [ -n "$BRANCH_TYPE" ] && [ -n "$ISSUE_TITLE" ]; then
if [[ ! "$ISSUE_TITLE" =~ ^\[.*\] ]]; then
# ํ์ค์นผ์ผ์ด์ค ๋ณํ
BRANCH_TYPE_PASCAL=$(echo "$BRANCH_TYPE" | sed 's/\b\(.\)/\u\1/')
NEW_ISSUE_TITLE="[${BRANCH_TYPE_PASCAL}] #${{ github.event.issue.number }} - ${ISSUE_TITLE}"
echo "์๋ก์ด ์ด์ ์ ๋ชฉ: $NEW_ISSUE_TITLE"
else
NEW_ISSUE_TITLE="$ISSUE_TITLE"
echo "์ด์ ์ ๋ชฉ์ ์ด๋ฏธ ํ์
์ด ํฌํจ๋์ด ์์ต๋๋ค."
fi
else
NEW_ISSUE_TITLE="$ISSUE_TITLE"
fi
# ๋ธ๋์น๋ช
์์ฑ
if [ -z "$BRANCH_NAME" ]; then
echo "๋ธ๋์น๋ช
์ ์ฐพ์ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ."
BRANCH_NAME="issue-${{ github.event.issue.number }}"
else
# Git ๋ธ๋์น๋ช
๊ท์น์ ๋ง๊ฒ ์ ์ (ํ๊ธ ํ์ฉ)
# 1. ๊ณต๋ฐฑ์ ํ์ดํ์ผ๋ก ๋ณ๊ฒฝ
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/ /-/g')
# 2. Git ๋นํ์ฉ ํน์๋ฌธ์ ์ ๊ฑฐ (:, ^, ~, ?, *, [, \, .., @{)
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/[:^~?*\[\\]//g; s/\.\.//g; s/@{//g')
# 3. ์ฐ์๋ ํ์ดํ/์ฌ๋์ ์ ๊ฑฐ
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/--*/-/g; s/\/\/*\//\//g')
# 4. ์๋ค ํ์ดํ/์ฌ๋์/์ ์ ๊ฑฐ
BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/^[-\/\.]*//; s/[-\/\.]*$//')
# 5. ๋น ๋ฌธ์์ด ๊ฒ์ฆ
if [ -z "$BRANCH_NAME" ]; then
echo "์ ์ ํ ๋ธ๋์น๋ช
์ด ๋น์ด์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ."
BRANCH_NAME="issue-${{ github.event.issue.number }}"
else
# ๋ธ๋์น ํ์
prefix ์ถ๊ฐ (type/)
if [ -n "$BRANCH_TYPE" ]; then
if [[ ! "$BRANCH_NAME" =~ ^$BRANCH_TYPE/ ]]; then
BRANCH_NAME="${BRANCH_TYPE}/${BRANCH_NAME}"
fi
fi
# ์ด์๋ฒํธ prefix ์ถ๊ฐ (๋ฒํธ-)
BRANCH_NAME="${{ github.event.issue.number }}-$BRANCH_NAME"
fi
fi
echo "์ต์ข
๋ธ๋์น๋ช
: $BRANCH_NAME"
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "new_issue_title=$NEW_ISSUE_TITLE" >> $GITHUB_OUTPUT
echo "branch_type=$BRANCH_TYPE" >> $GITHUB_OUTPUT
# ์์ ํ์ผ ์ ๋ฆฌ
rm -f issue_body.txt
- name: Update issue title and labels
run: |
NEW_TITLE="${{ steps.extract.outputs.new_issue_title }}"
BRANCH_TYPE="${{ steps.extract.outputs.branch_type }}"
# ๋ธ๋์น ํ์
๋ณ ์ด๋ชจ์ง ๋ผ๋ฒจ ๋งคํ
case "$BRANCH_TYPE" in
"feat") LABEL="โจFeature" ;;
"fix") LABEL="๐BugFix" ;;
"hotfix") LABEL="๐จHotfix" ;;
"refactor") LABEL="โป๏ธRefactor" ;;
"test") LABEL="โ
Test" ;;
"docs") LABEL="๐Docs" ;;
"chore") LABEL="๐ ๏ธ Chore" ;;
*) LABEL="$BRANCH_TYPE" ;;
esac
# ์ด์ ๋ฉํ๋ฐ์ดํฐ ์
๋ฐ์ดํธ (์ ๋ชฉ, ๋ผ๋ฒจ, ๋ด๋น์)
if [ -n "$NEW_TITLE" ] && [ -n "$LABEL" ]; then
curl -X PATCH \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
-d "{\"title\":\"$NEW_TITLE\",\"labels\":[\"$LABEL\"],\"assignees\":[\"${{ github.event.issue.user.login }}\"]}"
echo "์ด์ ์ ๋ชฉ ์
๋ฐ์ดํธ: $NEW_TITLE"
echo "๋ผ๋ฒจ ์ค์ : $LABEL"
echo "๋ด๋น์ ํ ๋น: ${{ github.event.issue.user.login }}"
fi
- name: Create branch
run: |
BRANCH_NAME="${{ steps.extract.outputs.branch_name }}"
# Git ์ค์
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Main ๋ธ๋์น ๊ธฐ๋ฐ์ผ๋ก ์ ๋ธ๋์น ์์ฑ
git checkout main
git pull origin main
git checkout -b "$BRANCH_NAME"
git push origin "$BRANCH_NAME"
echo "๋ธ๋์น '$BRANCH_NAME' ์์ฑ ์๋ฃ"
- name: Add comment to issue
env:
BRANCH_NAME: ${{ steps.extract.outputs.branch_name }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# ์ด์์ ๋ธ๋์น ์์ฑ ์๋ฆผ ๋๊ธ ์ถ๊ฐ
COMMENT=$(cat <<EOF
๐ฟ ๋ธ๋์น๊ฐ ์์ฑ๋์์ต๋๋ค!
\`\`\`
$BRANCH_NAME
\`\`\`
EOF
)
gh issue comment ${{ github.event.issue.number }} --body "$COMMENT"
echo "์ด์์ ๋๊ธ ์ถ๊ฐ ์๋ฃ"