-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy patharchive.js
More file actions
108 lines (89 loc) · 2.69 KB
/
archive.js
File metadata and controls
108 lines (89 loc) · 2.69 KB
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
const NopCommand = require('../nopcommand')
module.exports = class Archive {
constructor (nop, github, repo, settings, log) {
this.github = github
this.repo = repo
this.settings = settings
this.log = log
this.nop = nop
}
async getRepo () {
try {
const { data } = await this.github.rest.repos.get({
owner: this.repo.owner,
repo: this.repo.repo
})
return data
} catch (error) {
if (error.status === 404) {
return null
}
throw error
}
}
async updateRepoArchiveStatus (archived) {
const action = archived ? 'archive' : 'unarchive'
if (this.nop) {
const change = { msg: 'Change found', additions: {}, modifications: { archived: action }, deletions: {} }
return new NopCommand(
this.constructor.name,
this.repo,
this.github.rest.repos.update.endpoint(this.settings),
change,
'INFO'
)
}
const { data } = await this.github.rest.repos.update({
owner: this.repo.owner,
repo: this.repo.repo,
archived
})
this.log.debug({ result: data }, `Repo ${this.repo.owner}/${this.repo.repo} ${action}d`)
}
getDesiredArchiveState () {
if (typeof this.settings?.archived === 'undefined') {
return null
}
return typeof this.settings.archived === 'boolean'
? this.settings.archived
: this.settings.archived === 'true'
}
shouldArchive (repository = this.repository) {
const desiredState = this.getDesiredArchiveState()
if (desiredState === null) return false
return !repository?.archived && desiredState
}
shouldUnarchive (repository = this.repository) {
const desiredState = this.getDesiredArchiveState()
if (desiredState === null) return false
return repository?.archived && !desiredState
}
isArchived () {
return this.repository?.archived
}
async getState () {
this.repository = await this.getRepo()
return {
isArchived: this.isArchived(),
shouldArchive: this.shouldArchive(),
shouldUnarchive: this.shouldUnarchive()
}
}
async sync () {
this.repository = await this.getRepo()
const results = []
if (!this.repository) {
this.log.warn(`Repo ${this.repo.owner}/${this.repo.repo} not found, skipping archive sync`)
return results
}
const shouldArchive = this.shouldArchive()
const shouldUnarchive = this.shouldUnarchive()
if (!shouldArchive && !shouldUnarchive) {
this.log.debug(`No archive changes needed for ${this.repo.owner}/${this.repo.repo}`)
return results
}
const archived = shouldArchive
results.push(await this.updateRepoArchiveStatus(archived))
return results
}
}