在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jdalrymple/gitbeaker开源软件地址:https://github.com/jdalrymple/gitbeaker开源编程语言:TypeScript 98.8%开源软件介绍:GitLab API NodeJS library with full support of all the Gitlab API services. Table of ContentsInstall# Install from npm
npm install @gitbeaker/node # NodeJS default, index.es.js for esm
npm install @gitbeaker/browser # UMD default
npm -g install @gitbeaker/cli # CLI
Getting StartedNodeJS // ES6
import { Gitlab } from '@gitbeaker/node'; // All Resources
import { Projects } from '@gitbeaker/node'; // Just the Project Resource
//...etc
// ES5
const { Gitlab } = require('@gitbeaker/node'); Browser // ES6
import { Gitlab } from '@gitbeaker/browser'; // All Resources
import { Projects } from '@gitbeaker/browser'; // Just the Project Resource
//...etc
// ES5
const { Gitlab } = require('@gitbeaker/browser'); OR through the script tag: <script src="https://unpkg.com/@gitbeaker/browser/dist/index.es.js" />
<script>
const { Gitlab } = gitbeaker;
</script> CLI The CLI export functions in a similar manner, following the pattern: gitbeaker [service name] [method name] --config_args pos_arg1 pos_arg2 --opts_arg1 Where:
There is one small exception with the instantiating arguments, however, which must be supplied using a # To get all the projects
gitbeaker projects all --gb-token="personaltoken"
# To get a project with id = 2
gitbeaker projects show --gl-token="personaltoken" 2 To reduce the annoyance of having to pass those configuration properties each time, it is also possible to pass the token and host information through environment variables in the form of GITLAB_HOST=http://example.com
GITLAB_TOKEN=personaltoken
GITBEAKER_CAMELIZE=true This could be set globally or using a .env file in the project folder. Instantiation Instantiate the library using a basic token created in your Gitlab Profile const api = new Gitlab({
token: 'personaltoken',
}); Available instantiating options:
Typing SupportAll the exposed types are exported though the import { Types } from '@gitbeaker/node'; DocsAlthough there are the official docs for the API, there are some extra goodies offered by this package! The next large project will be putting together proper documentation for these goodies [#39]! Stay tuned!! Supported APIsThe API's that are currently supported are:
ExamplesOnce you have your library instantiated, you can utilize many of the API's functionality: Using the await/async method import { Gitlab } from '@gitbeaker/node';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
// Listing users
let users = await api.Users.all();
// Or using Promise-Then notation
api.Projects.all().then((projects) => {
console.log(projects);
}); A general rule about all the function parameters:
ie. import { Gitlab } from '@gitbeaker/node';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
api.Projects.create({
//options defined in the Gitlab API documentation
}); PaginationAvailable pagination options:
Offset PaginationFor 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 itself 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({
host: 'http://example.com',
token: 'personaltoken',
});
let projects = await api.Projects.all({ maxPages: 2 }); You can also use this in conjunction with the perPage argument which would override the default of 30 per page set by Gitlab: import { Gitlab } from 'gitlab';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
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 option ...
const { data, paginationInfo } = await api.Projects.all({
perPage:40,
maxPages:2,
showExpanded: true
});
... This will result in a response in this format: data: [
...
],
paginationInfo: {
next: 4,
current: 2,
previous: 1,
perPage: 3,
}
Keyset PaginationSimilarly, support for Keyset pagination can be toggled on by passing a pagination parameter as a query option const { data } = await api.Projects.all({
pagination: 'keyset',
}); Note that for keyset pagination, SudoFor private gitlab instances, administrators can impersonate users through the API. To do so, you have to set the 'Sudo' header on the services you want to impersonate the user for. For example, if you want to disable notifications for a specific user: import { NotificationSettings } from 'gitlab';
const service = new NotificationSettings({
host: 'http://example.com',
token: 'personaltoken'
sudo: 8 // Can be the user ID or a username
});
await service.edit({
level: NotificationSettings.LEVELS.DISABLED
}) Custom Request LibrariesThere is another constructor parameter that allows the user to specify their custom request library
as long as it has a similar API to ky. To specify the library, simply set the An example can be seen in the KyRequester.ts file import { Gitlab } from 'gitlab';
import YourCustomRequester from 'custom-requester';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
requester: YourCustomRequester,
}); MiscHandling HTTPS certificatesIf your Gitlab server is running via HTTPS, the proper way to pass in your certificates is via a "scripts": {
"start": "NODE_EXTRA_CA_CERTS=./secrets/3ShapeCA.pem node bot.js"
},
Non JSON/Text ResponsesFor responses such as file data that may be returned from the API, the data is exposed as a buffer. For example, when trying to write a file, this can be done like: let bufferedData = await api.Jobs.downloadLatestArtifactFile(project.id, 'test', 'job_test');
fs.writeFileSync('test.zip', bufferedData); DebuggingDepending on the library used, the full information about the request error can be a bit obfuscated. Ideally, the entire information is returned from Ky (browser) or Got (nodejs) however for simplicity, a description property is added to the error object that has the error message attached. Simply look for e.description to have a better idea of what the error actually is. TestingTesting is a work-in-progress right now but here is the start. Unit Tests Run: yarn test:unit Integration Tests
docker-compose -f scripts/docker-compose.yml up
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}"')
yarn test:integration:node You can also define them in front of the yarn script
ContributorsThis started as a fork from node-gitlab-legacy but I ended up rewriting much of the code. Here are the original work's contributors.
全部评论
专题导读
上一篇:mirror/scintilla: Scintilla Git mirror发布时间:2022-06-11下一篇:glandium/git-cinnabar: git remote helper to interact with mercurial repositories发布时间:2022-06-11热门推荐
热门话题
阅读排行榜
|
请发表评论