This repository was archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (64 loc) · 2.08 KB
/
script.js
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
import {appAuth} from '@stoe/octoherd-script-common'
import {setTimeout} from 'timers/promises'
/**
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {object} options
* @param {string} [options.excludes='']
* @param {boolean} [options.deleteArchived=false]
* @param {int} [options.appId=0]
* @param {string} [options.privateKey='']
* @param {boolean} [options.dryRun=false]
*/
export async function script(
octokit,
repository,
{excludes = '', deleteArchived = false, appId = 0, privateKey = '', dryRun = false},
) {
const {
archived,
name: repo,
owner: {login: owner},
clone_url: url,
} = repository
// skip excluded repos
const exclude = excludes.split(',').map(s => s.trim())
if (exclude.includes(repo)) {
octokit.log.info(` 🙈 ${owner}/${repo} excluded, skipping`)
return
}
// skip archived repos, unless deleteArchived is true
if (archived && !deleteArchived) {
octokit.log.info(` 📦 ${owner}/${repo} archived, skipping`)
return
}
try {
let ok = octokit
if (appId && privateKey) {
try {
ok = await appAuth(repository, appId, privateKey)
octokit.log.info(` 🤖 authenticated as app`)
} catch (error) {
octokit.log.info({error}, ` ❌ failed to authenticate as app`)
return
}
}
if (dryRun) {
octokit.log.info({delete: false, url}, ` 🐢 ${owner}/${repo} would be deleted`)
return
}
// https://docs.github.com/en/rest/reference/repos#delete-a-repository
await ok.request('DELETE /repos/{owner}/{repo}', {
owner,
repo,
})
octokit.log.info(` 🧹 ${owner}/${repo} deleted`)
// sleep 1 second
await setTimeout(1000)
} catch (error) {
octokit.log.info(
{delete: false, url, error: error.message, status: error.status},
` ❌ ${owner}/${repo} not deleted`,
)
}
}