84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package git
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
)
|
|
|
|
var cli *gitea.Client
|
|
|
|
func init() {
|
|
var err error
|
|
// cli, err = gitea.NewClient("http://gitea:3000/", gitea.SetToken("4322b0d361004db5dcea1741417f9e014a0f428f"))
|
|
cli, err = gitea.NewClient("https://git.icechen.cn", gitea.SetToken("4322b0d361004db5dcea1741417f9e014a0f428f"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// DefaultRealConfigFilePath 默认原始配置文件路径
|
|
const DefaultRealConfigFilePath = ".drone.yml"
|
|
|
|
// Client git 客户端
|
|
type Client struct {
|
|
namespace string
|
|
repo string
|
|
ref string
|
|
realConfigPath string
|
|
}
|
|
|
|
// New 创建 git 客户端
|
|
func New(namespace string, repo string, ref string, configPath string) Client {
|
|
return Client{
|
|
namespace: namespace,
|
|
repo: repo,
|
|
ref: ref,
|
|
realConfigPath: configPath,
|
|
}
|
|
}
|
|
|
|
// GetFileContent 获取文件内容
|
|
func (c Client) GetFileContent(filepath string) (string, error) {
|
|
ret, resp, err := cli.GetContents(c.namespace, c.repo, c.ref, filepath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", errors.New("gitea fail")
|
|
}
|
|
|
|
fileContent, err := base64.StdEncoding.DecodeString(*ret.Content)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(fileContent), nil
|
|
}
|
|
|
|
// GetRealConfig 获取原始配置文件
|
|
func (c Client) GetRealConfig() (string, error) {
|
|
filePath := DefaultRealConfigFilePath
|
|
if c.realConfigPath == "" {
|
|
filePath = c.realConfigPath
|
|
}
|
|
return c.GetFileContent(filePath)
|
|
}
|
|
|
|
func (c Client) GetCommitModifiedFileList() ([]string, error) {
|
|
commit, resp, err := cli.GetSingleCommit(c.namespace, c.repo, c.ref)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, errors.New("gitea fail")
|
|
}
|
|
|
|
modifiedFileList := make([]string, len(commit.Files))
|
|
for i, file := range commit.Files {
|
|
modifiedFileList[i] = file.Filename
|
|
}
|
|
return modifiedFileList, nil
|
|
}
|