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

Added autolinks #545

Open
wants to merge 3 commits into
base: master
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ repository:
# vulnerability alerts.
enable_vulnerability_alerts: true

# Autolinks
autolinks:
- key_prefix: ASDF-
url_template: https://jira.company.com/browse/ASDF-<num>
- key_prefix: BOLIGRAFO-
url_template: https://jira.company.com/browse/BOLIGRAFO-<num>

# Labels: define labels for Issues and Pull Requests
labels:
- name: bug
Expand Down
33 changes: 33 additions & 0 deletions lib/plugins/autolinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Diffable = require('./diffable')
const previewHeaders = { accept: 'application/vnd.github.v3+json' }

module.exports = class Autolinks extends Diffable {
find () {
const options = this.github.repos.listAutolinks.endpoint.merge(this.wrapAttrs({ per_page: 100 }))
return this.github.paginate(options)
}

comparator (existing, attrs) {
return existing.key_prefix === attrs.key_prefix
}

changed (existing, attrs) {
return existing.url_template !== attrs.url_template
}

update (existing, attrs) {
return this.add(attrs)
}

add (attrs) {
return this.github.repos.createAutolink(this.wrapAttrs(attrs))
}

remove (existing) {
return this.github.repos.deleteAutolink(this.wrapAttrs({ autolink_id: existing.id }))
}

wrapAttrs (attrs) {
return Object.assign({}, attrs, this.repo, { headers: previewHeaders })
}
}
1 change: 1 addition & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Settings.FILE_NAME = '.github/settings.yml'

Settings.PLUGINS = {
repository: require('./plugins/repository'),
autolinks: require('./plugins/autolinks'),
labels: require('./plugins/labels'),
collaborators: require('./plugins/collaborators'),
teams: require('./plugins/teams'),
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/autolinks-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repository:
name: bar
delete_branch_on_merge: true
is_template: true

autolinks:
- key_prefix: ASDF-
url_template: https://jira.company.com/browse/ASDF-<num>

- key_prefix: BOLIGRAFO-
url_template: https://jira.company.com/browse/BOLIGRAFO-<num>
61 changes: 61 additions & 0 deletions test/integration/plugins/autolinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const path = require('path')
const fs = require('fs')
const { CREATED, NO_CONTENT, OK } = require('http-status-codes')
const settings = require('../../../lib/settings')
const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')

describe('autolinks plugin', function () {
let probot, githubScope

beforeEach(() => {
githubScope = initializeNock()
probot = loadInstance()
})

afterEach(() => {
teardownNock(githubScope)
})

it('syncs autolinks', async () => {
const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'autolinks-config.yml')
const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
const config = configFile.toString()
githubScope
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${encodeURIComponent(settings.FILE_NAME)}`)
.reply(OK, config)
githubScope
.patch(`/repos/${repository.owner.name}/${repository.name}`)
.reply(200)
githubScope
.get(`/repos/${repository.owner.name}/${repository.name}/autolinks?per_page=100`)
.reply(
OK,
[
{
id: 1,
key_prefix: 'ASDF-',
url_template: 'https://jira.company.com/browse/ASDF-<num>'
},
{
id: 2,
key_prefix: 'TEST-',
url_template: 'https://jira.company.com/browse/TEST-<num>'
}
]
)
githubScope
.post(`/repos/${repository.owner.name}/${repository.name}/autolinks`, body => {
expect(body).toMatchObject({
key_prefix: 'BOLIGRAFO-',
url_template: 'https://jira.company.com/browse/BOLIGRAFO-<num>'
})
return true
})
.reply(CREATED)
githubScope
.delete(`/repos/${repository.owner.name}/${repository.name}/autolinks/2`)
.reply(NO_CONTENT)

await probot.receive(buildTriggerEvent())
})
})
73 changes: 73 additions & 0 deletions test/unit/lib/plugins/autolinks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const Autolinks = require('../../../../lib/plugins/autolinks')

describe('Autolinks', () => {
let github

function configure (config) {
return new Autolinks(github, { owner: 'bkeepers', repo: 'test' }, config)
}

beforeEach(() => {
github = {
paginate: jest.fn().mockImplementation(() => Promise.resolve()),
repos: {
listAutolinks: {
endpoint: {
merge: jest.fn().mockImplementation(() => {})
}
},
deleteAutolink: jest.fn().mockImplementation(() => Promise.resolve()),
createAutolink: jest.fn().mockImplementation(() => Promise.resolve())
}
}
})

describe('sync', () => {
it('syncs autolinks', () => {
github.paginate.mockReturnValueOnce(Promise.resolve([
{
id: 1,
key_prefix: 'ASDF-',
url_template: 'https://jira.company.com/browse/ASDF-<num>'
},
{
id: 2,
key_prefix: 'TEST-',
url_template: 'https://jira.company.com/browse/TEST-<num>'
}
]))

const plugin = configure([
{
key_prefix: 'ASDF-',
url_template: 'https://jira.company.com/browse/ASDF-<num>'
},
{
key_prefix: 'BOLIGRAFO-',
url_template: 'https://jira.company.com/browse/BOLIGRAFO-<num>'
}
])

return plugin.sync().then(() => {
expect(github.repos.createAutolink).toHaveBeenCalledWith({
key_prefix: 'BOLIGRAFO-',
url_template: 'https://jira.company.com/browse/BOLIGRAFO-<num>',
owner: 'bkeepers',
repo: 'test',
headers: { accept: 'application/vnd.github.v3+json' }
})

expect(github.repos.createAutolink).toHaveBeenCalledTimes(1)

expect(github.repos.deleteAutolink).toHaveBeenCalledWith({
autolink_id: 2,
owner: 'bkeepers',
repo: 'test',
headers: { accept: 'application/vnd.github.v3+json' }
})

expect(github.repos.deleteAutolink).toHaveBeenCalledTimes(1)
})
})
})
})