Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_<uppercased_client_id>_<key>`, for example:

- GITLAB_OTHER_BASE_URL
- GITLAB_OTHER_API_TOKEN
- …

## Project

> Show GitLab project info.
Expand Down
20 changes: 0 additions & 20 deletions src/client/config.js

This file was deleted.

61 changes: 41 additions & 20 deletions src/client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const config = require('./config')
const Gitlab = require('./gitlab').Gitlab

const aggregatePipelineJobs = jobs => {
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -80,15 +94,17 @@ 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,
jobs,
})
)
},
projectBranches({ project }) {
projectBranches({ project, client }) {
const gitlab = getClient(mozaik, client)
return Promise.all([
gitlab.getProject(project),
gitlab.getProjectBranches(project),
Expand All @@ -97,18 +113,21 @@ 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,
labels,
})
)
},
projectMilestones({ project }) {
projectMilestones({ project, client }) {
const gitlab = getClient(mozaik, client)
return Promise.all([
gitlab.getProject(project),
gitlab.getProjectMilestones(project),
Expand All @@ -117,15 +136,17 @@ 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,
events,
})
)
},
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

Expand Down
7 changes: 3 additions & 4 deletions src/components/Branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions src/components/JobHistogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/components/JobHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/MergeRequestsGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/ProjectContributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/components/ProjectMembers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/components/ProjectMilestones.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/activity/ProjectActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/components/labels/LabelsChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/components/pipelines/LatestProjectPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/components/Branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
})

Expand Down
Loading