Skip to content

Commit

Permalink
fix eslint errors, except environments
Browse files Browse the repository at this point in the history
Updated with `npx standard --fix`.

Skipping environments.js and environments.test.js as they had a lot of
errors, handling those in a separate commit.
  • Loading branch information
Gramatus committed Sep 15, 2024
1 parent 955434c commit 027b08b
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 204 deletions.
33 changes: 14 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const env = require('./lib/env')

let deploymentConfig


module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) => {
let appSlug = 'safe-settings'
async function syncAllSettings (nop, context, repo = context.repo(), ref) {
Expand Down Expand Up @@ -100,7 +99,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
const config = Object.assign({}, deploymentConfig, runtimeConfig)
const renameConfig = Object.assign({}, config, rename)
robot.log.debug(`config for ref ${ref} is ${JSON.stringify(config)}`)
return Settings.sync(nop, context, repo, renameConfig, ref )
return Settings.sync(nop, context, repo, renameConfig, ref)
} catch (e) {
if (nop) {
let filename = env.SETTINGS_FILE_PATH
Expand Down Expand Up @@ -216,7 +215,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
robot.log.debug(JSON.stringify(res, null))
}

async function info() {
async function info () {
const github = await robot.auth()
const installations = await github.paginate(
github.apps.listInstallations.endpoint.merge({ per_page: 100 })
Expand All @@ -231,7 +230,6 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
}
}


async function syncInstallation () {
robot.log.trace('Fetching installations')
const github = await robot.auth()
Expand Down Expand Up @@ -393,8 +391,8 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
})

robot.on('repository.renamed', async context => {
if (env.BLOCK_REPO_RENAME_BY_HUMAN!== 'true') {
robot.log.debug(`"env.BLOCK_REPO_RENAME_BY_HUMAN" is 'false' by default. Repo rename is not managed by Safe-settings. Continue with the default behavior.`)
if (env.BLOCK_REPO_RENAME_BY_HUMAN !== 'true') {
robot.log.debug('"env.BLOCK_REPO_RENAME_BY_HUMAN" is \'false\' by default. Repo rename is not managed by Safe-settings. Continue with the default behavior.')
return
}
const { payload } = context
Expand All @@ -412,7 +410,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
const newPath = `.github/repos/${payload.repository.name}.yml`
robot.log.debug(oldPath)
try {
const repofile = await context.octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
const repofile = await context.octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: payload.repository.owner.login,
repo: env.ADMIN_REPO,
path: oldPath,
Expand Down Expand Up @@ -441,8 +439,8 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
owner: payload.repository.owner.login,
repo: env.ADMIN_REPO,
path: newPath,
name: `${payload.repository.name}.yml`,
content: content,
name: `${payload.repository.name}.yml`,
content,
message: `Repo Renamed and safe-settings renamed the file from ${payload.changes.repository.name.from} to ${payload.repository.name}`,
sha: repofile.data.sha,
headers: {
Expand All @@ -453,26 +451,23 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
} else {
robot.log.error(error)
}
}

}
} catch (error) {
if (error.status === 404) {
//nop
} else {
// nop
} else {
robot.log.error(error)
}
}
return
}
} else {
robot.log.debug('Repository Edited by a Human')
// Create a repository config to reset the name back to the previous name
const rename = {repository: { name: payload.changes.repository.name.from, oldname: payload.repository.name}}
const repo = {repo: payload.changes.repository.name.from, owner: payload.repository.owner.login}
const rename = { repository: { name: payload.changes.repository.name.from, oldname: payload.repository.name } }
const repo = { repo: payload.changes.repository.name.from, owner: payload.repository.owner.login }
return renameSync(false, context, repo, rename)
}
})


robot.on('check_suite.requested', async context => {
const { payload } = context
const { repository } = payload
Expand Down Expand Up @@ -661,7 +656,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
syncInstallation()
})
}

