Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated according to lint rules #683

Open
wants to merge 6 commits into
base: main-enterprise
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
"standard"
],
"parserOptions": {
"ecmaVersion": 12
"ecmaVersion": 13
},
"rules": {
},
"ignorePatterns": ["test/**/*.js"]
"overrides": [
{
"files": ["test/**/*.js"],
"env": {
"jest": true
}
}
]
}
37 changes: 15 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ const env = require('./lib/env')

let deploymentConfig


module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) => {
let appName = 'safe-settings'
let appSlug = 'safe-settings'
async function syncAllSettings (nop, context, repo = context.repo(), ref) {
try {
Expand Down Expand Up @@ -101,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 @@ -217,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 @@ -227,13 +225,11 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
const installation = installations[0]
const github = await robot.auth(installation.id)
const app = await github.apps.getAuthenticated()
appName = app.data.name
appSlug = app.data.slug
robot.log.debug(`Validated the app is configured properly = \n${JSON.stringify(app.data, null, 2)}`)
}
}


async function syncInstallation () {
robot.log.trace('Fetching installations')
const github = await robot.auth()
Expand Down Expand Up @@ -395,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 @@ -414,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 All @@ -439,12 +435,12 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
} catch (error) {
if (error.status === 404) {
// if the a config file does not exist, create one from the old one
const update = await context.octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
await context.octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
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 @@ -455,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 @@ -663,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() %>\`
<% }) -%>


`
`
Loading