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

feat(extension): also allow .yaml file format #735

Open
wants to merge 11 commits into
base: main-enterprise
Choose a base branch
from
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
> [!NOTE]
> The `suborg` and `repo` level settings directory structure cannot be customized.
>
> Settings files must have a `.yml` extension only. For now, the `.yaml` extension is ignored.


## How it works
Expand Down
16 changes: 10 additions & 6 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ ${this.results.reduce((x, y) => {

// Overlay repo config
// RepoConfigs should be preloaded but checking anyway
const overrideRepoConfig = this.repoConfigs[`${repo.repo}.yml`]?.repository
const overrideRepoConfig = this.repoConfigs[`${repo.repo}.yml`]?.repository || this.repoConfigs[`${repo.repo}.yaml`]?.repository
if (overrideRepoConfig) {
repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, overrideRepoConfig)
}
Expand Down Expand Up @@ -391,8 +391,8 @@ ${this.results.reduce((x, y) => {
childPluginsList(repo) {
const repoName = repo.repo
const subOrgOverrideConfig = this.getSubOrgConfig(repoName)
this.log.debug(`suborg config for ${repoName} is ${JSON.stringify(subOrgOverrideConfig)}`)
const repoOverrideConfig = this.repoConfigs[`${repoName}.yml`] || {}
this.log.debug(`suborg config for ${repoName} is ${JSON.stringify(subOrgOverrideConfig)}`)
const repoOverrideConfig = this.getRepoOverrideConfig(repoName)
const overrideConfig = this.mergeDeep.mergeDeep({}, this.returnRepoSpecificConfigs(this.config), subOrgOverrideConfig, repoOverrideConfig)

this.log.debug(`consolidated config is ${JSON.stringify(overrideConfig)}`)
Expand Down Expand Up @@ -420,6 +420,10 @@ ${this.results.reduce((x, y) => {
return childPlugins
}

getRepoOverrideConfig(repoName) {
return this.repoConfigs[`${repoName}.yml`] || this.repoConfigs[`${repoName}.yaml`] || {}
}

validate(section, baseConfig, overrideConfig) {
const configValidator = this.configvalidators[section]
if (configValidator) {
Expand Down Expand Up @@ -680,7 +684,7 @@ ${this.results.reduce((x, y) => {
// If repo is passed get only its config
// else load all the config
if (repo) {
if (override.name === `${repo.repo}.yml`) {
if (override.name === `${repo.repo}.yml` || override.name === `${repo.repo}.yaml`) {
const data = await this.loadYaml(override.path)
this.log.debug(`data = ${JSON.stringify(data)}`)
repoConfigs[override.name] = data
Expand Down Expand Up @@ -786,7 +790,7 @@ ${this.results.reduce((x, y) => {
}
)) {
delete subOrgConfigs[key]
}
}
}
}
return subOrgConfigs
Expand Down Expand Up @@ -857,7 +861,7 @@ ${this.results.reduce((x, y) => {
if (this.nop) {
//Remove nulls and undefined from the results
const results = res.flat(3).filter(r => r)

this.results = this.results.concat(results)
}
}
Expand Down
97 changes: 69 additions & 28 deletions test/unit/lib/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ describe('Settings Tests', () => {
beforeEach(() => {
const mockOctokit = jest.mocked(Octokit)
const content = Buffer.from(`
suborgrepos:
- new-repo
#- test*
#- secret*
suborgrepos:
- new-repo
#- test*
#- secret*

suborgteams:
- core

suborgproperties:
suborgproperties:
- EDP: true
- do_no_delete: true
teams:
- name: core
permission: bypass

teams:
- name: core
permission: bypass
- name: docss
permission: pull
- name: docs
permission: pull

validator:
pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*'
pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*'

repository:
repository:
# A comma-separated list of topics to set on the repository
topics:
topics:
- frontend
`).toString('base64');
mockOctokit.repos = {
Expand Down Expand Up @@ -192,6 +192,47 @@ repository:
})
}) // restrictedRepos

describe('getRepoOverrideConfig', () => {
describe('repository defined in a file using the .yaml extension', () => {
beforeEach(() => {
stubConfig = {
repoConfigs: {
'repository.yaml': { repository: { name: 'repository', config: 'config1' } }
}
}
})

it('Picks up a repository defined in file using the .yaml extension', () => {
settings = createSettings(stubConfig)
settings.repoConfigs = stubConfig.repoConfigs
const repoConfig = settings.getRepoOverrideConfig('repository')

expect(typeof repoConfig).toBe('object')
expect(repoConfig).not.toBeNull()
expect(Object.keys(repoConfig).length).toBeGreaterThan(0)
})
})

describe('repository defined in a file using the .yml extension', () => {
beforeEach(() => {
stubConfig = {
repoConfigs: {
'repository.yml': { repository: { name: 'repository', config: 'config1' } }
}
}
})

it('Picks up a repository defined in file using the .yml extension', () => {
settings = createSettings(stubConfig)
settings.repoConfigs = stubConfig.repoConfigs
const repoConfig = settings.getRepoOverrideConfig('repository')

expect(typeof repoConfig).toBe('object')
expect(repoConfig).not.toBeNull()
expect(Object.keys(repoConfig).length).toBeGreaterThan(0)
})
})
}) // repoOverrideConfig
describe('loadConfigs', () => {
describe('load suborg configs', () => {
beforeEach(() => {
Expand All @@ -200,29 +241,29 @@ repository:
}
}
subOrgConfig = yaml.load(`
suborgrepos:
- new-repo
suborgproperties:
suborgrepos:
- new-repo

suborgproperties:
- EDP: true
- do_no_delete: true
teams:
- name: core
permission: bypass

teams:
- name: core
permission: bypass
- name: docss
permission: pull
- name: docs
permission: pull

validator:
pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*'
repository:
pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*'

repository:
# A comma-separated list of topics to set on the repository
topics:
topics:
- frontend

`)

})
Expand Down