-
Notifications
You must be signed in to change notification settings - Fork 0
314 lines (263 loc) · 13 KB
/
Copy pathissue_team_pr.yaml
File metadata and controls
314 lines (263 loc) · 13 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
name: Route Issue to Repo or Team PR
on:
issues:
types: [opened]
permissions:
contents: write
pull-requests: write
jobs:
process_repo_or_team_request:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Determine Request Type
id: classify
run: |
TITLE="${{ github.event.issue.title }}"
echo "Issue Title: $TITLE"
if [[ "$TITLE" == *"[Repo Request]"* ]]; then
echo "type=repo" >> $GITHUB_OUTPUT
elif [[ "$TITLE" == *"[TEAM]"* ]]; then
echo "type=team" >> $GITHUB_OUTPUT
else
echo "Unsupported issue type, skipping."
echo "type=skip" >> $GITHUB_OUTPUT
fi
- name: Stop if unsupported issue
if: steps.classify.outputs.type == 'skip'
run: echo "No supported issue type found. Exiting."
- name: Process Repo Request
if: steps.classify.outputs.type == 'repo'
run: |
ISSUE_BODY=$(jq -r '.issue.body' "$GITHUB_EVENT_PATH")
# Print the full issue body to debug formatting issues
echo "Full ISSUE_BODY content:"
echo "$ISSUE_BODY"
- name: Parse Issue Details
if: steps.classify.outputs.type == 'repo'
id: parse
run: |
ISSUE_BODY=$(jq -r '.issue.body' "$GITHUB_EVENT_PATH")
# Extract values by looking for the header and getting the next non-empty line
REPO_NAME=$(echo "$ISSUE_BODY" | awk -F'\n' '/^### Repository Name$/{getline; getline; print $0}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
VISIBILITY=$(echo "$ISSUE_BODY" | awk -F'\n' '/^### Visibility$/{getline; getline; print $0}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
BRANCH=$(echo "$ISSUE_BODY" | awk -F'\n' '/^### Default Branch$/{getline; getline; print $0}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
INCLUDE_README=$(echo "$ISSUE_BODY" | awk -F'\n' '/^### Include README$/{getline; getline; print $0}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Debugging Output
echo "Extracted Repository Name: '$REPO_NAME'"
echo "Extracted Visibility: '$VISIBILITY'"
echo "Extracted Branch: '$BRANCH'"
echo "Extracted Include README: '$INCLUDE_README'"
# Ensure default values if missing
if [ -z "$REPO_NAME" ]; then echo "Error: Repository Name is required"; exit 1; fi
if [ -z "$BRANCH" ]; then BRANCH="main"; fi
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
echo "VISIBILITY=$VISIBILITY" >> $GITHUB_ENV
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "INCLUDE_README=$INCLUDE_README" >> $GITHUB_ENV
- name: Set Git Identity
if: steps.classify.outputs.type == 'repo'
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
- name: Create Repository Structure
if: steps.classify.outputs.type == 'repo'
run: |
mkdir -p repos/${{ env.REPO_NAME }}
cd repos/${{ env.REPO_NAME }}
git init -b ${{ env.BRANCH }}
# Ensure at least one file exists before committing
if [[ "${{ env.INCLUDE_README }}" == "Yes" ]]; then
echo "# ${{ env.REPO_NAME }}" > README.md
else
touch .gitkeep # Create a placeholder file if README is not included
fi
git add .
git commit -m "Initial commit for ${{ env.REPO_NAME }}"
- name: Commit Changes
if: steps.classify.outputs.type == 'repo'
run: |
cd repos/${{ env.REPO_NAME }}
git add .
git commit -m "Add repository structure for ${{ env.REPO_NAME }}" || echo "No changes to commit"
- name: Create Pull Request for Repository
if: steps.classify.outputs.type == 'repo'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GH_PAT }}
title: "Create new repository: ${{ env.REPO_NAME }}"
body: |
Repository Details:
- **Name**: ${{ env.REPO_NAME }}
- **Visibility**: ${{ env.VISIBILITY }}
- **Default Branch**: ${{ env.BRANCH }}
- **Include README**: ${{ env.INCLUDE_README }}
branch: "create-${{ env.REPO_NAME }}"
- name: Process Team Request
if: steps.classify.outputs.type == 'team'
run: |
# Extract issue body from GitHub event
ISSUE_BODY="${{ github.event.issue.body }}"
# Debugging: Print the issue body
echo "Issue Body:"
echo "$ISSUE_BODY"
TEAM_NAME=$(echo "$ISSUE_BODY" | awk '/^### Team Name$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
PARENT_TEAM=$(echo "$ISSUE_BODY" | awk '/^### Parent Team \(optional\)$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
VISIBILITY=$(echo "$ISSUE_BODY" | awk '/^### Visibility$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
PURPOSE=$(echo "$ISSUE_BODY" | awk '/^### Purpose$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
MAINTAINERS=$(echo "$ISSUE_BODY" | awk '/^### Maintainers \(comma-separated\)$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
MAINTAINER_FULL_NAMES=$(echo "$ISSUE_BODY" | awk '/^### Maintainer Full Names \(comma-separated\)$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
TEAM_MEMBERS=$(echo "$ISSUE_BODY" | awk '/^### Team Members \(comma-separated\)$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
MEMBER_FULL_NAMES=$(echo "$ISSUE_BODY" | awk '/^### Team Member Full Names \(comma-separated\)$/{getline; getline; print}' | sed 's/[`]//g' | xargs)
echo "Extracted Values:"
echo "Team Name: '$TEAM_NAME'"
echo "Visibility: '$VISIBILITY'"
echo "Purpose: '$PURPOSE'"
echo "Maintainers: '$MAINTAINERS'"
echo "Maintainer Full Names: '$MAINTAINER_FULL_NAMES'"
echo "Team Members: '$TEAM_MEMBERS'"
echo "Member Full Names: '$MEMBER_FULL_NAMES'"
echo "parent_team=$PARENT_TEAM" >> "$GITHUB_ENV"
if [[ ! "$TEAM_NAME" =~ ^TEAM-[a-zA-Z0-9_-]{1,20}$ ]]; then
echo "::error::Invalid team name format: '$TEAM_NAME'"
exit 1
fi
VISIBILITY=${VISIBILITY:-Visible}
PURPOSE=${PURPOSE:-Team created via automation}
MAINTAINERS=${MAINTAINERS:-none}
TEAM_MEMBERS=${TEAM_MEMBERS:-none}
MAINTAINER_FULL_NAMES=${MAINTAINER_FULL_NAMES:-none}
MEMBER_FULL_NAMES=${MEMBER_FULL_NAMES:-none}
PARENT_TEAM=${PARENT_TEAM:-none}
echo "$TEAM_NAME" > team-name.txt
echo "team_name=$TEAM_NAME" >> "$GITHUB_ENV"
echo "visibility=$VISIBILITY" >> "$GITHUB_ENV"
echo "purpose=$PURPOSE" >> "$GITHUB_ENV"
echo "maintainers=$MAINTAINERS" >> "$GITHUB_ENV"
echo "maintainer_full_names=$MAINTAINER_FULL_NAMES" >> "$GITHUB_ENV"
echo "team_members=$TEAM_MEMBERS" >> "$GITHUB_ENV"
echo "member_full_names=$MEMBER_FULL_NAMES" >> "$GITHUB_ENV"
echo "parent_team=$PARENT_TEAM" >> "$GITHUB_ENV"
- name: Debug team info
run: |
echo "Maintainers to validate: $MAINTAINERS"
echo "Team members to validate: $TEAM_MEMBERS"
echo "Maintainer full names: $MAINTAINER_FULL_NAMES"
echo "Member full names: $MEMBER_FULL_NAMES"
env:
MAINTAINERS: ${{ env.maintainers }}
TEAM_MEMBERS: ${{ env.team_members }}
MAINTAINER_FULL_NAMES: ${{ env.maintainer_full_names }}
MEMBER_FULL_NAMES: ${{ env.member_full_names }}
- name: Validate GitHub usernames and full names
if: env.maintainers != '_No response_' || env.team_members != '_No response_'
shell: bash
run: |
validate_username_format() {
local username=$1
local username_regex='^[a-zA-Z0-9][a-zA-Z0-9-]{0,38}$'
if [[ ! "$username" =~ $username_regex ]]; then
echo "::error::Invalid GitHub username format: '$username'"
exit 1
fi
}
validate_username_existence() {
local username=$1
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/users/$username")
if echo "$response" | grep -q '"login":'; then
echo "✅ GitHub user exists: $username"
else
echo "::error::GitHub user does not exist: $username"
echo "Response: $response"
exit 1
fi
}
validate_full_name_with_api() {
local username=$1
local expected_full_name=$2
user_info=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/users/$username")
actual_name=$(echo "$user_info" | jq -r '.name')
if [[ "$actual_name" == "null" || -z "$actual_name" ]]; then
echo "::warning::No public full name for '$username'. Skipping name match."
return
fi
expected_full_name=$(echo "$expected_full_name" | xargs)
actual_name=$(echo "$actual_name" | xargs)
if [[ "${expected_full_name,,}" != "${actual_name,,}" ]]; then
echo "::error::Full name mismatch for '$username': expected '$expected_full_name', found '$actual_name'"
exit 1
fi
echo "✅ Full name matches for '$username'"
}
validate_user_block() {
local user_type=$1
local usernames=$2
local full_names=$3
if [[ "$usernames" == "_No response_" || "$usernames" == "none" || -z "$usernames" ]]; then
echo "No $user_type to validate."
return
fi
if [[ "$full_names" == "_No response_" || "$full_names" == "none" || -z "$full_names" ]]; then
echo "::error::Full names required for $user_type but missing."
exit 1
fi
IFS=',' read -ra USERNAME_ARRAY <<< "$usernames"
IFS=',' read -ra FULLNAME_ARRAY <<< "$full_names"
if [[ ${#USERNAME_ARRAY[@]} -ne ${#FULLNAME_ARRAY[@]} ]]; then
echo "::error::Mismatch in number of $user_type usernames and full names."
exit 1
fi
for i in "${!USERNAME_ARRAY[@]}"; do
username=$(echo "${USERNAME_ARRAY[$i]}" | xargs)
full_name=$(echo "${FULLNAME_ARRAY[$i]}" | xargs)
echo "🔍 Validating $user_type: '$username' -> '$full_name'"
validate_username_format "$username"
validate_username_existence "$username"
validate_full_name_with_api "$username" "$full_name"
done
}
validate_user_block "maintainers" "$MAINTAINERS" "$MAINTAINER_FULL_NAMES"
validate_user_block "team members" "$TEAM_MEMBERS" "$MEMBER_FULL_NAMES"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MAINTAINERS: ${{ env.maintainers }}
TEAM_MEMBERS: ${{ env.team_members }}
MAINTAINER_FULL_NAMES: ${{ env.maintainer_full_names }}
MEMBER_FULL_NAMES: ${{ env.member_full_names }}
- name: Create Pull Request for Team
if: steps.classify.outputs.type == 'team'
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GH_PAT }}
commit-message: "Add team: ${{ env.team_name }}"
title: "Create team: ${{ env.team_name }}"
body: |
### Team Creation Request
**Team Name:** ${{ env.team_name }}
**Parent Team:** ${{ env.parent_team }}
**Visibility:** ${{ env.visibility }}
**Purpose:** ${{ env.purpose }}
**Maintainers:** ${{ env.maintainers }}
**Maintainer Full Names:** ${{ env.maintainer_full_names }}
**Team Members:** ${{ env.team_members }}
**Team Member Full Names:** ${{ env.member_full_names }}
Generated from issue #${{ github.event.issue.number }}
branch: "team-request/${{ github.event.issue.number }}"
labels: "team-creation"
paths: |
team-name.txt
maintainers_mapping.txt
team_members_mapping.txt
- name: Post a message in Slack
uses: slackapi/slack-github-action@v2.0.0
with:
webhook: ${{ secrets.SLACK_TOKEN }}
webhook-type: incoming-webhook
payload: |
text: "*New Team PR Created: `${{ env.team_name }}`*"
blocks:
- type: section
text:
type: mrkdwn
text: ":tada: A new PR has been created for team *`${{ env.team_name }}`*!\n*Purpose:* ${{ env.purpose }}\n*Maintainers:* ${{ env.maintainers }}\n*Parent:* `${{ env.parent_team }}`\n<https://github.com/${{ github.repository }}/pull/${{ steps.create_pr.outputs.pull-request-number }}|View Pull Request>"