From e294dc4edc6bd2ebf1b15766ec49e5ebc8200ce6 Mon Sep 17 00:00:00 2001 From: chris48s Date: Tue, 28 Aug 2018 15:49:43 +0100 Subject: [PATCH] refactor [circleci] integration (#1927) * refactor [circleci] integration --- lib/all-badge-examples.js | 18 ------ server.js | 80 ------------------------ services/circleci/circleci.service.js | 89 +++++++++++++++++++++++++++ services/circleci/circleci.tester.js | 34 +++------- 4 files changed, 97 insertions(+), 124 deletions(-) create mode 100644 services/circleci/circleci.service.js diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index f06e559c959d9..34afd68ce5906 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -167,24 +167,6 @@ const allBadgeExamples = [ title: 'Codeship', previewUri: '/codeship/d6c1ddd0-16a3-0132-5f85-2e35c05e22b1/master.svg', }, - { - title: 'CircleCI', - previewUri: '/circleci/project/github/RedSparr0w/node-csgo-parser.svg', - }, - { - title: 'CircleCI branch', - previewUri: - '/circleci/project/github/RedSparr0w/node-csgo-parser/master.svg', - }, - { - title: 'CircleCI token', - previewUri: - '/circleci/project/github/RedSparr0w/node-csgo-parser/master.svg', - urlPattern: - '/circleci/token/:token/project/github/RedSparr0w/node-csgo-parser/master.svg', - exampleUri: - '/circleci/token/b90b5c49e59a4c67ba3a92f7992587ac7a0408c2/project/github/RedSparr0w/node-csgo-parser/master.svg', - }, { title: 'Visual Studio Team services', previewUri: diff --git a/server.js b/server.js index bb5f6c1350efb..a55c4e339aa1c 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,6 @@ const dom = require('xmldom').DOMParser; const jp = require('jsonpath'); const path = require('path'); -const queryString = require('query-string'); const xpath = require('xpath'); const yaml = require('js-yaml'); const Raven = require('raven'); @@ -118,85 +117,6 @@ loadServiceClasses().forEach( { camp, handleRequest: cache, githubApiProvider }, { handleInternalErrors: config.handleInternalErrors })); -// CircleCI build integration. -// https://circleci.com/api/v1/project/BrightFlair/PHP.Gt?circle-token=0a5143728784b263d9f0238b8d595522689b3af2&limit=1&filter=completed -camp.route(/^\/circleci\/(?:token\/(\w+))?[+/]?project\/(?:(github|bitbucket)\/)?([^/]+\/[^/]+)(?:\/(.*))?\.(svg|png|gif|jpg|json)$/, -cache(function(data, match, sendBadge, request) { - const token = match[1]; - const type = match[2] || 'github'; // github OR bitbucket - const userRepo = match[3]; // eg, `RedSparr0w/node-csgo-parser`. - const branch = match[4]; - const format = match[5]; - - // Base API URL - let apiUrl = 'https://circleci.com/api/v1.1/project/' + type + '/' + userRepo; - - // Query Params - var queryParams = {}; - queryParams['limit'] = 1; - queryParams['filter'] = 'completed'; - - // Custom Banch if present - if (branch != null) { - apiUrl += "/tree/" + branch; - } - - // Append Token to Query Params if present - if (token) { - queryParams['circle-token'] = token; - } - - // Apprend query params to API URL - apiUrl += '?' + queryString.stringify(queryParams); - - const badgeData = getBadgeData('build', data); - request(apiUrl, { json:true }, function(err, res, data) { - if (checkErrorResponse(badgeData, err, res, { 404: 'project not found' })) { - sendBadge(format, badgeData); - return; - } - try { - if (data.message !== undefined){ - badgeData.text[1] = data.message; - sendBadge(format, badgeData); - return; - } - - let passCount = 0; - let status; - for (let i=0; i - nock('https://circleci.com') - .get( - '/api/v1.1/project/github/RedSparr0w/node-csgo-parser?filter=completed&limit=1' - ) - .reply(invalidJSON) - ) - .expectJSON({ name: 'build', value: 'invalid' }) - t.create('circle ci (no response data)') .get('/project/github/RedSparr0w/node-csgo-parser.json') .intercept(nock => @@ -55,9 +43,10 @@ t.create('circle ci (no response data)') ) .reply(200) ) - .expectJSON({ name: 'build', value: 'invalid' }) + .expectJSON({ name: 'build', value: 'unparseable json response' }) -t.create('circle ci (multiple pipelines, pass)') +// we're passing &limit=1 so we expect exactly one array element +t.create('circle ci (invalid json)') .get('/project/github/RedSparr0w/node-csgo-parser.json?style=_shields_test') .intercept(nock => nock('https://circleci.com') @@ -66,15 +55,8 @@ t.create('circle ci (multiple pipelines, pass)') ) .reply(200, [{ status: 'success' }, { status: 'fixed' }]) ) - .expectJSON({ name: 'build', value: 'passing', colorB: '#4c1' }) - -t.create('circle ci (multiple pipelines, fail)') - .get('/project/github/RedSparr0w/node-csgo-parser.json?style=_shields_test') - .intercept(nock => - nock('https://circleci.com') - .get( - '/api/v1.1/project/github/RedSparr0w/node-csgo-parser?filter=completed&limit=1' - ) - .reply(200, [{ status: 'success' }, { status: 'failed' }]) - ) - .expectJSON({ name: 'build', value: 'failed', colorB: '#e05d44' }) + .expectJSON({ + name: 'build', + value: 'invalid json response', + colorB: '#9f9f9f', + })