Skip to content

Commit fdd5430

Browse files
committed
First implementation of comment compression
2 parents abffac0 + d86fa03 commit fdd5430

9 files changed

Lines changed: 1917 additions & 160 deletions

File tree

index.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,29 @@ let deploymentConfig
1919

2020
module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) => {
2121
let appSlug = 'safe-settings'
22-
async function syncAllSettings (nop, context, repo = context.repo(), ref) {
22+
async function syncAllSettings (nop, context, repo = context.repo(), ref, baseRef, changedFiles = {}) {
2323
try {
2424
deploymentConfig = await loadYamlFileSystem()
2525
robot.log.debug(`deploymentConfig is ${JSON.stringify(deploymentConfig)}`)
2626
const configManager = new ConfigManager(context, ref)
2727
const runtimeConfig = await configManager.loadGlobalSettingsYaml()
2828
const config = Object.assign({}, deploymentConfig, runtimeConfig)
2929
robot.log.debug(`config for ref ${ref} is ${JSON.stringify(config)}`)
30+
31+
// Load base branch config for NOP filtering (only show PR-introduced changes)
32+
let baseConfig = null
33+
if (nop && baseRef) {
34+
try {
35+
const baseConfigManager = new ConfigManager(context, baseRef)
36+
const baseRuntimeConfig = await baseConfigManager.loadGlobalSettingsYaml()
37+
baseConfig = Object.assign({}, deploymentConfig, baseRuntimeConfig)
38+
} catch (e) {
39+
robot.log.debug(`Could not load base config for NOP filtering: ${e.message}`)
40+
}
41+
}
42+
3043
if (ref) {
31-
return Settings.syncAll(nop, context, repo, config, ref)
44+
return Settings.syncAll(nop, context, repo, config, ref, baseConfig, changedFiles)
3245
} else {
3346
return Settings.syncAll(nop, context, repo, config)
3447
}
@@ -73,15 +86,28 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
7386
}
7487
}
7588

76-
async function syncSelectedSettings (nop, context, repos, subOrgs, ref) {
89+
async function syncSelectedSettings (nop, context, repos, subOrgs, ref, baseRef) {
7790
try {
7891
deploymentConfig = await loadYamlFileSystem()
7992
robot.log.debug(`deploymentConfig is ${JSON.stringify(deploymentConfig)}`)
8093
const configManager = new ConfigManager(context, ref)
8194
const runtimeConfig = await configManager.loadGlobalSettingsYaml()
8295
const config = Object.assign({}, deploymentConfig, runtimeConfig)
8396
robot.log.debug(`config for ref ${ref} is ${JSON.stringify(config)}`)
84-
return Settings.syncSelectedRepos(nop, context, repos, subOrgs, config, ref)
97+
98+
// Load base branch config for NOP filtering
99+
let baseConfig = null
100+
if (nop && baseRef) {
101+
try {
102+
const baseConfigManager = new ConfigManager(context, baseRef)
103+
const baseRuntimeConfig = await baseConfigManager.loadGlobalSettingsYaml()
104+
baseConfig = Object.assign({}, deploymentConfig, baseRuntimeConfig)
105+
} catch (e) {
106+
robot.log.debug(`Could not load base config for NOP filtering: ${e.message}`)
107+
}
108+
}
109+
110+
return Settings.syncSelectedRepos(nop, context, repos, subOrgs, config, ref, baseConfig)
85111
} catch (e) {
86112
if (nop) {
87113
let filename = env.SETTINGS_FILE_PATH
@@ -585,17 +611,21 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
585611
const files = changes.data.map(f => { return f.filename })
586612

587613
const settingsModified = files.includes(Settings.FILE_PATH)
614+
const repoChanges = getChangedRepoConfigName(files, context.repo().owner)
615+
const subOrgChanges = getChangedSubOrgConfigName(files)
588616

589617
if (settingsModified) {
590618
robot.log.debug(`Changes in '${Settings.FILE_PATH}' detected, doing a full synch...`)
591-
return syncAllSettings(true, context, context.repo(), pull_request.head.ref)
619+
const baseRef = pull_request.base.ref || repository.default_branch
620+
return syncAllSettings(true, context, context.repo(), pull_request.head.ref, baseRef, {
621+
repos: repoChanges,
622+
subOrgs: subOrgChanges
623+
})
592624
}
593625

594-
const repoChanges = getChangedRepoConfigName(files, context.repo().owner)
595-
const subOrgChanges = getChangedSubOrgConfigName(files)
596-
597626
if (repoChanges.length > 0 || subOrgChanges.length > 0) {
598-
return syncSelectedSettings(true, context, repoChanges, subOrgChanges, pull_request.head.ref)
627+
const baseRef = pull_request.base.ref || repository.default_branch
628+
return syncSelectedSettings(true, context, repoChanges, subOrgChanges, pull_request.head.ref, baseRef)
599629
}
600630

601631
// if no safe-settings changes detected, send a success to the check run

lib/commentmessage.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
module.exports = `* Run on: \` <%= new Date() %> \`
1+
module.exports = `* Run on: \`<%= new Date() %>\`
22
3-
* Number of repos that were considered: \`<%= Object.keys(it.reposProcessed).length %> \`
3+
* Number of repos that were considered: \`<%= Object.keys(it.reposProcessed).length %>\`
4+
* Number of repos affected: \`<%= it.reposAffected || 0 %>\`
45
56
### Breakdown of changes
6-
| Repo <% Object.keys(it.changes).forEach(plugin => { %> | <%= plugin %> settings <% }) %> |
7-
| -- <% Object.keys(it.changes).forEach(plugin => { -%> | -- <% }) %>
8-
|
9-
<% Object.keys(it.reposProcessed).forEach( repo => { -%>
10-
| <%= repo -%>
11-
<%- Object.keys(it.changes).forEach(plugin => { -%>
12-
<%_ if (it.changes[plugin][repo]) { -%> | :hand: <% } else { %> | :grey_exclamation: <% } -%>
13-
<%_ }) -%> |
14-
<% }) -%>
15-
16-
:hand: -> Changes to be applied to the GitHub repository.
17-
:grey_exclamation: -> nothing to be changed in that particular GitHub repository.
7+
<% if (!it.changeSections || it.changeSections.length === 0) { %>
188
19-
### Breakdown of errors
9+
No changes to apply.
10+
<% } else { %>
2011
12+
<%~ it.checkRunDetails %>
13+
<% } %>
14+
15+
### Breakdown of errors
2116
<% if (Object.keys(it.errors).length === 0) { %>
17+
2218
\`None\`
2319
<% } else { %>
24-
<% Object.keys(it.errors).forEach(repo => { %>
25-
<%_= repo %>:
26-
<% it.errors[repo].forEach(plugin => { %>
27-
* <%= plugin.msg %>
28-
<% }) %>
2920
30-
<% }) %>
21+
<details>
22+
<summary>Errors by repo</summary>
23+
24+
<% Object.keys(it.errors).forEach(function(repo) { %>
25+
**<%= repo %>**
26+
27+
<% it.errors[repo].forEach(function(err) { %>* <%= err.msg %>
28+
<% }) %>
29+
<% }) %>
30+
31+
</details>
3132
<% } %>`

lib/plugins/custom_properties.js

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,7 @@ module.exports = class CustomProperties extends Diffable {
1212

1313
// Force all names to lowercase to avoid comparison issues.
1414
normalizeEntries () {
15-
this.entries = this.entries.reduce((normalizedEntries, entry) => {
16-
if (!entry || typeof entry !== 'object') {
17-
return normalizedEntries
18-
}
19-
20-
const entryName = entry.name || entry.property_name
21-
22-
if (typeof entryName !== 'string') {
23-
return normalizedEntries
24-
}
25-
26-
normalizedEntries.push({
27-
name: entryName.toLowerCase(),
28-
value: entry.value
29-
})
30-
31-
return normalizedEntries
32-
}, [])
15+
this.entries = this.normalize(this.entries)
3316
}
3417

3518
async find () {
@@ -52,24 +35,16 @@ module.exports = class CustomProperties extends Diffable {
5235

5336
// Force all names to lowercase to avoid comparison issues.
5437
normalize (properties) {
55-
return properties.reduce((normalizedProperties, property) => {
56-
if (!property || typeof property !== 'object') {
57-
return normalizedProperties
58-
}
59-
60-
const propertyName = property.property_name || property.name
61-
62-
if (typeof propertyName !== 'string') {
63-
return normalizedProperties
64-
}
65-
66-
normalizedProperties.push({
67-
name: propertyName.toLowerCase(),
68-
value: property.value
69-
})
70-
71-
return normalizedProperties
72-
}, [])
38+
return properties
39+
.map(({ property_name: propertyName, name, value }) => ({
40+
name: propertyName ?? name,
41+
value
42+
}))
43+
.filter(({ name }) => name != null)
44+
.map(({ name, value }) => ({
45+
name: name.toLowerCase(),
46+
value
47+
}))
7348
}
7449

7550
comparator (existing, attrs) {

0 commit comments

Comments
 (0)