Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Move [Jenkins] job url to query parameter (badges#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB authored Nov 30, 2019
1 parent c2876e8 commit 92b1380
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 97 deletions.
18 changes: 15 additions & 3 deletions services/jenkins/jenkins-build-redirect.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict'

const { buildRedirectUrl } = require('./jenkins-common')
const { redirector } = require('..')

const commonProps = {
category: 'build',
transformPath: ({ protocol, host, job }) =>
`/jenkins/build/${protocol}/${host}/${job}`,
dateAdded: new Date('2019-04-20'),
transformPath: () => '/jenkins/build',
transformQueryParams: ({ protocol, host, job }) => ({
jobUrl: buildRedirectUrl({ protocol, host, job }),
}),
}

module.exports = [
Expand All @@ -15,13 +17,23 @@ module.exports = [
base: 'jenkins-ci/s',
pattern: ':protocol(http|https)/:host/:job+',
},
dateAdded: new Date('2019-04-20'),
...commonProps,
}),
redirector({
route: {
base: 'jenkins/s',
pattern: ':protocol(http|https)/:host/:job+',
},
dateAdded: new Date('2019-04-20'),
...commonProps,
}),
redirector({
route: {
base: 'jenkins/build',
pattern: ':protocol(http|https)/:host/:job+',
},
dateAdded: new Date('2019-11-29'),
...commonProps,
}),
]
24 changes: 20 additions & 4 deletions services/jenkins/jenkins-build-redirect.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@ const t = (module.exports = new ServiceTester({
pathPrefix: '/',
}))

t.create('jenkins ci')
t.create('old jenkins ci prefix + job url in path')
.get('jenkins-ci/s/https/updates.jenkins-ci.org/job/foo.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
'/jenkins/build/https/updates.jenkins-ci.org/job/foo.svg'
`/jenkins/build.svg?jobUrl=${encodeURIComponent(
'https://updates.jenkins-ci.org/job/foo'
)}`
)

t.create('jenkins shorthand')
t.create('old jenkins shorthand prefix + job url in path')
.get('jenkins/s/https/updates.jenkins-ci.org/job/foo.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
'/jenkins/build/https/updates.jenkins-ci.org/job/foo.svg'
`/jenkins/build.svg?jobUrl=${encodeURIComponent(
'https://updates.jenkins-ci.org/job/foo'
)}`
)

