diff --git a/README.md b/README.md index 8059f1f..bcb7bcb 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,35 @@ Configuration is loaded from environment variables. | GITLAB_BASE_URL | yes | | gitlab API base url, eg. `'https://gitlab.com/api/v4` | GITLAB_API_TOKEN | yes | | gitlab API token +You also have the ability to use multiple clients, you'll have to set the client parameter on your widget: + +```yaml +dashboards: +- + title: '@mozaik/ext-gitlab demo' + # … + widgets: + - + # will use 'default' client, GITLAB_* environment variables + extension: gitlab + widget: Project + project: gitlab-org/gitlab-ce + # … + - + # will use 'other' client, GITLAB_OTHER_* environment variables + extension: gitlab + widget: Project + project: gitlab-org/gitlab-ce + client: other <— HERE IT IS + # … +``` +when you define the `client` property, the loaded environment variables +must have the form `GITLAB__`, for example: + +- GITLAB_OTHER_BASE_URL +- GITLAB_OTHER_API_TOKEN +- … + ## Project > Show GitLab project info. diff --git a/src/client/config.js b/src/client/config.js deleted file mode 100644 index a59fd64..0000000 --- a/src/client/config.js +++ /dev/null @@ -1,20 +0,0 @@ -const convict = require('convict') - -const config = convict({ - gitlab: { - baseUrl: { - doc: 'The gitlab API base url.', - default: null, - format: String, - env: 'GITLAB_BASE_URL', - }, - token: { - doc: 'The gitlab API token.', - default: null, - format: String, - env: 'GITLAB_API_TOKEN', - }, - }, -}) - -module.exports = config diff --git a/src/client/index.js b/src/client/index.js index 97745e8..024be4d 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -1,6 +1,5 @@ 'use strict' -const config = require('./config') const Gitlab = require('./gitlab').Gitlab const aggregatePipelineJobs = jobs => { @@ -45,24 +44,38 @@ const aggregatePipelineJobs = jobs => { return stages } +const clients = {} + +const getClient = (mozaik, id = 'default') => { + if (clients[id] !== undefined) return clients[id] + + let envPrefix = 'GITLAB_' + if (id !== 'default') { + envPrefix += id.toUpperCase() + '_' + } + + let options = { + baseUrl: process.env[envPrefix + 'BASE_URL'], + token: process.env[envPrefix + 'API_TOKEN'], + } + + let client = new Gitlab(options.baseUrl, options.token, mozaik.request, mozaik.logger) + clients[id] = client + + return client +} + /** * @param {Mozaik} mozaik */ module.exports = mozaik => { - mozaik.loadApiConfig(config) - - const gitlab = new Gitlab( - config.get('gitlab.baseUrl'), - config.get('gitlab.token'), - mozaik.request, - mozaik.logger - ) - return { - project({ project }) { + project({ project, client }) { + const gitlab = getClient(mozaik, client) return gitlab.getProject(project) }, - projectMembers({ project }) { + projectMembers({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([ gitlab.getProject(project), gitlab.getProjectMembers(project), @@ -71,7 +84,8 @@ module.exports = mozaik => { members, })) }, - projectContributors({ project }) { + projectContributors({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([ gitlab.getProject(project), gitlab.getProjectContributors(project), @@ -80,7 +94,8 @@ module.exports = mozaik => { contributors, })) }, - projectJobs({ project }) { + projectJobs({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([gitlab.getProject(project), gitlab.getProjectJobs(project)]).then( ([project, jobs]) => ({ project, @@ -88,7 +103,8 @@ module.exports = mozaik => { }) ) }, - projectBranches({ project }) { + projectBranches({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([ gitlab.getProject(project), gitlab.getProjectBranches(project), @@ -97,10 +113,12 @@ module.exports = mozaik => { branches, })) }, - projectMergeRequests({ project, query = {} }) { + projectMergeRequests({ project, query = {}, client }) { + const gitlab = getClient(mozaik, client) return gitlab.getProjectMergeRequests(project, query) }, - projectLabels({ project }) { + projectLabels({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([gitlab.getProject(project), gitlab.getProjectLabels(project)]).then( ([project, labels]) => ({ project, @@ -108,7 +126,8 @@ module.exports = mozaik => { }) ) }, - projectMilestones({ project }) { + projectMilestones({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([ gitlab.getProject(project), gitlab.getProjectMilestones(project), @@ -117,7 +136,8 @@ module.exports = mozaik => { milestones, })) }, - projectEvents({ project }) { + projectEvents({ project, client }) { + const gitlab = getClient(mozaik, client) return Promise.all([gitlab.getProject(project), gitlab.getProjectEvents(project)]).then( ([project, events]) => ({ project, @@ -125,7 +145,8 @@ module.exports = mozaik => { }) ) }, - latestProjectPipeline({ project, ref }) { + latestProjectPipeline({ project, ref, client }) { + const gitlab = getClient(mozaik, client) return gitlab.getProjectPipelines(project, { ref, per_page: 1 }).then(({ items }) => { if (items.length === 0) return null diff --git a/src/components/Branches.js b/src/components/Branches.js index cbcf82e..edd8523 100644 --- a/src/components/Branches.js +++ b/src/components/Branches.js @@ -30,13 +30,12 @@ export default class Branches extends Component { apiError: PropTypes.object, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectBranches.${project}`, - params: { project }, + id: `gitlab.projectBranches.${client}.${project}`, + params: { project, client }, } } - render() { const { title, apiData, apiError } = this.props diff --git a/src/components/JobHistogram.js b/src/components/JobHistogram.js index 2041d92..3e5a8c1 100644 --- a/src/components/JobHistogram.js +++ b/src/components/JobHistogram.js @@ -46,13 +46,12 @@ export default class JobHistogram extends Component { theme: PropTypes.object.isRequired, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectJobs.${project}`, - params: { project }, + id: `gitlab.projectJobs.${client}.${project}`, + params: { project, client }, } } - render() { const { title, apiData, apiError, theme } = this.props diff --git a/src/components/JobHistory.js b/src/components/JobHistory.js index 7c1af36..0f4a2ce 100644 --- a/src/components/JobHistory.js +++ b/src/components/JobHistory.js @@ -27,10 +27,10 @@ export default class JobHistory extends Component { apiError: PropTypes.object, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectJobs.${project}`, - params: { project }, + id: `gitlab.projectJobs.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/MergeRequestsGauge.js b/src/components/MergeRequestsGauge.js index a77bef8..2b01fe6 100644 --- a/src/components/MergeRequestsGauge.js +++ b/src/components/MergeRequestsGauge.js @@ -33,15 +33,15 @@ export default class MergeRequestsGauge extends Component { ], apiData: 0, } - - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectMergeRequests.${project}.opened`, + id: `gitlab.projectMergeRequests.${client}.${project}`, params: { project, query: { state: 'opened', }, + client, }, } } diff --git a/src/components/Project.js b/src/components/Project.js index 7b4da20..2ed9996 100644 --- a/src/components/Project.js +++ b/src/components/Project.js @@ -81,10 +81,10 @@ export default class Project extends Component { theme: PropTypes.object.isRequired, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.project.${project}`, - params: { project }, + id: `gitlab.project.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/ProjectContributors.js b/src/components/ProjectContributors.js index 7e360be..367dd5d 100644 --- a/src/components/ProjectContributors.js +++ b/src/components/ProjectContributors.js @@ -31,10 +31,10 @@ export default class ProjectContributors extends Component { apiError: PropTypes.object, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectContributors.${project}`, - params: { project }, + id: `gitlab.projectContributors.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/ProjectMembers.js b/src/components/ProjectMembers.js index 0540873..539e39b 100644 --- a/src/components/ProjectMembers.js +++ b/src/components/ProjectMembers.js @@ -29,11 +29,10 @@ export default class ProjectMembers extends Component { }), apiError: PropTypes.object, } - - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectMembers.${project}`, - params: { project }, + id: `gitlab.projectMembers.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/ProjectMilestones.js b/src/components/ProjectMilestones.js index d55eebf..4f51f37 100644 --- a/src/components/ProjectMilestones.js +++ b/src/components/ProjectMilestones.js @@ -32,11 +32,10 @@ export default class ProjectMilestones extends Component { }), apiError: PropTypes.object, } - - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectMilestones.${project}`, - params: { project }, + id: `gitlab.projectMilestones.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/activity/ProjectActivity.js b/src/components/activity/ProjectActivity.js index 45c2875..e850983 100644 --- a/src/components/activity/ProjectActivity.js +++ b/src/components/activity/ProjectActivity.js @@ -31,10 +31,10 @@ export default class ProjectActivity extends Component { apiError: PropTypes.object, } - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectEvents.${project}`, - params: { project }, + id: `gitlab.projectEvents.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/labels/LabelsChart.js b/src/components/labels/LabelsChart.js index f4a0357..0331c71 100644 --- a/src/components/labels/LabelsChart.js +++ b/src/components/labels/LabelsChart.js @@ -42,11 +42,10 @@ export default class LabelsChart extends Component { countBy: 'open_issues_count', animate: false, } - - static getApiRequest({ project }) { + static getApiRequest({ project, client = 'default' }) { return { - id: `gitlab.projectLabels.${project}`, - params: { project }, + id: `gitlab.projectLabels.${client}.${project}`, + params: { project, client }, } } diff --git a/src/components/pipelines/LatestProjectPipeline.js b/src/components/pipelines/LatestProjectPipeline.js index 392b670..2001fd5 100644 --- a/src/components/pipelines/LatestProjectPipeline.js +++ b/src/components/pipelines/LatestProjectPipeline.js @@ -172,16 +172,15 @@ export default class LatestProjectPipeline extends Component { static defaultProps = { hideCommitMessage: false, } - - static getApiRequest({ project, gitRef }) { - let id = `gitlab.latestProjectPipeline.${project}` + static getApiRequest({ project, gitRef, client = 'default' }) { + let id = `gitlab.latestProjectPipeline.${client}.${project}` if (gitRef !== undefined) { id += `.${gitRef}` } return { id, - params: { project, ref: gitRef }, + params: { project, ref: gitRef, client }, } } diff --git a/test/components/Branches.test.js b/test/components/Branches.test.js index ad042c9..9493fec 100644 --- a/test/components/Branches.test.js +++ b/test/components/Branches.test.js @@ -15,8 +15,8 @@ test('should return correct api request', () => { project: fixtures.project.name, }) ).toEqual({ - id: `gitlab.projectBranches.${fixtures.project.name}`, - params: { project: fixtures.project.name }, + id: `gitlab.projectBranches.default.${fixtures.project.name}`, + params: { client: 'default', project: fixtures.project.name }, }) }) diff --git a/test/components/__snapshots__/Branches.test.js.snap b/test/components/__snapshots__/Branches.test.js.snap index eea2bd1..cd40242 100644 --- a/test/components/__snapshots__/Branches.test.js.snap +++ b/test/components/__snapshots__/Branches.test.js.snap @@ -155,7 +155,7 @@ exports[`should render as expected 1`] = ` />   - 3 years ago + 4 years ago @@ -244,7 +244,7 @@ exports[`should render as expected 1`] = ` />   - 3 years ago + 4 years ago @@ -333,7 +333,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -422,7 +422,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -511,7 +511,7 @@ exports[`should render as expected 1`] = ` />   - a year ago + 2 years ago @@ -600,7 +600,7 @@ exports[`should render as expected 1`] = ` />   - a year ago + 2 years ago @@ -689,7 +689,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -778,7 +778,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -867,7 +867,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -956,7 +956,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -1045,7 +1045,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -1134,7 +1134,7 @@ exports[`should render as expected 1`] = ` />   - 2 years ago + 3 years ago @@ -1223,7 +1223,7 @@ exports[`should render as expected 1`] = ` />   - a year ago + 2 years ago @@ -1312,7 +1312,7 @@ exports[`should render as expected 1`] = ` />   - a year ago + 2 years ago @@ -1401,7 +1401,7 @@ exports[`should render as expected 1`] = ` />   - 10 months ago + 2 years ago @@ -1490,7 +1490,7 @@ exports[`should render as expected 1`] = ` />   - 6 months ago + 2 years ago @@ -1579,7 +1579,7 @@ exports[`should render as expected 1`] = ` />   - 6 months ago + 2 years ago @@ -1668,7 +1668,7 @@ exports[`should render as expected 1`] = ` />   - 6 months ago + 2 years ago @@ -1757,7 +1757,7 @@ exports[`should render as expected 1`] = ` />   - 6 months ago + 2 years ago @@ -1846,7 +1846,7 @@ exports[`should render as expected 1`] = ` />   - 5 months ago + a year ago