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
124 lines (111 loc) · 3.11 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
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
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 {int} [options.appId=0]
* @param {string} [options.privateKey='']
* @param {boolean} [options.dryRun=false]
*/
export async function script(octokit, repository, {appId = 0, privateKey = '', dryRun = false}) {
const {
archived,
name: repo,
owner: {login: owner},
clone_url: url,
} = repository
// skip non-archived repos
if (!archived) 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
}
}
const {
repository: {
issues: {totalCount: issuesCount, nodes: issues},
pullRequests: {totalCount: pullRequestsCount, nodes: pullRequests},
},
} = await ok.graphql(
`query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues(first: 50, states: [OPEN]) {
totalCount
nodes {
number
title
url
}
}
pullRequests(first: 50, states: [OPEN]) {
totalCount
nodes {
number
title
url
}
}
}
}
`,
{
owner,
repo,
},
)
if (issuesCount || pullRequestsCount) {
// unarchive repository
if (!dryRun) {
// https://docs.github.com/en/rest/repos/repos#update-a-repository
await ok.request(`PATCH /repos/{owner}/{repo}`, {
owner,
repo,
archived: false,
})
// sleep 1 second
await setTimeout(1000)
}
// close all issues and pull requests
for (const {number, title, url: i} of [...issues, ...pullRequests]) {
if (dryRun) {
octokit.log.info({title, url: i}, ` 🐢 dry-run`)
} else {
// https://docs.github.com/en/rest/reference/issues#update-an-issue
await ok.request(`PATCH /repos/{owner}/{repo}/issues/{issue_number}`, {
owner,
repo,
issue_number: number,
state: 'closed',
state_reason: 'not_planned',
})
octokit.log.info({title, url: i}, ` 🛑 closed as not planned`)
// sleep 1 second
await setTimeout(1000)
}
}
// archive repository
if (!dryRun) {
// https://docs.github.com/en/rest/repos/repos#update-a-repository
await ok.request(`PATCH /repos/{owner}/{repo}`, {
owner,
repo,
archived: true,
})
// sleep 1 second
await setTimeout(1000)
}
octokit.log.info(` ✅ ${url}`)
return
}
} catch (error) {
octokit.log.error(` ❌ ${error.message}`)
return
}
}