Skip to content

Commit

Permalink
fix: Updating autolinks with is_alphanumeric and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevoland committed Feb 14, 2024
1 parent 74bd046 commit a67b8c7
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/plugins/autolinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module.exports = class Autolinks extends Diffable {
}

comparator (existing, attr) {
return existing.key_prefix === attr.key_prefix &&
existing.url_template === attr.url_template
return existing.key_prefix === attr.key_prefix
}

changed (existing, attr) {
Expand All @@ -24,9 +23,7 @@ module.exports = class Autolinks extends Diffable {
const isAlphaNumericMatch = attr.is_alphanumeric === undefined
? existing.is_alphanumeric // === true, the default
: attr.is_alphanumeric === existing.is_alphanumeric
return existing.key_prefix === attr.key_prefix &&
existing.url_template !== attr.url_template &&
!isAlphaNumericMatch
return existing.url_template !== attr.url_template || !isAlphaNumericMatch
}

async update (existing, attr) {
Expand Down
147 changes: 147 additions & 0 deletions test/unit/lib/plugins/autolinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const Autolinks = require('../../../../lib/plugins/autolinks')

describe('Autolinks', () => {
const repo = { owner: 'owner', repo: 'repo' }
let github

function configure (config) {
const log = { debug: jest.fn(), error: console.error }
const nop = false
const errors = []
return new Autolinks(nop, github, repo, config, log, errors)
}

beforeEach(() => {
github = {
repos: {
listAutolinks: jest.fn().mockResolvedValue([]),
createAutolink: jest.fn().mockResolvedValue(),
deleteAutolink: jest.fn().mockResolvedValue(),
}
}
})

describe('sync', () => {
it('syncs autolinks', () => {
const plugin = configure([
{ key_prefix: 'ADD-', url_template: 'https://test/<num>' },
{ key_prefix: 'SAME-', url_template: 'https://test/<num>' },
{ key_prefix: 'NEW_URL-', url_template: 'https://new-url/<num>' },
{ key_prefix: 'SAME_ALPHA-UNDEFINED-', url_template: 'https://test/<num>' },
{ key_prefix: 'SAME_ALPHA-FALSE-', url_template: 'https://test/<num>', is_alphanumeric: false },
{ key_prefix: 'SAME_ALPHA-TRUE-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ key_prefix: 'NEW_ALPHA-UNDEFINED-', url_template: 'https://test/<num>' },
{ key_prefix: 'NEW_ALPHA-FALSE-', url_template: 'https://test/<num>', is_alphanumeric: false },
{ key_prefix: 'NEW_ALPHA-TRUE-', url_template: 'https://test/<num>', is_alphanumeric: true },
])

github.repos.listAutolinks.mockResolvedValueOnce({
data: [
{ id: '1', key_prefix: 'SAME-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ id: '2', key_prefix: 'REMOVE-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ id: '3', key_prefix: 'NEW_URL-', url_template: 'https://current-url/<num>', is_alphanumeric: true },
{ id: '4', key_prefix: 'SAME_ALPHA-UNDEFINED-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ id: '5', key_prefix: 'SAME_ALPHA-FALSE-', url_template: 'https://test/<num>', is_alphanumeric: false },
{ id: '6', key_prefix: 'SAME_ALPHA-TRUE-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ id: '7', key_prefix: 'NEW_ALPHA-UNDEFINED-', url_template: 'https://test/<num>', is_alphanumeric: false },
{ id: '8', key_prefix: 'NEW_ALPHA-FALSE-', url_template: 'https://test/<num>', is_alphanumeric: true },
{ id: '9', key_prefix: 'NEW_ALPHA-TRUE-', url_template: 'https://test/<num>', is_alphanumeric: false },
]
})

return plugin.sync().then(() => {
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'ADD-',
url_template: 'https://test/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: '2',
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: '3',
...repo
})
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'NEW_URL-',
url_template: 'https://new-url/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({
autolink_id: '4',
...repo
})
expect(github.repos.createAutolink).not.toHaveBeenCalledWith({
key_prefix: 'SAME_ALPHA-UNDEFINED-',
url_template: 'https://test/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({
autolink_id: '5',
...repo
})
expect(github.repos.createAutolink).not.toHaveBeenCalledWith({
key_prefix: 'SAME_ALPHA-FALSE-',
url_template: 'https://test/<num>',
is_alphanumeric: false,
...repo
})

expect(github.repos.deleteAutolink).not.toHaveBeenCalledWith({
autolink_id: '6',
...repo
})
expect(github.repos.createAutolink).not.toHaveBeenCalledWith({
key_prefix: 'SAME_ALPHA-TRUE-',
url_template: 'https://test/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: '7',
...repo
})
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'NEW_ALPHA-UNDEFINED-',
url_template: 'https://test/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: '8',
...repo
})
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'NEW_ALPHA-FALSE-',
url_template: 'https://test/<num>',
is_alphanumeric: false,
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: '9',
...repo
})
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'NEW_ALPHA-TRUE-',
url_template: 'https://test/<num>',
is_alphanumeric: true,
...repo
})

expect(github.repos.deleteAutolink).toHaveBeenCalledTimes(5)
expect(github.repos.createAutolink).toHaveBeenCalledTimes(5)
})
})
})
})

0 comments on commit a67b8c7

Please sign in to comment.