git 代码托管平台能力(@serverless-cd/git-provider)
针对 github、gitee、gitlab 和阿里云 codeup 做一些特殊的处理,尽可能处理掉不同平台的差异
安装
$ npm install @serverless-cd/git-provider --save
示例
import git from "@serverless-cd/git-provider";
const github = git("github", { access_token: "ghp_xxxx" });
await github.listOrgs();
const gitee = git("gitee", { access_token: "xxxxxxx" });
await gitee.listRepos();
const gitlab = git("gitlab", {
access_token: "xxxxxxx",
endpoint: "https://gitlab.com",
});
const codeup = git("codeup", {
access_token: "xxxxxxx",
accessKeyId: "xxxxxxx",
accessKeySecret: "xxxxxxx",
});
参数解析
const provider = git(git-provider, config);
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| git-provider | 供应商。目前仅支持 github、gitee、gitlab、codeup | string | 是 | - |
| config | 密钥信息配置 | GitConfig | 是 | - |
GitConfig
github 和 gitee
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| access_token | 私人令牌 | string | 是 | - |
gitlab
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| access_token | 私人令牌 | string | 是 | - |
| endpoint | 访问域名地址 | string | 是 | - |
codeup
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| access_token | 私人令牌 | string | 是 | - |
| accessKeyId | aliyun 密钥 accessKeyId | string | 是 | - |
| accessKeySecret | aliyun 密钥 accessKeySecret | string | 是 | - |
| securityToken | aliyun 密钥 securityToken | string | 否 | - |
方法
获取用户的仓库 listRepos
gitee、github 示例
await provider.listRepos();
codeup 示例
await provider.listRepos({ organization_id: "xxx" });
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| organization_id | 企业标识,也称企业 id。可在云效访问链接中获取,如 https://devops.aliyun.com/organization/ | string | 是 | - |
gitlab 暂未支持
返回值
返回示例
[
{
"id": 0000,
"name": "xxxxxxxx",
"url": "xxxxxxxx",
"avatar_url": "xxxxxxxx",
"owner": "xxxxxxxx",
"private": false,
"description": "xxxxxxxx",
"default_branch": "xxxxxxxx",
"source": {}
}
]
详细描述
返回类型为 Array<ListReposRes>
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 仓库唯一 id 值 |
| name | string | 仓库名称 |
| url | string | 仓库访问地址 |
| avatar_url | string | 头像地址 |
| owner | string | 仓库拥有者 |
| private | boolean | 是否私有 |
| description | string | 仓库描述 |
| default_branch | string | 默认分支 |
| source | any | 各自平台返回的数据源 |
获取用户的仓库 listBranches
gitee、github、gitlab 示例
await provider.listBranches({
owner: "xxxxxxxx",
repo: "xxxxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
codeup 示例
await provider.listBranches({
project_id: 00000,
organization_id: "xxxxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
返回值
返回示例
[
{
"name": "xxxxxxxx",
"commit_sha": "xxxxxxxx",
"source": {}
}
]
详细描述
返回类型为 Array<ListBranches>
| 参数 | 类型 | 说明 |
|---|---|---|
| name | string | 分支名称 |
| commit_sha | string | 最近一次提交 ID |
| source | any | 各自平台返回的数据源 |
获取传入 Branch/Tag 最近一次 Commit getRefCommit
gitee、github 示例
await provider.getRefCommit({
owner: "xxxxxx",
repo: "xxxxxx",
ref: "refs/heads/xxx", // 'refs/tags/xxx'
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| ref | ref 参数。仅支持 refs/tags/ 和 ref/heads/ 开头 | string | 是 | - |
codeup、gitlab 暂未支持
返回值
返回示例
{
"name": "xxxxxxxx",
"commit_sha": "xxxxxxxx",
"source": {}
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| name | string | 分支名称 |
| commit_sha | string | 最近一次提交 ID |
| source | any | 各自平台返回的数据源 |
通过 commitId 获取信息 getCommitById
github 接口文档 其中文档存在一些异常:GET /repos/{owner}/{repo}/comments/{sha} => GET /repos/{owner}/{repo}/commits/{sha}
gitee 接口文档
gitee、github、gitlab 示例
await provider.getCommitById({
owner: "xxxxxx",
repo: "xxxxxx",
sha: "xxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| sha | 提交信息的 id | string | 是 | - |
codeup 示例
await provider.getCommitById({
project_id: 00000,
organization_id: "xxxxxxxx",
sha: "xxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
| sha | 提交信息的 id | string | 是 | - |
返回值
返回示例
{
"message": "xxxxxxxx",
"sha": "xxxxxxxx",
"source": {}
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| message | string | 提交的描述信息 |
| sha | string | 提交信息的 id |
| source | any | 各自平台返回的数据源 |
获取 webhook 列表 listWebhook
gitee、github 示例
await provider.listWebhook({
owner: "xxxxxx",
repo: "xxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
codeup、gitlab 暂未支持
返回值
返回示例
[
{
"id": 0000,
"url": "xxxxxxxx",
"source": {}
}
]
详细描述
返回类型为 Array<ListWebhook>
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | webhook Id 值 |
| url | string | url 配置信息 |
| source | any | 各自平台返回的数据源 |
创建 webhook createWebhook
gitee、github 示例
await provider.createWebhook({
owner: "xxxx",
repo: "xxxx",
url: "xxxx",
secret: "xxxxx",
events: ["push", "release", "pull_request", "issues"],
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| url | 触发的 webhook 地址 | string | 是 | - |
| secret | 签名密钥 | string | 否 | - |
| events | 触发的事件 | Array<'push' | 'release' | 'pull_request' | 'issues'> | 否 | ['push', 'release'] |
codeup、gitlab 暂未支持
返回值
返回示例
{
"id": 0000,
"source": {}
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | webhook Id 值 |
| source | any | 各自平台返回的数据源 |
获取 webhook 信息 getWebhook
gitee、github 示例
await provider.getWebhook({
owner: "xxxx",
repo: "xxxx",
hook_id: 00000,
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| hook_id | webhook 的 ID | string | 是 | - |
codeup、gitlab 暂未支持
返回值
返回示例
{
"id": 0000,
"url": "xxxxxxxx",
"source": {}
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | webhook Id 值 |
| url | string | 触发的 webhook 地址 |
| source | any | 各自平台返回的数据源 |
修改 webhook updateWebhook
gitee、github 示例
await provider.updateWebhook({
owner: "xxxx",
repo: "xxxx",
url: "xxxx",
hook_id: 00000,
secret: "xxxxx",
events: ["push", "release", "pull_request", "issues"],
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| hook_id | webhook 的 ID | string | 是 | - |
| url | 触发的 webhook 地址 | string | 是 | - |
| secret | 签名密钥 | string | 否 | - |
| events | 触发的事件,默认 ['push', 'release'] | Array<'push' | 'release' | 'pull_request' | 'issues'> | 否 | - |
codeup、gitlab 暂未支持
返回值
无
删除 webhook deleteWebhook
gitee、github 示例
await provider.deleteWebhook({
owner: "xxxx",
repo: "xxxx",
hook_id: 00000,
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| hook_id | webhook 的 ID | string | 是 | - |
codeup、gitlab 暂未支持
返回值
无
推送文件 putFile
github 示例
await provider.putFile({
owner: "xxxx",
repo: "xxxx",
path: "filename.txt",
message: "commit message",
content: "content",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库拥有者 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| path | 创建文件的路径 | string | 是 | - |
| message | commit 信息 | string | 是 | - |
| content | 推送的内容信息 | string | 是 | - |
| branch | 推送的分支 | string | 否 | 主分支 |
gitee、codeup、gitlab 暂未支持
返回值
无
获取组织信息 listOrgs
gitee、github 示例
await provider.listOrgs();
codeup、gitlab 暂未支持
返回值
返回示例
[
{
"org": "xxxxxxxx",
"id": 00000,
"source": {}
}
]
详细描述
返回类型为 Array<ListOrgs>
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 组织 id |
| org | string | 组织名称 |
| source | any | 各自平台返回的数据源 |
获取组织的仓库 listOrgRepos
github 示例
const org = "xxx";
await provider.listOrgRepos(org);
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| org | 组织名称 | string | 是 | - |
gitee、codeup、gitlab 暂未支持
返回值
返回示例
[
{
"id": 0000,
"name": "xxxxxxxx",
"url": "xxxxxxxx",
"avatar_url": "xxxxxxxx",
"owner": "xxxxxxxx",
"private": false,
"description": "xxxxxxxx",
"default_branch": "xxxxxxxx",
"source": {}
}
]
详细描述
返回类型为 Array<ListOrgRepos>
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 仓库唯一 id 值 |
| name | string | 仓库名称 |
| url | string | 仓库访问地址 |
| avatar_url | string | 头像地址 |
| owner | string | 仓库拥有者 |
| private | boolean | 是否私有 |
| description | string | 仓库描述 |
| default_branch | string | 默认分支 |
| source | any | 各自平台返回的数据源 |
fork一个仓库 createFork
github、gitee、gitlab 示例
await provider.createFork({
owner: "xxxxxx",
repo: "xxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库所属空间地址 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
codeup 暂未支持
返回值
返回示例
{
"id": 0000,
"full_name": "xxxx",
"url": "xxxx",
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 仓库唯一 id 值 |
| full_name | string | 仓库名称 |
| url | string | 仓库访问地址 |
创建一个仓库 createRepo
github、gitee 示例
await provider.createRepo({
name: "xxxx",
private: false,
description: "xxxx"
});
gitlab 示例
await provider.createRepo({
name: "xxxx",
visibility: ["Private", "Public"],
description: "xxxx"
});
codeup 示例
await provider.createRepo({
name: "xxxx",
organization_id: "xxxx",
visibility_level: [0, 10],
description: "xxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| name | 仓库名称 | string | 是 | - |
| private | 是否私有 | boolean | 否 | false |
| description | 仓库描述 | string | 否 | - |
| visibility | 仓库可见, 'Private'为私有,'Public'表示公开 | string | 否 | 'Private' |
| visibility_level | 代码仓库可见性 0 - 私有,仅代码库成员可见 10 - 企业内公开,企业成员可见 | string | 否 | 0 |
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
返回值
返回示例
{
"id": 0000,
"full_name": "xxxx",
"url": "xxxx",
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 仓库唯一 id 值 |
| full_name | string | 仓库名称 |
| url | string | 仓库访问地址 |
获取一个仓库信息 hasRepo
github、gitee、gitlab 示例
await provider.hasRepo({
owner: "xxxxxx",
repo: "xxxxxx",
});
codeup 示例
await provider.hasRepo({
project_id: 00000,
organization_id: "xxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库所属空间地址 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
返回值
返回示例
{
"id": 0000,
"full_name": "xxxx",
"url": "xxxx",
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| id | number | 仓库唯一 id 值 |
| full_name | string | 仓库名称 |
| url | string | 仓库访问地址 |
删除一个仓库 deleteRepo
github、gitee、gitlab 示例
await provider.deleteRepo({
owner: "xxxxxx",
repo: "xxxxxx",
});
codeup 示例
await provider.deleteRepo({
project_id: 00000,
organization_id: "xxxxxx",
reason: "xxxx"
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库所属空间地址 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
| reason | 删除代码库原因 | string | 否 | - |
返回值
无
创建保护分支 setProtectionBranch
github、gitee、gitlab 示例
await provider.setProtectionBranch({
owner: "xxxxxx",
repo: "xxxxxx",
branch: "xxxxx",
});
codeup 示例
await provider.hasRepo({
project_id: 00000,
organization_id: "xxxxxx",
branch: "xxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库所属空间地址 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| branch | 分支名称 | string | 是 | - |
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
返回值
返回示例
无
获取分支是否为保护分支 getProtectionBranch
github、gitee、gitlab 示例
await provider.getProtectionBranch({
owner: "xxxxxx",
repo: "xxxxxx",
branch: "xxxxx",
});
codeup 示例
await provider.hasRepo({
project_id: 00000,
organization_id: "xxxxxx",
branch: "xxxxxx",
});
参数解析
| 参数 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| owner | 仓库所属空间地址 | string | 是 | - |
| repo | 仓库名称 | string | 是 | - |
| branch | 分支名称 | string | 是 | - |
| organization_id | 企业标识,也称企业 id。 | string | 是 | - |
| project_id | 代码库 ID | number | 是 | - |
返回值
返回示例
{
"protected": true,
}
详细描述
| 参数 | 类型 | 说明 |
|---|---|---|
| protected | boolean | 分支是否为保护分支 |