// Get info about the app
info()

Expand Down
2 changes: 1 addition & 1 deletion lib/commentmessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ module.exports = `* Run on: \` <%= new Date() %> \`
<% }) %>
<% }) %>
<% } %>`
<% } %>`
69 changes: 35 additions & 34 deletions lib/deploymentConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,47 @@ const env = require('./env')
* The settings are loaded from the deployment-settings.yml file during initialization and stored as static properties.
*/
class DeploymentConfig {
//static config
static configvalidators = {}
static overridevalidators = {}
// static config
static configvalidators = {}
static overridevalidators = {}

static {
const deploymentConfigPath = process.env.DEPLOYMENT_CONFIG_FILE ? process.env.DEPLOYMENT_CONFIG_FILE : 'deployment-settings.yml'
if (fs.existsSync(deploymentConfigPath)) {
this.config = yaml.load(fs.readFileSync(deploymentConfigPath))
} else {
this.config = { restrictedRepos: ['admin', '.github', 'safe-settings'] }
}

const overridevalidators = this.config.overridevalidators
if (this.isIterable(overridevalidators)) {
for (const validator of overridevalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
}
}
const configvalidators = this.config.configvalidators
if (this.isIterable(configvalidators)) {
for (const validator of configvalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'githubContext', validator.script)
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
}
}
static {
const deploymentConfigPath = process.env.DEPLOYMENT_CONFIG_FILE ? process.env.DEPLOYMENT_CONFIG_FILE : 'deployment-settings.yml'
if (fs.existsSync(deploymentConfigPath)) {
this.config = yaml.load(fs.readFileSync(deploymentConfigPath))
} else {
this.config = { restrictedRepos: ['admin', '.github', 'safe-settings'] }
}

static isIterable (obj) {
// checks for null and undefined
if (obj == null) {
return false
}
return typeof obj[Symbol.iterator] === 'function'
const overridevalidators = this.config.overridevalidators
if (this.isIterable(overridevalidators)) {
for (const validator of overridevalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
}
}
const configvalidators = this.config.configvalidators
if (this.isIterable(configvalidators)) {
for (const validator of configvalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'githubContext', validator.script)
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
}
}
}

constructor (nop, context, repo, config, ref, suborg) {
static isIterable (obj) {
// checks for null and undefined
if (obj == null) {
return false
}
return typeof obj[Symbol.iterator] === 'function'
}

// eslint-disable-next-line no-useless-constructor
constructor (nop, context, repo, config, ref, suborg) {
}
}
DeploymentConfig.FILE_NAME = `${env.CONFIG_PATH}/settings.yml`

Expand Down
2 changes: 1 addition & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports = `Run on: \`<%= new Date().toISOString() %>\`
<% }) -%>
`
`
22 changes: 11 additions & 11 deletions lib/plugins/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ module.exports = class Repository extends ErrorStash {
const topicChanges = mergeDeep.compareDeep({ entries: resp.data.topics }, { entries: this.topics })
// hasTopicChanges = topicChanges.additions.length > 0 || topicChanges.modifications.length > 0

//const results = JSON.stringify(changes, null, 2)
// const results = JSON.stringify(changes, null, 2)
const results = { msg: `${this.constructor.name} settings changes`, additions: changes.additions, modifications: changes.modifications, deletions: changes.deletions }

this.log.debug(`Result of comparing repo for changes = ${results}`)

//const topicResults = JSON.stringify(topicChanges, null, 2)
// const topicResults = JSON.stringify(topicChanges, null, 2)
const topicResults = { msg: `${this.constructor.name} settings changes for topics`, additions: topicChanges.additions, modifications: topicChanges.modifications, deletions: topicChanges.deletions }
this.log.debug(`Result of comparing topics for changes source ${JSON.stringify(resp.data.topics)} target ${JSON.stringify(this.topics)} = ${topicResults}`)

Expand All @@ -91,7 +91,7 @@ module.exports = class Repository extends ErrorStash {
const promises = []
if (topicChanges.hasChanges) {
promises.push(this.updatetopics(resp.data, resArray))
}
}
if (changes.hasChanges) {
this.log.debug('There are repo changes')
let updateDefaultBranchPromise = Promise.resolve()
Expand Down Expand Up @@ -189,7 +189,7 @@ module.exports = class Repository extends ErrorStash {
})
}

renameBranch(oldname, newname, resArray) {
renameBranch (oldname, newname, resArray) {
this.log.error(`Branch ${newname} does not exist. So renaming the current default branch ${oldname} to ${newname}`)
const parms = {
owner: this.settings.owner,
Expand Down Expand Up @@ -227,12 +227,12 @@ module.exports = class Repository extends ErrorStash {
if (this.topics) {
// if (repoData.data?.topics.length !== this.topics.length ||
// !repoData.data?.topics.every(t => this.topics.includes(t))) {
this.log.debug(`Updating repo with topics ${this.topics.join(',')}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.replaceAllTopics.endpoint(parms), 'Update Topics')))
return Promise.resolve(resArray)
}
return this.github.repos.replaceAllTopics(parms)
this.log.debug(`Updating repo with topics ${this.topics.join(',')}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.replaceAllTopics.endpoint(parms), 'Update Topics')))
return Promise.resolve(resArray)
}
return this.github.repos.replaceAllTopics(parms)
// } else {
// this.log.debug(`no need to update topics for ${repoData.data.name}`)
// if (this.nop) {
Expand Down Expand Up @@ -277,7 +277,7 @@ module.exports = class Repository extends ErrorStash {
} else {
this.log.debug(`no need to update security for ${repoData.name}`)
if (this.nop) {
//resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update security for ${repoData.name}`)))
// resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update security for ${repoData.name}`)))
return Promise.resolve([])
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,14 +798,13 @@ ${this.results.reduce((x, y) => {
}

async getReposForCustomProperty (customPropertyTuple) {
const name=Object.keys(customPropertyTuple)[0]
const name = Object.keys(customPropertyTuple)[0]
let q = `props.${name}:${customPropertyTuple[name]}`
q = encodeURIComponent(q)
const options = this.github.request.endpoint((`/orgs/${this.repo.owner}/properties/values?repository_query=${q}`))
return this.github.paginate(options)
}


isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item))
}
Expand All @@ -819,7 +818,7 @@ ${this.results.reduce((x, y) => {
}
}

function prettify(obj) {
function prettify (obj) {
if (obj === null || obj === undefined) {
return ''
}
Expand Down
3 changes: 1 addition & 2 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe.skip('plugin', () => {
}
}

app = new Probot({ secret: "abcdef", Octokit })
app = new Probot({ secret: 'abcdef', Octokit })
github = {
repos: {
getContents: jest.fn(() => Promise.resolve({ data: { content: '' } }))
Expand Down Expand Up @@ -151,5 +151,4 @@ describe.skip('plugin', () => {
expect(sync).toHaveBeenCalled()
})
})

})
5 changes: 0 additions & 5 deletions test/unit/lib/env.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-undef */
describe('env', () => {

describe('load default values without override', () => {

const envTest = require('../../../lib/env')

it('loads default ADMIN_REPO if not passed', () => {
Expand All @@ -29,11 +27,9 @@ describe('env', () => {
const CREATE_PR_COMMENT = envTest.CREATE_PR_COMMENT
expect(CREATE_PR_COMMENT).toEqual('true')
})

})

describe('load override values', () => {

beforeAll(() => {
jest.resetModules()
process.env.ADMIN_REPO = '.github'
Expand All @@ -57,5 +53,4 @@ describe('env', () => {
expect(CREATE_PR_COMMENT).toEqual('false')
})
})

})
Loading

0 comments on commit 027b08b

Please sign in to comment.