-
Notifications
You must be signed in to change notification settings - Fork 1
118 lines (104 loc) · 4.29 KB
/
new-repo-create.yml
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
name: new-repo-create
run-name: 'New Repo: Issue #${{ github.event.issue.number }} by @${{ github.actor }}'
on:
issue_comment:
types: [created]
jobs:
new-repo-create:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment' &&
(startsWith(github.event.comment.body, '/create-repo') &&
contains(github.event.issue.labels.*.name, 'new-repo'))
steps:
- uses: actions/checkout@v4
- name: Write GitHub context to log
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"
- uses: stefanbuck/github-issue-parser@v3
id: issue-parser
with:
template-path: .github/ISSUE_TEMPLATE/new-repo.yml
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
owner: ${{ github.repository_owner }}
- name: Set env vars
run: |
echo "REPO_NAME=${{ steps.issue-parser.outputs.issueparser_repo_name }}" >> $GITHUB_ENV
echo "REPO_DESCRIPTION=${{ steps.issue-parser.outputs.issueparser_repo_description }}" >> $GITHUB_ENV
echo "REPO_VISIBILITY=${{ steps.issue-parser.outputs.issueparser_repo_visibility }}" >> $GITHUB_ENV
# doing this again in case someone else renamed the issue
- name: Rename issue
uses: actions/github-script@v6
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
title: `New Repo: ${process.env.REPO_NAME}`
})
- name: Create repository
id: create-repo
uses: actions/github-script@v6
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
return await github.rest.repos.createInOrg({
org: context.repo.owner,
name: process.env.REPO_NAME,
description: process.env.REPO_DESCRIPTION,
visibility: process.env.REPO_VISIBILITY,
auto_init: true,
allow_merge_commit: true,
allow_rebase_merge: true,
allow_squash_merge: true,
delete_branch_on_merge: true,
allow_update_branch: true,
})
- name: Add created label and close issue
if: ${{ success() }}
uses: actions/github-script@v6
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["created", "new-repo"],
state: "closed"
})
- name: Post successful message
uses: actions/github-script@v6
if: ${{ success() }}
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
let commentBody
commentBody = `🚀 New repo created successfully.
Visit the following link for the repo: [https://github.com/${{ github.repository_owner }}/${{ steps.issue-parser.outputs.issueparser_repo_name }}](https://github.com/${{ github.repository_owner }}/${{ steps.issue-parser.outputs.issueparser_repo_name }})
`
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody.replace(/ +/g, '')
})
- name: Post failure message
if: ${{ failure() }}
uses: actions/github-script@v6
with:
script: |
let commentBody
commentBody = `😢 The new repo could not be created. Please review the [action logs](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) for more information.`
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody.replace(/ +/g, '')
})