Skip to content

New Repo: Issue #74 by @josh-issueops-bot[bot] #98

New Repo: Issue #74 by @josh-issueops-bot[bot]

New Repo: Issue #74 by @josh-issueops-bot[bot] #98

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, '')
})