🤖 GitLab API NodeJS library with full support of all the Gitlab API services.
- Install
- Usage
- Migrating from node-gitlab
- Docs
- Development
- Testing
- CLI
- Contributors
- License
- Changelog
# Install from npm
npm install gitlab
The API's that are currently supported are:
// General
ApplicationSettings
BroadcastMessages
Events
FeatureFlags
GeoNodes
GitignoreTemplates
GitLabCIYMLTemplates
Keys
Licence
LicenceTemplates
Lint
Namespaces
NotificationSettings
PagesDomains
Search
SidekiqMetrics
SystemHooks
Version
Wikis
// Groups
Groups
GroupAccessRequests
GroupBadges
GroupCustomAttributes
GroupIssueBoards
GroupMembers
GroupMilestones
GroupProjects
GroupVariables
Epics
EpicIssues
EpicNotes
EpicDiscussions
// Projects
Branches
Commits
Deployments
DeployKeys
Environments
Issues
IssueNotes
IssueDiscussions
IssueAwardEmojis
Jobs
Labels
MergeRequests
MergeRequestAwardEmojis
MergeRequestNotes
Pipelines
PipelineSchedules
PipelineScheduleVariables
Projects
ProjectAccessRequests
ProjectBadges
ProjectCustomAttributes
ProjectImportExport
ProjectIssueBoards
ProjectHooks
ProjectMembers
ProjectMilestones
ProjectSnippets
ProjectSnippetNotes
ProjectSnippetDiscussions
ProjectSnippetAwardEmojis
ProtectedBranches
ProjectVariables
Repositories
RepositoryFiles
Runners
Services
Tags
Todos
Triggers
// Users
Users
UserEmails
UserImpersonationTokens
UserKeys
UserGPGKeys
URL to your GitLab instance should not include /api/v4
path.
Instantiate the library using a basic token created in your Gitlab Profile
// ES6 (>=node 8.0.0)
import Gitlab from 'gitlab';
// ES5, assuming native or polyfilled Promise is available
const Gitlab = require('gitlab/dist/es5').default
// Instantiating
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
})
// Or, use a OAuth token instead!
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
oauthToken: 'abcdefghij123456'
})
Sometimes you don't want to import and instantiate the whole Gitlab API, perhaps you only want access to the Projects API. To do this, one only needs to import and instantiate this specific API:
import { Projects } from 'gitlab';
const service = new Projects({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
})
It can be annoying to have to import all the API's pertaining to a specific resource. For example, the Projects resource is composed of many API's, Projects, Issues, Labels, MergeRequests, etc. For convenience, there is a Bundle export for importing and instantiating all these related API's at once.
import { ProjectsBundle } from 'gitlab';
const services = new ProjectsBundle({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
})
services.Projects.all()
services.MergeRequests.all()
etc..
Currently there are three Bundles:
- ProjectsBundle which includes:
Branches
Commits
CommitDiscussions
Deployments
DeployKeys
Environments
Issues
IssueNotes
IssueDiscussions
IssueAwardEmojis
Jobs
Labels
MergeRequests
MergeRequestAwardEmojis
MergeRequestDiscussions
MergeRequestNotes
Pipelines
PipelineSchedules
PipelineScheduleVariables
Projects
ProjectAccessRequests
ProjectBadges
ProjectCustomAttributes
ProjectImportExport
ProjectIssueBoards
ProjectHooks
ProjectMembers
ProjectMilestones
ProjectSnippets
ProjectSnippetNotes
ProjectSnippetDiscussions
ProjectSnippetAwardEmojis
ProtectedBranches
ProjectVariables
Repositories
RepositoryFiles
Runners
Services
Tags
Todos
Triggers
- UsersBundle which includes:
Users,
UserCustomAttributes,
UserEmails,
UserImpersonationTokens,
UserKeys,
UserGPGKeys
- GroupsBundle which includes:
Groups
GroupAccessRequests
GroupBadges
GroupCustomAttributes
GroupIssueBoards
GroupMembers
GroupMilestones
GroupProjects
GroupVariables
Epics
EpicIssues
EpicNotes
EpicDiscussions
This package uses the Request library by default, which is built into Node. However, if your code is running in a browser, you can get better built-in resolution of proxies and self-signed certificates by using the browser's XMLHttpRequest implementation instead:
import Gitlab from 'gitlab';
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456', // Can be created in your profile.
useXMLHttpRequest: true // Use the browser's XMLHttpRequest instead of Node's Request library
})
WARNING: Currently this option does not support the multipart/form-data
content type, and therefore the endpoint for uploading a file to a project will not work correctly. All other endpoints should work exactly as expected.
Once you have your library instantiated, you can utilize many of the API's functionality:
Using the await/async method
import Gitlab from 'gitlab';
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
});
// Listing users
let users = await api.Users.all();
// Or using Promise-Then notation
api.Projects.all()
.then((projects) => {
console.log(projects)
})
General rule about all the function parameters:
- If its a required parameter, it is a named argument in the functions
- If its an optional parameter, it is defined in a options object following the named arguments
ie.
import Gitlab from 'gitlab';
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
});
api.Projects.create(projectId, {
//options defined in the Gitlab API documentation
})
For any .all() function on a resource, it will return all the items from Gitlab. This can be troublesome if there are many items, as the request it self can take a while to be fulfilled. As such, a maxPages option can be passed to limit the scope of the all function.
import Gitlab from 'gitlab';
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
});
let projects = await api.Projects.all({maxPages:2});
You can also use this in conjunction to the perPage argument which would override the default of 30 per page set by Gitlab:
import Gitlab from 'gitlab';
const api = new Gitlab({
url: 'http://example.com', // Defaults to http://gitlab.com
token: 'abcdefghij123456' // Can be created in your profile.
});
let projects = await api.Projects.all({maxPages:2, perPage:40});
Additionally, if you would like to get back the pagination information, to know how many total pages there are for example, pass the pagination option:
...
let { data, pagination } = await api.Projects.all({ perPage:40, maxPages:2, showPagination: true });
...
This will result in a response in this format:
data: [
...
],
pagination: {
total: 20,
next: 4,
current: 2,
previous: 1,
perPage: 3,
totalPages: 3,
}
With the success of this library thanks to the community, this has become the main npm package to interact with the Gitlab API. As such, there will be a little bit of growing pains for those upgrading from the original node-gitlab v1.8 to our newest 3.0.0 release, far too many to list here. I hope the library is written clearly enough to ease this transition, but if there is anything that you're having trouble with please feel free to create an issue! If not myself, someone will definitely have the answer to help get you all setup up as quickly as possible.
Although there are the official docs for the API, there are some extra goodies offered by this package! After the 3.0.0 release, the next large project will be putting together proper documentation for these goodies [#39]! Stay tuned!!
To get this running locally rather than from your node_modules
folder:
$ git clone https://github.com/jdalrymple/node-gitlab.git
$ cd node-gitlab
$ npm install
$ npm build
And then inside whatever project you are using node-gitlab
in you change your references to use that repo. In your package.json of that upstream project change:
"dependencies": {
...
"node-gitlab": "2.1.0"
...
}
to this
"dependencies": {
...
"node-gitlab": "<path-to-your-clone>"
...
}
Testing is a work-in-progress right now but here is the start.
- First run Gitlab in a docker container:
docker-compose -f docker-compose.test.yml up
- Once GitLab is up on localhost:8080, get the two environment variables from the docker image could either export them into environment variables locally:
- export PERSONAL_ACCESS_TOKEN=$(docker exec -it gitlab bash -lc 'printf "%q" "${PERSONAL_ACCESS_TOKEN}"')
- export GITLAB_URL=$(docker exec -it gitlab bash -lc 'printf "%q" "${GITLAB_URL}"')
- Now run the tests
npm run test
You can also define them in front of the npm script
PERSONAL_ACCESS_TOKEN='abcdefg' GITLAB_URL='http://localhost:8080' npm run test
Note it may take about 3 minutes to get the variables while Gitlab is starting up in the container
npm install -g node-gitlab
# ES6 (>=node 8.0.0)
gitlab
// ES5
gitlabe5
This started off as a fork from node-gitlab but I ended up rewriting much of the code. Here are the original work's contributors.
- Dylan DesRosier
- Mike Wyatt
- Cory Zibeill
- Martin Bour
- Christoph Lehmann
- Frank V
- Salim Benabbou
- Tamás Török-Vistai
- Martin Benninger
- Adam Dehnel
- fewieden
- Jeff Pelton
- Claude Abounegm
- Stefan Hall
- Jordan Wallet
- Ev Haus
- zhao0
- Joshua Grosso
- Frédéric Boutin
- Isaac Ouellet Therrien
- Pavel Birukov
- Sharma-Rajat
- Joseph Petersen
- Igor Katsuba
- Michael Townsend
- bodtx
- Artem
- Munif Tanjim
- Max Wittig
- Quentin Dreyer
- Norm MacLennan
- jnovick
- Fabian Aussems
- mdsb100