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

move [Jira] hostname to query param #3990

Merged
merged 5 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions services/jira/jira-issue-redirect.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const { redirector } = require('..')

module.exports = [
redirector({
category: 'issue-tracking',
route: {
base: 'jira/issue',
pattern: ':protocol(http|https)/:hostAndPath(.+)/:issueKey',
},
transformPath: ({ issueKey }) => `/jira/issue/${issueKey}`,
transformQueryParams: ({ protocol, hostAndPath }) => ({
hostUrl: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-14'),
}),
]
21 changes: 21 additions & 0 deletions services/jira/jira-issue-redirect.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { ServiceTester } = require('../tester')

const t = (module.exports = new ServiceTester({
id: 'JiraIssueRedirect',
title: 'JiraIssueRedirect',
pathPrefix: '/jira/issue',
}))

t.create('jira issue')
.get('/https/issues.apache.org/jira/kafka-2896.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/jira/issue/kafka-2896.svg?hostUrl=${encodeURIComponent(
'https://issues.apache.org/jira'
)}`
)
22 changes: 12 additions & 10 deletions services/jira/jira-issue.service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict'

const Joi = require('@hapi/joi')
const { optionalUrl } = require('../validators')
const { authConfig } = require('./jira-common')
const { BaseJsonService } = require('..')

const queryParamSchema = Joi.object({
hostUrl: optionalUrl.required(),
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
}).required()

const schema = Joi.object({
fields: Joi.object({
status: Joi.object({
Expand All @@ -23,9 +28,8 @@ module.exports = class JiraIssue extends BaseJsonService {
static get route() {
return {
base: 'jira/issue',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: ':protocol(http|https)/:hostAndPath(.+)/:issueKey',
pattern: ':issueKey',
queryParamSchema,
}
}

Expand All @@ -37,12 +41,12 @@ module.exports = class JiraIssue extends BaseJsonService {
return [
{
title: 'JIRA issue',
pattern: ':protocol/:hostAndPath/:issueKey',
namedParams: {
protocol: 'https',
hostAndPath: 'issues.apache.org/jira',
issueKey: 'KAFKA-2896',
},
queryParams: {
hostUrl: 'https://issues.apache.org/jira',
},
staticPreview: this.render({
issueKey: 'KAFKA-2896',
statusName: 'Resolved',
Expand Down Expand Up @@ -77,13 +81,11 @@ module.exports = class JiraIssue extends BaseJsonService {
}
}

async handle({ protocol, hostAndPath, issueKey }) {
async handle({ issueKey }, { hostUrl }) {
// Atlassian Documentation: https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-api-2-issue-issueIdOrKey-get
const json = await this._requestJson({
schema,
url: `${protocol}://${hostAndPath}/rest/api/2/issue/${encodeURIComponent(
issueKey
)}`,
url: `${hostUrl}/rest/api/2/issue/${encodeURIComponent(issueKey)}`,
options: { auth: this.authHelper.basicAuth },
errorMessages: { 404: 'issue not found' },
})
Expand Down
13 changes: 8 additions & 5 deletions services/jira/jira-issue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ describe('JiraIssue', function() {
.reply(200, { fields: { status: { name: 'in progress' } } })

expect(
await JiraIssue.invoke(defaultContext, config, {
protocol: 'https',
hostAndPath: 'myprivatejira.test',
issueKey: 'secure-234',
})
await JiraIssue.invoke(
defaultContext,
config,
{
issueKey: 'secure-234',
},
{ hostUrl: 'https://myprivatejira.test' }
)
).to.deep.equal({
label: 'secure-234',
message: 'in progress',
Expand Down
18 changes: 9 additions & 9 deletions services/jira/jira-issue.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
const t = (module.exports = require('../tester').createServiceTester())

t.create('unknown issue')
.get('/https/issues.apache.org/jira/notArealIssue-000.json')
.get('/notArealIssue-000.json?hostUrl=https://issues.apache.org/jira')
.expectBadge({ label: 'jira', message: 'issue not found' })

t.create('known issue')
.get('/https/issues.apache.org/jira/kafka-2896.json')
.get('/kafka-2896.json?hostUrl=https://issues.apache.org/jira')
.expectBadge({ label: 'kafka-2896', message: 'Resolved' })

t.create('no status color')
.get('/http/issues.apache.org/jira/foo-123.json')
.get('/foo-123.json?hostUrl=http://issues.apache.org/jira')
.intercept(nock =>
nock('http://issues.apache.org/jira/rest/api/2/issue')
.get(`/${encodeURIComponent('foo-123')}`)
Expand All @@ -30,7 +30,7 @@ t.create('no status color')
})

t.create('green status color')
.get('/https/issues.apache.org:8000/jira/bar-345.json')
.get('/bar-345.json?hostUrl=https://issues.apache.org:8000/jira')
.intercept(nock =>
nock('https://issues.apache.org:8000/jira/rest/api/2/issue')
.get(`/${encodeURIComponent('bar-345')}`)
Expand All @@ -52,7 +52,7 @@ t.create('green status color')
})

t.create('medium-gray status color')
.get('/https/issues.apache.org:8080/abc-123.json')
.get('/abc-123.json?hostUrl=https://issues.apache.org:8080')
.intercept(nock =>
nock('https://issues.apache.org:8080/rest/api/2/issue')
.get(`/${encodeURIComponent('abc-123')}`)
Expand All @@ -74,7 +74,7 @@ t.create('medium-gray status color')
})

t.create('yellow status color')
.get('/https/issues.apache.org/test-001.json')
.get('/test-001.json?hostUrl=https://issues.apache.org')
.intercept(nock =>
nock('https://issues.apache.org/rest/api/2/issue')
.get(`/${encodeURIComponent('test-001')}`)
Expand All @@ -96,7 +96,7 @@ t.create('yellow status color')
})

t.create('brown status color')
.get('/https/issues.apache.org/zzz-789.json')
.get('/zzz-789.json?hostUrl=https://issues.apache.org')
.intercept(nock =>
nock('https://issues.apache.org/rest/api/2/issue')
.get(`/${encodeURIComponent('zzz-789')}`)
Expand All @@ -118,7 +118,7 @@ t.create('brown status color')
})

t.create('warm-red status color')
.get('/https/issues.apache.org/fire-321.json')
.get('/fire-321.json?hostUrl=https://issues.apache.org')
.intercept(nock =>
nock('https://issues.apache.org/rest/api/2/issue')
.get(`/${encodeURIComponent('fire-321')}`)
Expand All @@ -140,7 +140,7 @@ t.create('warm-red status color')
})

t.create('blue-gray status color')
.get('/https/issues.apache.org/sky-775.json')
.get('/sky-775.json?hostUrl=https://issues.apache.org')
.intercept(nock =>
nock('https://issues.apache.org/rest/api/2/issue')
.get(`/${encodeURIComponent('sky-775')}`)
Expand Down
18 changes: 18 additions & 0 deletions services/jira/jira-sprint-redirect.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const { redirector } = require('..')

module.exports = [
redirector({
category: 'issue-tracking',
route: {
base: 'jira/sprint',
pattern: ':protocol(http|https)/:hostAndPath(.+)/:sprintId',
},
transformPath: ({ sprintId }) => `/jira/sprint/${sprintId}`,
transformQueryParams: ({ protocol, hostAndPath }) => ({
hostUrl: `${protocol}://${hostAndPath}`,
}),
dateAdded: new Date('2019-09-14'),
}),
]
21 changes: 21 additions & 0 deletions services/jira/jira-sprint-redirect.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { ServiceTester } = require('../tester')

