-
Notifications
You must be signed in to change notification settings - Fork 0
85 lines (72 loc) · 2.95 KB
/
Copy pathcreate_repo.yaml
File metadata and controls
85 lines (72 loc) · 2.95 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
name: Create Repository After PR Merge
on:
pull_request:
types: [closed]
jobs:
create_repo:
if: |
github.event.pull_request.merged == true &&
contains(github.event.pull_request.labels.*.name, 'repo-creation')
runs-on: ubuntu-latest
steps:
- name: Extract PR Details
id: extract
run: |
PR_BODY="${{ github.event.pull_request.body }}"
REPO_NAME=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Name\*\*: ).*')
BRANCH=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Default Branch\*\*: ).*')
INCLUDE_README=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Include README\*\*: ).*')
VISIBILITY=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Repository Visibility\*\*: ).*')
CODEOWNERS=$(echo "$PR_BODY" | grep -oP '(?<=- \*\*Code Owner\(s\)\*\*: ).*')
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "INCLUDE_README=$INCLUDE_README" >> $GITHUB_ENV
echo "VISIBILITY=$VISIBILITY" >> $GITHUB_ENV
echo "CODEOWNERS=$CODEOWNERS" >> $GITHUB_ENV
- name: Set Repository Visibility
run: |
if [[ "$VISIBILITY" == "public" ]]; then
echo "REPO_VISIBILITY=false" >> $GITHUB_ENV
elif [[ "$VISIBILITY" == "private" || "$VISIBILITY" == "internal" ]]; then
echo "REPO_VISIBILITY=true" >> $GITHUB_ENV
else
echo "Error: Invalid visibility option" && exit 1
fi
- name: Create Repository
uses: actions/github-script@v6
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
with:
github-token: ${{ secrets.GH_PAT }}
script: |
try {
await github.rest.repos.createInOrg({
org: context.repo.owner,
name: process.env.REPO_NAME,
private: process.env.REPO_VISIBILITY === 'true',
auto_init: true,
license_template: 'mit'
});
console.log(`Created repository: ${process.env.REPO_NAME}`);
} catch (error) {
core.setFailed(`Repository creation failed: ${error}`);
}
- name: Add README and CODEOWNERS if requested
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
git config --global user.name "github-actions"
git config --global user.email "actions@github.com"
git clone https://${{ github.actor }}:${{ secrets.GH_PAT }}@github.com/${{ github.repository_owner }}/${{ env.REPO_NAME }}.git
cd "${{ env.REPO_NAME }}"
mkdir -p .github
if [[ "$INCLUDE_README" == "Yes" ]]; then
echo "# $REPO_NAME" > README.md
git add README.md
fi
if [[ -n "$CODEOWNERS" ]]; then
echo "* $CODEOWNERS" > .github/CODEOWNERS
git add .github/CODEOWNERS
fi
git commit -m "Add README and CODEOWNERS file"
git push