-
Notifications
You must be signed in to change notification settings - Fork 1
129 lines (113 loc) · 4.48 KB
/
delete-repos-delete.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
119
120
121
122
123
124
125
126
127
128
129
name: delete-repos-delete
run-name: 'Delete Repos: Issue #${{ github.event.issue.number }} by @${{ github.actor }}'
on:
issue_comment:
types: [created]
jobs:
delete-repos-delete:
runs-on: ubuntu-latest
if: github.event_name == 'issue_comment' &&
(startsWith(github.event.comment.body, '/delete-repos') &&
contains(github.event.issue.labels.*.name, 'delete-repos'))
permissions:
contents: read
issues: write
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/delete-repos.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 }}
# doing this again in case someone else renamed the issue
- name: Rename issue
uses: actions/github-script@v6
env:
REPOSITORIES: ${{ steps.issue-parser.outputs.issueparser_repositories }}
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
let repositories = process.env.REPOSITORIES.trim().split('\n');
let issueTitleRepo
if (repositories.length > 1) {
issueTitleRepo = `${repositories[0].trim()}...`;
} else {
issueTitleRepo = repositories[0].trim();
}
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
title: `Delete Repo(s): ${{ github.actor }} (${issueTitleRepo}) (Count: ${repositories.length})`
})
- name: ApproveOps
uses: joshjohanning/approveops@v2
id: check-approval
with:
token: ${{ steps.app-token.outputs.token }}
approve-command: /approve
team-name: approver-team
fail-if-approval-not-found: true
post-successful-approval-comment: false
- name: npm install
run: npm i octokit@2.1.0 @actions/core
- name: Delete Repos
uses: actions/github-script@v6
with:
script: |
const repositories = process.env.REPOSITORIES;
await require('./.github/scripts/delete-repos.js') ({repositories});
env:
APP_TOKEN: ${{ steps.app-token.outputs.token }}
REPOSITORIES: ${{ steps.issue-parser.outputs.issueparser_repositories }}
- name: Add deleted 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: ["deleted", "delete-repos"],
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 = `🚮 Repo(s) deleted successfully.
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, '')
})
- name: Post failure message
if: ${{ failure() && steps.check-approval.outputs.approved == 'true' }}
uses: actions/github-script@v6
with:
script: |
let commentBody
commentBody = `😢 The repo(s) could not be deleted. 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, '')
})