-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathvalidator.js
65 lines (64 loc) · 2.15 KB
/
validator.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
const NopCommand = require('../nopcommand')
module.exports = class Validator {
constructor (nop, github, repo, settings, log) {
this.github = github
this.pattern = settings.pattern
// this.regex = /[a-zA-Z0-9_-]+_\w[a-zA-Z0-9_-]+.*/gm
this.regex = new RegExp(this.pattern, 'gm')
this.repo = repo
this.log = log
this.nop = nop
}
sync () {
try {
return this.github.repos.getAllTopics({
owner: this.repo.owner,
repo: this.repo.repo,
mediaType: {
previews: ['mercy']
}
}).then(res => {
if (this.repo.repo.search(this.regex) >= 0) {
this.log(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, null, `Passed Validation for pattern ${this.pattern}`)
])
}
if (res.data.names.find(x => x === 'validation-error')) {
res.data.names = res.data.names.filter(x => x !== 'validation-error')
return this.github.repos.replaceAllTopics({
owner: this.repo.owner,
repo: this.repo.repo,
names: res.data.names,
mediaType: {
previews: ['mercy']
}
})
}
} else {
this.log(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, null, `Failed Validation for pattern ${this.pattern}`, 'ERROR')
])
}
if (!res.data.names.find(x => x === 'validation-error')) {
res.data.names.push('validation-error')
return this.github.repos.replaceAllTopics({
owner: this.repo.owner,
repo: this.repo.repo,
names: res.data.names,
mediaType: {
previews: ['mercy']
}
})
}
}
})
.catch(() => {})
} catch (error) {
this.log(`Error in Validation checking ${error}`)
}
}
}