t.create('new jenkins build prefix + job url in path')
.get('jenkins/build/https/updates.jenkins-ci.org/job/foo.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
`/jenkins/build.svg?jobUrl=${encodeURIComponent(
'https://updates.jenkins-ci.org/job/foo'
)}`
)
16 changes: 7 additions & 9 deletions services/jenkins/jenkins-build.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ module.exports = class JenkinsBuild extends JenkinsBase {

static get route() {
return {
base: 'jenkins/build',
pattern: ':protocol(http|https)/:host/:job+',
base: 'jenkins',
pattern: 'build',
queryParamSchema,
}
}
Expand All @@ -50,11 +50,9 @@ module.exports = class JenkinsBuild extends JenkinsBase {
return [
{
title: 'Jenkins',
namedParams: {
protocol: 'https',
host: 'jenkins.qa.ubuntu.com',
job:
'view/Precise/view/All%20Precise/job/precise-desktop-amd64_default',
namedParams: {},
queryParams: {
jobUrl: 'https://wso2.org/jenkins/view/All%20Builds/job/archetypes',
},
staticPreview: renderBuildStatusBadge({ status: 'passing' }),
},
Expand Down Expand Up @@ -82,9 +80,9 @@ module.exports = class JenkinsBuild extends JenkinsBase {
return { status: colorStatusMap[json.color] }
}

async handle({ protocol, host, job }, { disableStrictSSL }) {
async handle(namedParams, { jobUrl, disableStrictSSL }) {
const json = await this.fetch({
url: buildUrl({ protocol, host, job, lastCompletedBuild: false }),
url: buildUrl({ jobUrl, lastCompletedBuild: false }),
schema,
qs: buildTreeParamQueryString('color'),
disableStrictSSL,
Expand Down
14 changes: 9 additions & 5 deletions services/jenkins/jenkins-build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ describe('JenkinsBuild', function() {
.reply(200, { color: 'blue' })

expect(
await JenkinsBuild.invoke(defaultContext, config, {
protocol: 'https',
host: 'jenkins.ubuntu.com',
job: 'server/job/curtin-vmtest-daily-x',
})
await JenkinsBuild.invoke(
defaultContext,
config,
{},
{
jobUrl:
'https://jenkins.ubuntu.com/server/job/curtin-vmtest-daily-x',
}
)
).to.deep.equal({
label: undefined,
message: 'passing',
Expand Down
10 changes: 7 additions & 3 deletions services/jenkins/jenkins-build.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ const isJenkinsBuildStatus = Joi.alternatives(
)

t.create('build job not found')
.get('/https/ci.eclipse.org/jgit/job/does-not-exist.json')
.get('/build.json?jobUrl=https://ci.eclipse.org/jgit/job/does-not-exist')
.expectBadge({ label: 'build', message: 'instance or job not found' })

t.create('build found (view)')
.get('/https/wso2.org/jenkins/view/All%20Builds/job/archetypes.json')
.get(
`/build.json?jobUrl=${encodeURIComponent(
'https://wso2.org/jenkins/view/All Builds/job/archetypes'
)}`
)
.expectBadge({ label: 'build', message: isJenkinsBuildStatus })

t.create('build found (job)')
.get('/https/ci.eclipse.org/jgit/job/jgit.json')
.get('/build.json?jobUrl=https://ci.eclipse.org/jgit/job/jgit')
.expectBadge({ label: 'build', message: isJenkinsBuildStatus })
23 changes: 13 additions & 10 deletions services/jenkins/jenkins-common.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
'use strict'

const Joi = require('@hapi/joi')
const { optionalUrl } = require('../validators')

const queryParamSchema = Joi.object({
disableStrictSSL: Joi.equal(''),
jobUrl: optionalUrl,
}).required()

const buildUrl = ({
protocol,
host,
job,
lastCompletedBuild = true,
plugin,
}) => {
const buildRedirectUrl = ({ protocol, host, job }) => {
const jobPrefix = job.indexOf('/') > -1 ? '' : 'job/'
return `${protocol}://${host}/${jobPrefix}${job}/${
lastCompletedBuild ? 'lastCompletedBuild/' : ''
}${plugin ? `${plugin}/` : ''}api/json`
return `${protocol}://${host}/${jobPrefix}${job}`
}

const buildUrl = ({ jobUrl, lastCompletedBuild = true, plugin }) => {
const lastCompletedBuildElement = lastCompletedBuild
? 'lastCompletedBuild/'
: ''
const pluginElement = plugin ? `${plugin}/` : ''
return `${jobUrl}/${lastCompletedBuildElement}${pluginElement}api/json`
}

module.exports = {
queryParamSchema,
buildTreeParamQueryString: tree => ({ tree }),
buildUrl,
buildRedirectUrl,
}
65 changes: 65 additions & 0 deletions services/jenkins/jenkins-common.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'
const { expect } = require('chai')
const { buildRedirectUrl, buildUrl } = require('./jenkins-common')

describe('jenkins-common', function() {
describe('buildUrl', function() {
it('returns the json api url', function() {
const actualResult = buildUrl({
jobUrl: 'https://ci.eclipse.org/jgit/job/jgit',
})

expect(actualResult).to.equal(
'https://ci.eclipse.org/jgit/job/jgit/lastCompletedBuild/api/json'
)
})

it('returns the json api url including a plugin name', function() {
const actualResult = buildUrl({
jobUrl: 'https://ci.eclipse.org/jgit/job/jgit',
plugin: 'cobertura',
})

expect(actualResult).to.equal(
'https://ci.eclipse.org/jgit/job/jgit/lastCompletedBuild/cobertura/api/json'
)
})

it('returns the json api url without the lastCompletedBuild element', function() {
const actualResult = buildUrl({
jobUrl: 'https://ci.eclipse.org/jgit/job/jgit',
lastCompletedBuild: false,
})

expect(actualResult).to.equal(
'https://ci.eclipse.org/jgit/job/jgit/api/json'
)
})
})

describe('buildRedirectUrl', function() {
it('returns the job url', function() {
const actualResult = buildRedirectUrl({
protocol: 'https',
host: 'jenkins.sqlalchemy.org',
job: 'job/alembic_coverage',
})

expect(actualResult).to.equal(
'https://jenkins.sqlalchemy.org/job/alembic_coverage'
)
})

it('returns the job url and adds missing /job prefixes', function() {
const actualResult = buildRedirectUrl({
protocol: 'https',
host: 'jenkins.sqlalchemy.org',
job: 'alembic_coverage',
})

expect(actualResult).to.equal(
'https://jenkins.sqlalchemy.org/job/alembic_coverage'
)
})
})
})
41 changes: 30 additions & 11 deletions services/jenkins/jenkins-coverage-redirector.service.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
'use strict'

const { buildRedirectUrl } = require('./jenkins-common')
const { redirector } = require('..')

module.exports = redirector({
const commonProps = {
category: 'coverage',
route: {
base: 'jenkins',
pattern: ':coverageFormat(j|c)/:protocol(http|https)/:host/:job+',
},
transformPath: ({ coverageFormat, protocol, host, job }) =>
`/jenkins/coverage/${
coverageFormat === 'j' ? 'jacoco' : 'cobertura'
}/${protocol}/${host}/${job}`,
dateAdded: new Date('2019-04-20'),
})
transformQueryParams: ({ protocol, host, job }) => ({
jobUrl: buildRedirectUrl({ protocol, host, job }),
}),
}

module.exports = [
redirector({
route: {
base: 'jenkins',
pattern: ':coverageFormat(j|c)/:protocol(http|https)/:host/:job+',
},
transformPath: ({ coverageFormat }) =>
`/jenkins/coverage/${coverageFormat === 'j' ? 'jacoco' : 'cobertura'}`,
dateAdded: new Date('2019-04-20'),
...commonProps,
}),
redirector({
route: {
base: 'jenkins/coverage',
pattern:
':coverageFormat(jacoco|cobertura|api)/:protocol(http|https)/:host/:job+',
},
transformPath: ({ coverageFormat }) =>
`/jenkins/coverage/${coverageFormat}`,
dateAdded: new Date('2019-11-29'),
...commonProps,
}),
]
57 changes: 53 additions & 4 deletions services/jenkins/jenkins-coverage-redirector.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const t = (module.exports = new ServiceTester({
pathPrefix: '/jenkins',
}))

t.create('Jacoco')
t.create('old Jacoco prefix + job url in path')
.get(
'/j/https/wso2.org/jenkins/view/All%20Builds/job/sonar/job/sonar-carbon-dashboards.svg',
{
Expand All @@ -18,15 +18,64 @@ t.create('Jacoco')
.expectStatus(301)
.expectHeader(
'Location',
'/jenkins/coverage/jacoco/https/wso2.org/jenkins/view/All Builds/job/sonar/job/sonar-carbon-dashboards.svg'
`/jenkins/coverage/jacoco.svg?jobUrl=${encodeURIComponent(
'https://wso2.org/jenkins/view/All Builds/job/sonar/job/sonar-carbon-dashboards'
)}`
)

t.create('Cobertura')
t.create('new Jacoco prefix + job url in path')
.get(
'/coverage/jacoco/https/wso2.org/jenkins/view/All%20Builds/job/sonar/job/sonar-carbon-dashboards.svg',
{
followRedirect: false,
}
)
.expectStatus(301)
.expectHeader(
'Location',
`/jenkins/coverage/jacoco.svg?jobUrl=${encodeURIComponent(
'https://wso2.org/jenkins/view/All Builds/job/sonar/job/sonar-carbon-dashboards'
)}`
)

t.create('old Cobertura prefix + job url in path')
.get('/c/https/jenkins.sqlalchemy.org/job/alembic_coverage.svg', {
followRedirect: false,
})
.expectStatus(301)
.expectHeader(
'Location',
'/jenkins/coverage/cobertura/https/jenkins.sqlalchemy.org/job/alembic_coverage.svg'
`/jenkins/coverage/cobertura.svg?jobUrl=${encodeURIComponent(
'https://jenkins.sqlalchemy.org/job/alembic_coverage'
)}`
)

t.create('new Cobertura prefix + job url in path')
.get(
'/coverage/cobertura/https/jenkins.sqlalchemy.org/job/alembic_coverage.svg',
{
followRedirect: false,
}
)
.expectStatus(301)
.expectHeader(
'Location',
`/jenkins/coverage/cobertura.svg?jobUrl=${encodeURIComponent(
'https://jenkins.sqlalchemy.org/job/alembic_coverage'
)}`
)

t.create('api prefix + job url in path')
.get(
'/coverage/api/https/jenkins.library.illinois.edu/job/OpenSourceProjects/job/Speedwagon/job/master.svg',
{
followRedirect: false,
}
)
.expectStatus(301)
.expectHeader(
'Location',
`/jenkins/coverage/api.svg?jobUrl=${encodeURIComponent(
'https://jenkins.library.illinois.edu/job/OpenSourceProjects/job/Speedwagon/job/master'
)}`
)
Loading

0 comments on commit 92b1380

Please sign in to comment.