跳到主要内容

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、codeupstring是-
config密钥信息配置GitConfig是-

GitConfig​

github 和 gitee

参数说明类型必填默认值
access_token私人令牌string是-

gitlab

参数说明类型必填默认值
access_token私人令牌string是-
endpoint访问域名地址string是-

codeup

参数说明类型必填默认值
access_token私人令牌string是-
accessKeyIdaliyun 密钥 accessKeyIdstring是-
accessKeySecretaliyun 密钥 accessKeySecretstring是-
securityTokenaliyun 密钥 securityTokenstring否-

方法​

获取用户的仓库 listRepos​

github 接口文档
gitee 接口文档
codeup 接口文档

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>

参数类型说明
idnumber仓库唯一 id 值
namestring仓库名称
urlstring仓库访问地址
avatar_urlstring头像地址
ownerstring仓库拥有者
privateboolean是否私有
descriptionstring仓库描述
default_branchstring默认分支
sourceany各自平台返回的数据源

获取用户的仓库 listBranches​

github 接口文档
gitee 接口文档
codeup 接口文档
gitlab 接口文档

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代码库 IDnumber是-

返回值​

返回示例

[
{
"name": "xxxxxxxx",
"commit_sha": "xxxxxxxx",
"source": {}
}
]

详细描述

返回类型为 Array<ListBranches>

参数类型说明
namestring分支名称
commit_shastring最近一次提交 ID
sourceany各自平台返回的数据源

获取传入 Branch/Tag 最近一次 Commit getRefCommit​

github 接口文档
gitee 获取 branch 、 gitee 获取 tag

gitee、github 示例​

await provider.getRefCommit({
owner: "xxxxxx",
repo: "xxxxxx",
ref: "refs/heads/xxx", // 'refs/tags/xxx'
});

参数解析

参数说明类型必填默认值
owner仓库拥有者string是-
repo仓库名称string是-
refref 参数。仅支持 refs/tags/ 和 ref/heads/ 开头string是-

codeup、gitlab 暂未支持​

返回值​

返回示例

{
"name": "xxxxxxxx",
"commit_sha": "xxxxxxxx",
"source": {}
}

详细描述

参数类型说明
namestring分支名称
commit_shastring最近一次提交 ID
sourceany各自平台返回的数据源

通过 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提交信息的 idstring是-

codeup 示例​

await provider.getCommitById({
project_id: 00000,
organization_id: "xxxxxxxx",
sha: "xxxxx",
});

参数解析

参数说明类型必填默认值
organization_id企业标识,也称企业 id。string是-
project_id代码库 IDnumber是-
sha提交信息的 idstring是-

返回值​

返回示例

{
"message": "xxxxxxxx",
"sha": "xxxxxxxx",
"source": {}
}

详细描述

参数类型说明
messagestring提交的描述信息
shastring提交信息的 id
sourceany各自平台返回的数据源

获取 webhook 列表 listWebhook​

github 接口文档
gitee 接口文档

gitee、github 示例​

await provider.listWebhook({
owner: "xxxxxx",
repo: "xxxxxx",
});

参数解析

参数说明类型必填默认值
owner仓库拥有者string是-
repo仓库名称string是-

codeup、gitlab 暂未支持​

返回值​

返回示例

[
{
"id": 0000,
"url": "xxxxxxxx",
"source": {}
}
]

详细描述

返回类型为 Array<ListWebhook>

参数类型说明
idnumberwebhook Id 值
urlstringurl 配置信息
sourceany各自平台返回的数据源

创建 webhook createWebhook​

github 接口文档
gitee 接口文档

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": {}
}

详细描述

参数类型说明
idnumberwebhook Id 值
sourceany各自平台返回的数据源

获取 webhook 信息 getWebhook​

github 接口文档
gitee 接口文档

gitee、github 示例​

await provider.getWebhook({
owner: "xxxx",
repo: "xxxx",
hook_id: 00000,
});

参数解析

参数说明类型必填默认值
owner仓库拥有者string是-
repo仓库名称string是-
hook_idwebhook 的 IDstring是-

codeup、gitlab 暂未支持​

返回值​

返回示例

{
"id": 0000,
"url": "xxxxxxxx",
"source": {}
}

