diff --git a/package-lock.json b/package-lock.json index 6081a4b..2bd4b64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hubot-grafana", - "version": "7.0.1", + "version": "7.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hubot-grafana", - "version": "7.0.1", + "version": "7.0.2", "license": "MIT", "dependencies": { "@aws-sdk/client-s3": "^3.565.0", diff --git a/package.json b/package.json index 04eeb09..1480161 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hubot-grafana", "description": "Query Grafana dashboards", - "version": "7.0.1", + "version": "7.0.2", "author": "Stephen Yeargin ", "license": "MIT", "keywords": [ diff --git a/src/grafana-client.js b/src/grafana-client.js index 2695a93..5fb633a 100644 --- a/src/grafana-client.js +++ b/src/grafana-client.js @@ -4,6 +4,33 @@ const { URL, URLSearchParams } = require('url'); /// + /** + * If the given url does not have a host, it will add it to the + * url and return it. + * @param {string} url the url + * @returns {string} the expanded URL. + */ +function expandUrl(url, host) { + + if (url.startsWith('http://') || url.startsWith('https://')) { + return url; + } + + if (!host) { + throw new Error('No Grafana endpoint configured.'); + } + + let apiUrl = host; + if (!apiUrl.endsWith('/')) { + apiUrl += '/'; + } + + apiUrl += 'api/'; + apiUrl += url; + + return apiUrl; +} + class GrafanaClient { /** * Creates a new instance. @@ -38,12 +65,7 @@ class GrafanaClient { * @returns {Promise} the response data */ async get(url) { - if (!url.startsWith('http://') && !url.startsWith('https://') && !this.host) { - throw new Error('No Grafana endpoint configured.'); - } - - const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${this.host}/api/${url}`; - + const fullUrl = expandUrl(url, this.host); const response = await fetch(fullUrl, { method: 'GET', headers: grafanaHeaders(null, false, this.apiKey), @@ -63,8 +85,7 @@ class GrafanaClient { * @returns {Promise} */ async post(url, data) { - const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${this.host}/api/${url}`; - + const fullUrl = expandUrl(url, this.host); const response = await fetch(fullUrl, { method: 'POST', headers: grafanaHeaders('application/json', false, this.apiKey), @@ -87,16 +108,27 @@ class GrafanaClient { return; } - if (response.headers.get('content-type') == 'application/json') { - const json = await response.json(); + let contentType = null; + if (response.headers.has('content-type')) { + contentType = response.headers.get('content-type'); + if (contentType.includes(';')) { + contentType = contentType.split(';')[0]; + } + } + if (contentType == 'application/json') { + const json = await response.json(); const error = new Error(json.message || 'Error while fetching data from Grafana.'); error.data = json; throw error; } - const text = await response.text(); - throw new Error(text); + let error = new Error('Error while fetching data from Grafana.'); + if (contentType != 'text/html') { + error.data = await response.text(); + } + + throw error; } /** diff --git a/src/service/GrafanaService.js b/src/service/GrafanaService.js index ad95065..c00df8a 100644 --- a/src/service/GrafanaService.js +++ b/src/service/GrafanaService.js @@ -230,7 +230,7 @@ class GrafanaService { let page = 1; while (true) { - const url = `search?limit=${pageSize}&page=${encodeURIComponent(slug)}`; + const url = `search?limit=${pageSize}&page=${encodeURIComponent(page)}`; try { const items = await client.get(url); @@ -249,6 +249,7 @@ class GrafanaService { page++; } catch (err) { this.logger.error(err, `Error while getting dashboard on URL: ${url}`); + return null; } }