Skip to content

Commit

Permalink
add outer try-catch fix issue #545
Browse files Browse the repository at this point in the history
  • Loading branch information
decyjphr committed Oct 31, 2023
1 parent e584dbc commit 8ec4892
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
return
}

if (check_run.status === 'completed') {
robot.log.debug(' Checkrun created as completed, returning')
return
}

const adminRepo = repository.name === env.ADMIN_REPO
robot.log.debug(`Is Admin repo event ${adminRepo}`)
if (!adminRepo) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/errorStash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = class ErrorStash {
this.errors = errors
}

async logError (msg) {
logError (msg) {
this.log.error(msg)
this.errors.push({
owner: this.repo.owner,
Expand Down
9 changes: 6 additions & 3 deletions lib/plugins/rulesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const version = {
'X-GitHub-Api-Version': '2022-11-28'
}
module.exports = class Rulesets extends Diffable {
constructor (nop, github, repo, entries, log, scope, errors) {
constructor (nop, github, repo, entries, log, errors, scope) {
super(nop, github, repo, entries, log, errors)
this.github = github
this.repo = repo
Expand Down Expand Up @@ -152,7 +152,7 @@ module.exports = class Rulesets extends Diffable {
if (this.scope === 'org') {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.fetch.endpoint('DELETE /orgs/{org}/rulesets/{id}', parms), 'Delete Ruleset')
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('DELETE /orgs/{org}/rulesets/{id}', parms), 'Delete Ruleset')
])
}
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
Expand All @@ -165,14 +165,17 @@ module.exports = class Rulesets extends Diffable {
} else {
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.fetch.endpoint('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms), 'Delete Ruleset')
new NopCommand(this.constructor.name, this.repo, this.github.request.endpoint('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms), 'Delete Ruleset')
])
}
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
return this.github.request('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms).then(res => {
this.log(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
return res
}).catch(e => {
if (e.status === 404) {
return
}
return this.handleError(e)
})
}
Expand Down
64 changes: 44 additions & 20 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ const SCOPE = { ORG: 'org', REPO: 'repo' } // Determine if the setting is a org
class Settings {
static async syncAll (nop, context, repo, config, ref) {
const settings = new Settings(nop, context, repo, config, ref)
await settings.loadConfigs()
// settings.repoConfigs = await settings.getRepoConfigs()
await settings.updateOrg()
await settings.updateAll()
await settings.handleResults()
try {
await settings.loadConfigs()
// settings.repoConfigs = await settings.getRepoConfigs()
await settings.updateOrg()
await settings.updateAll()
await settings.handleResults()
} catch (error) {
settings.logError(error.message)
await settings.handleResults()
}
}

static async syncSubOrgs (nop, context, suborg, repo, config, ref) {
const settings = new Settings(nop, context, repo, config, ref, suborg)
await settings.loadConfigs()
await settings.updateAll()
await settings.handleResults()
try {
await settings.loadConfigs()
await settings.updateAll()
await settings.handleResults()
} catch (error) {
settings.logError(error.message)
await settings.handleResults()
}
}

static async sync (nop, context, repo, config, ref) {
const settings = new Settings(nop, context, repo, config, ref)
await settings.loadConfigs(repo)
if (settings.isRestricted(repo.repo)) {
return
try {
await settings.loadConfigs(repo)
if (settings.isRestricted(repo.repo)) {
return
}
await settings.updateRepos(repo)
await settings.handleResults()
} catch (error) {
settings.logError(error.message)
await settings.handleResults()
}
await settings.updateRepos(repo)
await settings.handleResults()
}

static async handleError (nop, context, repo, config, ref, nopcommand) {
Expand Down Expand Up @@ -124,6 +139,16 @@ class Settings {
})
}

logError (msg) {
this.log.error(msg)
this.errors.push({
owner: this.repo.owner,
repo: this.repo.repo,
msg,
plugin: this.constructor.name
})
}

async handleResults () {
const { payload } = this.context

Expand Down Expand Up @@ -163,7 +188,7 @@ class Settings {
stats.errors[res.repo] = []
}
stats.errors[res.repo].push(res.action)
} else if (!(res.action.additions === null && res.action.deletions === null && res.action.modifications === null)) {
} else if (!(res.action?.additions === null && res.action?.deletions === null && res.action?.modifications === null)) {
if (!stats.changes[res.plugin]) {
stats.changes[res.plugin] = {}
}
Expand Down Expand Up @@ -201,9 +226,6 @@ ${this.results.reduce((x, y) => {
if (!y) {
return x
}
// if (y.endpoint) {
// return `${x}`
// // } else
if (y.type === 'ERROR') {
error = true
return `${x}
Expand Down Expand Up @@ -256,7 +278,7 @@ ${this.results.reduce((x, y) => {
const rulesetsConfig = this.config.rulesets
if (rulesetsConfig) {
const RulesetsPlugin = Settings.PLUGINS.rulesets
return new RulesetsPlugin(this.nop, this.github, this.repo, rulesetsConfig, this.log, SCOPE.ORG, this.errors).sync().then(res => {
return new RulesetsPlugin(this.nop, this.github, this.repo, rulesetsConfig, this.log, this.errors, SCOPE.ORG).sync().then(res => {
this.appendToResults(res)
})
}
Expand Down Expand Up @@ -772,6 +794,9 @@ ${this.results.reduce((x, y) => {
}

function prettify(obj) {
if (obj === null || obj === undefined) {
return ''
}
return JSON.stringify(obj, null, 2).replaceAll('\n', '<br>').replaceAll(' ', '&nbsp;')
}

Expand All @@ -786,8 +811,7 @@ Settings.PLUGINS = {
branches: require('./plugins/branches'),
autolinks: require('./plugins/autolinks'),
validator: require('./plugins/validator'),
rulesets: require('./plugins/rulesets'),
environments: require('./plugins/environments')
rulesets: require('./plugins/rulesets')
}

module.exports = Settings
1 change: 1 addition & 0 deletions test/unit/lib/mergeDeep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('MergeDeep Test', () => {
it('CompareDeep extensive test', () => {
const target = YAML.load(`
repository:
name: test
# A short description of the repository that will show up on GitHub
description: description of the repos
Expand Down

0 comments on commit 8ec4892

Please sign in to comment.