详细描述

参数类型说明
idnumberwebhook Id 值
urlstring触发的 webhook 地址
sourceany各自平台返回的数据源

修改 webhook updateWebhook​

github 接口文档
gitee 接口文档

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_idwebhook 的 IDstring是-
url触发的 webhook 地址string是-
secret签名密钥string否-
events触发的事件,默认 ['push', 'release']Array<'push' | 'release' | 'pull_request' | 'issues'>否-

codeup、gitlab 暂未支持​

返回值​

无

删除 webhook deleteWebhook​

github 接口文档
gitee 接口文档

gitee、github 示例​

await provider.deleteWebhook({
owner: "xxxx",
repo: "xxxx",
hook_id: 00000,
});

参数解析

参数说明类型必填默认值
owner仓库拥有者string是-
repo仓库名称string是-
hook_idwebhook 的 IDstring是-

codeup、gitlab 暂未支持​

返回值​

无

推送文件 putFile​

github 接口文档

github 示例​

await provider.putFile({
owner: "xxxx",
repo: "xxxx",
path: "filename.txt",
message: "commit message",
content: "content",
});

参数解析

参数说明类型必填默认值
owner仓库拥有者string是-
repo仓库名称string是-
path创建文件的路径string是-
messagecommit 信息string是-
content推送的内容信息string是-
branch推送的分支string否主分支

gitee、codeup、gitlab 暂未支持​

返回值​

无

获取组织信息 listOrgs​

github 接口文档
gitee 接口文档

gitee、github 示例​

await provider.listOrgs();

codeup、gitlab 暂未支持​

返回值​

返回示例

[
{
"org": "xxxxxxxx",
"id": 00000,
"source": {}
}
]

详细描述

返回类型为 Array<ListOrgs>

参数类型说明
idnumber组织 id
orgstring组织名称
sourceany各自平台返回的数据源

获取组织的仓库 listOrgRepos​

github 接口文档

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>

参数类型说明
idnumber仓库唯一 id 值
namestring仓库名称
urlstring仓库访问地址
avatar_urlstring头像地址
ownerstring仓库拥有者
privateboolean是否私有
descriptionstring仓库描述
default_branchstring默认分支
sourceany各自平台返回的数据源

fork一个仓库 createFork​

github 接口文档
gitee 接口文档
gitlab 接口文档

github、gitee、gitlab 示例​

await provider.createFork({
owner: "xxxxxx",
repo: "xxxxxx",
});

参数解析

参数说明类型必填默认值
owner仓库所属空间地址string是-
repo仓库名称string是-

codeup 暂未支持​

返回值​

返回示例

{
"id": 0000,
"full_name": "xxxx",
"url": "xxxx",
}

详细描述

参数类型说明
idnumber仓库唯一 id 值
full_namestring仓库名称
urlstring仓库访问地址

创建一个仓库 createRepo​

github 接口文档
gitee 接口文档
gitlab 接口文档
codeup 接口文档

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",
}

详细描述

参数类型说明
idnumber仓库唯一 id 值
full_namestring仓库名称
urlstring仓库访问地址

获取一个仓库信息 hasRepo​

github 接口文档
gitee 接口文档
gitlab 接口文档
codeup 接口文档

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代码库 IDnumber是-

返回值​

返回示例

{
"id": 0000,
"full_name": "xxxx",
"url": "xxxx",
}

详细描述

参数类型说明
idnumber仓库唯一 id 值
full_namestring仓库名称
urlstring仓库访问地址

删除一个仓库 deleteRepo​

github 接口文档
gitee 接口文档
gitlab 接口文档
codeup 接口文档

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代码库 IDnumber是-
reason删除代码库原因string否-

返回值​

无

创建保护分支 setProtectionBranch​

github 接口文档
gitee 接口文档
gitlab 接口文档
codeup 接口文档

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代码库 IDnumber是-

返回值​

返回示例

无

获取分支是否为保护分支 getProtectionBranch​

github 接口文档
gitee 接口文档
gitlab 接口文档
codeup 接口文档

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代码库 IDnumber是-

返回值​

返回示例

{
"protected": true,
}

详细描述

参数类型说明
protectedboolean分支是否为保护分支