const t = (module.exports = new ServiceTester({
id: 'JiraSprintRedirect',
title: 'JiraSprintRedirect',
pathPrefix: '/jira/sprint',
}))

t.create('jira sprint')
.get('/https/jira.spring.io/94.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/jira/sprint/94.svg?hostUrl=${encodeURIComponent(
'https://jira.spring.io'
)}`
)
20 changes: 12 additions & 8 deletions services/jira/jira-sprint.service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict'

const Joi = require('@hapi/joi')
const { optionalUrl } = require('../validators')
const { authConfig } = require('./jira-common')
const { BaseJsonService } = require('..')

const queryParamSchema = Joi.object({
hostUrl: optionalUrl.required(),
}).required()

const schema = Joi.object({
total: Joi.number(),
issues: Joi.array()
Expand Down Expand Up @@ -35,9 +40,8 @@ module.exports = class JiraSprint extends BaseJsonService {
static get route() {
return {
base: 'jira/sprint',
// Do not base new services on this route pattern.
// See https://github.com/badges/shields/issues/3714
pattern: ':protocol(http|https)/:hostAndPath(.+)/:sprintId',
pattern: ':sprintId',
queryParamSchema,
}
}

Expand All @@ -49,12 +53,12 @@ module.exports = class JiraSprint extends BaseJsonService {
return [
{
title: 'JIRA sprint completion',
pattern: ':protocol/:hostAndPath/:sprintId',
namedParams: {
protocol: 'https',
hostAndPath: 'jira.spring.io',
sprintId: '94',
},
queryParams: {
hostUrl: 'https://jira.spring.io',
},
staticPreview: this.render({
numCompletedIssues: 27,
numTotalIssues: 28,
Expand Down Expand Up @@ -86,12 +90,12 @@ module.exports = class JiraSprint extends BaseJsonService {
}
}

async handle({ protocol, hostAndPath, sprintId }) {
async handle({ sprintId }, { hostUrl }) {
// Atlassian Documentation: https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-group-Search
// There are other sprint-specific APIs but those require authentication. The search API
// allows us to get the needed data without being forced to authenticate.
const json = await this._requestJson({
url: `${protocol}://${hostAndPath}/rest/api/2/search`,
url: `${hostUrl}/rest/api/2/search`,
schema,
options: {
qs: {
Expand Down
13 changes: 8 additions & 5 deletions services/jira/jira-sprint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ describe('JiraSprint', function() {
})

expect(
await JiraSprint.invoke(defaultContext, config, {
protocol: 'https',
hostAndPath: 'myprivatejira.test/jira',
sprintId,
})
await JiraSprint.invoke(
defaultContext,
config,
{
sprintId,
},
{ hostUrl: 'https://myprivatejira.test/jira' }
)
).to.deep.equal({
label: 'completion',
message: '50%',
Expand Down
12 changes: 6 additions & 6 deletions services/jira/jira-sprint.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ const { isIntegerPercentage } = require('../test-validators')
const { sprintId, sprintQueryString } = require('./jira-test-helpers')

t.create('unknown sprint')
.get('/https/jira.spring.io/abc.json')
.get('/abc.json?hostUrl=https://jira.spring.io')
.expectBadge({ label: 'jira', message: 'sprint not found' })

t.create('known sprint')
.get('/https/jira.spring.io/94.json')
.get('/94.json?hostUrl=https://jira.spring.io')
.expectBadge({
label: 'completion',
message: isIntegerPercentage,
})

t.create('100% completion')
.get(`/http/issues.apache.org/jira/${sprintId}.json`)
.get(`/${sprintId}.json?hostUrl=http://issues.apache.org/jira`)
.intercept(nock =>
nock('http://issues.apache.org/jira/rest/api/2')
.get('/search')
Expand Down Expand Up @@ -48,7 +48,7 @@ t.create('100% completion')
})

t.create('0% completion')
.get(`/http/issues.apache.org/jira/${sprintId}.json`)
.get(`/${sprintId}.json?hostUrl=http://issues.apache.org/jira`)
.intercept(nock =>
nock('http://issues.apache.org/jira/rest/api/2')
.get('/search')
Expand All @@ -73,7 +73,7 @@ t.create('0% completion')
})

t.create('no issues in sprint')
.get(`/http/issues.apache.org/jira/${sprintId}.json`)
.get(`/${sprintId}.json?hostUrl=http://issues.apache.org/jira`)
.intercept(nock =>
nock('http://issues.apache.org/jira/rest/api/2')
.get('/search')
Expand All @@ -90,7 +90,7 @@ t.create('no issues in sprint')
})

t.create('issue with null resolution value')
.get(`/https/jira.spring.io:8080/${sprintId}.json`)
.get(`/${sprintId}.json?hostUrl=https://jira.spring.io:8080`)
.intercept(nock =>
nock('https://jira.spring.io:8080/rest/api/2')
.get('/search')
Expand Down