-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: repository custom properties plugin (#626)
* feat: repository custom properties plugin * docs: custom properties
- Loading branch information
1 parent
b6afb1a
commit 678b02e
Showing
6 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Diffable = require('./diffable') | ||
|
||
module.exports = class CustomProperties extends Diffable { | ||
constructor (...args) { | ||
super(...args) | ||
|
||
if (this.entries) { | ||
// Force all names to lowercase to avoid comparison issues. | ||
this.entries.forEach(prop => { | ||
prop.name = prop.name.toLowerCase() | ||
}) | ||
} | ||
} | ||
|
||
async find () { | ||
const data = await this.github.request('GET /repos/:org/:repo/properties/values', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo | ||
}) | ||
|
||
const properties = data.data.map(d => { return { name: d.property_name, value: d.value } }) | ||
return properties | ||
} | ||
|
||
comparator (existing, attrs) { | ||
return existing.name === attrs.name | ||
} | ||
|
||
changed (existing, attrs) { | ||
return attrs.value !== existing.value | ||
} | ||
|
||
async update (existing, attrs) { | ||
await this.github.request('PATCH /repos/:org/:repo/properties/values', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo, | ||
properties: [{ | ||
property_name: attrs.name, | ||
value: attrs.value | ||
}] | ||
}) | ||
} | ||
|
||
async add (attrs) { | ||
await this.github.request('PATCH /repos/:org/:repo/properties/values', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo, | ||
properties: [{ | ||
property_name: attrs.name, | ||
value: attrs.value | ||
}] | ||
}) | ||
} | ||
|
||
async remove (existing) { | ||
await this.github.request('PATCH /repos/:org/:repo/properties/values', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo, | ||
properties: [ | ||
{ | ||
property_name: existing.name, | ||
value: null | ||
} | ||
] | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const CustomProperties = require('../../../../lib/plugins/custom_properties') | ||
|
||
describe('CustomProperties', () => { | ||
let github | ||
const repo = { owner: 'owner', repo: 'repo' } | ||
let log | ||
|
||
function configure (config) { | ||
const nop = false; | ||
const errors = [] | ||
return new CustomProperties(nop, github, { owner: 'bkeepers', repo: 'test' }, config, log, errors) | ||
} | ||
|
||
beforeEach(() => { | ||
github = { | ||
request: jest.fn() | ||
// .mockResolvedValue({ | ||
// data: [ | ||
// { property_name: 'test', value: 'test' } | ||
// ] | ||
// }) | ||
} | ||
log = { debug: jest.fn(), error: console.error } | ||
}) | ||
|
||
describe('sync', () => { | ||
it('syncs custom properties', async () => { | ||
const plugin = configure([ | ||
{ name: 'test', value: 'test' } | ||
]) | ||
|
||
github.request.mockResolvedValue({ | ||
data: [ | ||
{ property_name: 'test', value: 'test' } | ||
] | ||
}) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test' | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('sync', () => { | ||
it('add custom properties', async () => { | ||
const plugin = configure([ | ||
{ name: 'test', value: 'test' } | ||
]) | ||
|
||
github.request.mockResolvedValue({ | ||
data: [] | ||
}) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test' | ||
}) | ||
expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test', | ||
properties: [ | ||
{ | ||
property_name: 'test', | ||
value: 'test' | ||
} | ||
] | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('sync', () => { | ||
it('remove custom properties', async () => { | ||
const plugin = configure([]) | ||
|
||
github.request.mockResolvedValue({ | ||
data: [{ property_name: 'test', value: 'test' }] | ||
}) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test' | ||
}) | ||
expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test', | ||
properties: [ | ||
{ | ||
property_name: 'test', | ||
value: null | ||
} | ||
] | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('sync', () => { | ||
it('update custom properties', async () => { | ||
const plugin = configure([ | ||
{ name: 'test', value: 'foobar' } | ||
]) | ||
|
||
github.request.mockResolvedValue({ | ||
data: [{ property_name: 'test', value: 'test' }] | ||
}) | ||
|
||
return plugin.sync().then(() => { | ||
expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test' | ||
}) | ||
expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { | ||
org: 'bkeepers', | ||
repo: 'test', | ||
properties: [ | ||
{ | ||
property_name: 'test', | ||
value: 'foobar' | ||
} | ||
] | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |