跳到主要内容

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供应商。目前仅支持 githubgiteegitlabcodeupstring-
config密钥信息配置GitConfig-

GitConfig

githubgitee

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

gitlab

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

codeup

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

方法

获取用户的仓库 listRepos

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

giteegithub 示例

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 接口文档

giteegithubgitlab 示例

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

giteegithub 示例

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

参数解析

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

codeupgitlab 暂未支持

返回值

返回示例

{
"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 接口文档

giteegithubgitlab 示例

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 接口文档

giteegithub 示例

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

参数解析

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

codeupgitlab 暂未支持

返回值

返回示例

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

详细描述

返回类型为 Array<ListWebhook>

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

创建 webhook createWebhook

github 接口文档
gitee 接口文档

giteegithub 示例

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']

codeupgitlab 暂未支持

返回值

返回示例

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

详细描述

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

获取 webhook 信息 getWebhook

github 接口文档
gitee 接口文档

giteegithub 示例

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

参数解析

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

codeupgitlab 暂未支持

返回值

返回示例

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

详细描述

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

修改 webhook updateWebhook

github 接口文档
gitee 接口文档

giteegithub 示例

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'>-

codeupgitlab 暂未支持

返回值

删除 webhook deleteWebhook

github 接口文档
gitee 接口文档

giteegithub 示例

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

参数解析

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

codeupgitlab 暂未支持

返回值

推送文件 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主分支

giteecodeupgitlab 暂未支持

返回值

获取组织信息 listOrgs

github 接口文档
gitee 接口文档

giteegithub 示例

await provider.listOrgs();

codeupgitlab 暂未支持

返回值

返回示例

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

详细描述

返回类型为 Array<ListOrgs>

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

获取组织的仓库 listOrgRepos

github 接口文档

github 示例

const org = "xxx";
await provider.listOrgRepos(org);

参数解析

参数说明类型必填默认值
org组织名称string-

giteecodeupgitlab 暂未支持

返回值

返回示例

[
{
"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 接口文档

githubgiteegitlab 示例

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 接口文档

githubgitee 示例

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是否私有booleanfalse
description仓库描述string-
visibility仓库可见, 'Private'为私有,'Public'表示公开string'Private'
visibility_level代码仓库可见性 0 - 私有,仅代码库成员可见 10 - 企业内公开,企业成员可见string0
organization_id企业标识,也称企业 id。string-

返回值

返回示例

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

详细描述

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

获取一个仓库信息 hasRepo

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

githubgiteegitlab 示例

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 接口文档

githubgiteegitlab 示例

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 接口文档

githubgiteegitlab 示例

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 接口文档

githubgiteegitlab 示例

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分支是否为保护分支