-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathdiffable.js
159 lines (145 loc) · 5.42 KB
/
diffable.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Base class to make it easy to check for changes to a list of items
//
// class Thing extends Diffable {
// find() {
// }
//
// comparator(existing, attrs) {
// }
//
// changed(existing, attrs) {
// }
//
// update(existing, attrs) {
// }
//
// add(attrs) {
// }
//
// remove(existing) {
// }
// }
const ErrorStash = require('./errorStash')
const MergeDeep = require('../mergeDeep')
const NopCommand = require('../nopcommand')
const Glob = require('../glob')
const ignorableFields = ['id', 'node_id', 'default', 'url']
module.exports = class Diffable extends ErrorStash {
constructor (nop, github, repo, entries, log, errors) {
super(errors)
this.github = github
this.repo = repo
this.entries = entries
this.log = log
this.nop = nop
}
filterEntries () {
let filteredEntries = Array.from(this.entries)
filteredEntries = filteredEntries.filter(attrs => {
if (!Array.isArray(attrs.exclude)) return true
const excludeGlobs = attrs.exclude.map(exc => new Glob(exc))
const isExcluded = excludeGlobs.some(glob => glob.test(this.repo.repo))
return !isExcluded
})
filteredEntries = filteredEntries.filter(attrs => {
if (!Array.isArray(attrs.include)) return true
const includeGlobs = attrs.include.map(exc => new Glob(exc))
const isIncluded = includeGlobs.some(glob => glob.test(this.repo.repo))
return isIncluded
})
filteredEntries = filteredEntries.map(e => {
const { exclude, include, ...o } = e
return o
})
return filteredEntries
}
sync () {
const resArray = []
if (this.entries) {
let filteredEntries = this.filterEntries()
// this.log.debug(`filtered entries are ${JSON.stringify(filteredEntries)}`)
return this.find().then(existingRecords => {
this.log.debug(` ${JSON.stringify(existingRecords, null, 2)} \n\n ${JSON.stringify(filteredEntries, null, 2)} `)
const mergeDeep = new MergeDeep(this.log, this.github, ignorableFields)
const compare = mergeDeep.compareDeep(existingRecords, filteredEntries)
const results = { msg: 'Changes found', additions: compare.additions, modifications: compare.modifications, deletions: compare.deletions }
this.log.debug(`Results of comparing ${this.constructor.name} diffable target ${JSON.stringify(existingRecords)} with source ${JSON.stringify(filteredEntries)} is ${results}`)
if (!compare.hasChanges) {
this.log.debug(`There are no changes for ${this.constructor.name} for repo ${this.repo}. Skipping changes`)
return Promise.resolve()
} else {
if (this.nop) {
resArray.push(new NopCommand(this.constructor.name, this.repo, null, results, 'INFO'))
}
}
// Remove any null or undefined values from the diffables (usually comes from repo override)
for (const entry of filteredEntries) {
for (const key of Object.keys(entry)) {
if (entry[key] === null || entry[key] === undefined) {
delete entry[key]
}
}
}
// Delete any diffable that now only has name and no other attributes
filteredEntries = filteredEntries.filter(entry => Object.keys(entry).filter(key => !MergeDeep.NAME_FIELDS.includes(key)).length !== 0)
const changes = []
existingRecords.forEach(x => {
if (!filteredEntries.find(y => this.comparator(x, y))) {
const change = this.remove(x).then(res => {
if (this.nop) {
return resArray.push(res)
}
return res
})
changes.push(change)
}
})
filteredEntries.forEach(attrs => {
const existing = existingRecords.find(record => {
return this.comparator(record, attrs)
})
if (!existing) {
const change = this.add(attrs).then(res => {
if (this.nop) {
return resArray.push(res)
}
return res
})
changes.push(change)
} else if (this.changed(existing, attrs)) {
const change = this.update(existing, attrs).then(res => {
if (this.nop) {
return resArray.push(res)
}
return res
})
changes.push(change)
}
})
// if (changes.length === 0) {
// if (this.nop) {
// return Promise.resolve([
// // {plugin: this.constructor.name, repo: this.repo, action: `No changes`},
// ])
// }
// }
if (this.nop) {
return Promise.resolve(resArray)
}
return Promise.all(changes)
}).catch(e => {
if (this.nop) {
if (e.status === 404) {
// Ignore 404s which can happen in dry-run as the repo may not exist.
return Promise.resolve(resArray)
} else {
resArray.push(new NopCommand(this.constructor.name, this.repo, null, `error ${e} in ${this.constructor.name} for repo: ${JSON.stringify(this.repo)} entries ${JSON.stringify(this.entries)}`, 'ERROR'))
return Promise.resolve(resArray)
}
} else {
this.logError(`Error ${e} in ${this.constructor.name} for repo: ${JSON.stringify(this.repo)} entries ${JSON.stringify(this.entries)}`)
}
})
}
}
}