1.在开始调用之前需要做些准备工作:我们必须得有一个访问Token才能调用API,在gitlab上创建好之后保存下来就可以了。
2.接下来我们得先创建一个gitlab实例:
js
import { Gitlab } from '@gitbeaker/rest';
const gitlab = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
创建一个代码仓库
js
async function createProject() {
const res = await gitlab.Projects.create({
name: 'xxxx', //项目名称,必填
description: 'New Project',
namespace_id: '你自己在Gitlab上的组ID',
visibility: 'private', //默认是私有项目,值有“private”和“public”
initialize_with_readme: true, //是否自动创建一个readme文件
}).catch(error => {
console.error('项目创建失败:', error);
});
return res.id;
}
创建一个代码文件
js
async function createNewFile() {
const projectId = '项目ID';
const filePath = 'test.ts';//文件路径
const branch = 'master'; //分支名称
const content = 'console.log("Hello, World!");'; //文件内容
const commitMessage = 'Add test.ts'; //提交信息
const res = await gitlab.RepositoryFiles.create(
projectId,
filePath,
branch,
content,
commitMessage,
).catch((error) => {
console.error("添加文件失败:", error);
});
return res;
}
编辑代码文件
js
async function updateFile() {
const projectId = '项目ID';
const filePath = 'test.ts';//文件路径
const branch = 'master'; //分支名称
const content = 'console.log("Hello, World!");--------更新内容------------'; //文件内容
const commitMessage = 'Update test.ts'; //提交信息
const res = await gitlab.RepositoryFiles.edit(
projectId,
filePath,
branch,
content,
commitMessage,
).catch((error) => {
console.error("更新文件代码失败:", error);
});
return res;
}
删除文件
js
async function deleteFile() {
return gitlab.RepositoryFiles.remove(
projectId,
filePath,
branch,
commitMessage
).catch((error) => {
console.error("删除文件代码失败:", error);
});
}
提交一个commit
js
async function commit() {
const actions = [
{
"action": "create",// 创建一个文件
"filePath": 'a.ts',
"content": 'console.log("Hello, World!");'
},
{
"action": "delete",// 删除一个文件
"filePath": 'b.ts'
},
]
return gitlab.Commits.create(
projectId,
branch,
message,
actions
).catch((error) => {
console.error("Commit提交失败:", error);
});
}
使用示例
分页
可用的分页选项:
姓名 | 键盘 | 抵消 | 类型 | 默认 | 描述 |
---|---|---|---|---|---|
pagination | 十 | 十 | ‘偏移量’ 或 ‘键集’ | '抵消' | 定义应使用哪种分页类型 |
perPage | 十 | 十 | 数字 | 20 | 每个请求的结果数量 |
orderBy | 十 | 细绳 | 结果应按哪个字段排序 | ||
sort | 十 | ‘asc’ 或 ‘desc’ | ‘升序’ | 结果排序的方向 | |
maxPages | 十 | 数字 | 不适用 | 应发出的最大请求数量 | |
page | 十 | 数字 | 不适用 | 要检索的特定页面 | |
showExpanded | 十 | 布尔值 | 错误的 | 除了数据外,还返回分页信息 |
偏移分页
对于资源上的任何 .all() 函数,它将返回Gitlab 中的所有项目。如果项目很多,这可能会很麻烦,因为请求本身可能需要一段时间才能完成。因此,可以传递 maxPages 选项来限制 all 函数的范围。
import { Gitlab } from '@gitbeaker/rest';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
let projects = await api.Projects.all({ maxPages: 2 });
您还可以将其与 perPage 参数结合使用,以覆盖 Gitlab 设置的每页 30 个的默认值:
import { Gitlab } from '@gitbeaker/rest';
const api = new Gitlab({
host: 'http://example.com',
token: 'personaltoken',
});
let projects = await api.Projects.all({ maxPages: 2, perPage: 40 });
此外,如果您想要获取分页信息,例如要知道总共有多少页,请传递选项showExpanded
。如果有多个结果,则将包含分页属性,如下所示:
...
const { data, paginationInfo } = await api.Projects.all({
perPage:40,
maxPages:2,
showExpanded: true
});
...
这将产生以下格式的响应:
data: [
...
],
paginationInfo: {
next: 4,
current: 2,
previous: 1,
perPage: 3,
}
注意:提供任何分页限制都需要大量调用。某些资源需要大量请求,这会给 Gitlab 服务器带来很大负担。一般最佳做法是将页面请求选项设置为,如果不需要所有结果,则仅返回第一页。