Files

373 lines
9.9 KiB
Go

package gitee
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/chaitin/MonkeyCode/backend/pkg/request"
)
// giteeAuthOpts 根据 isOAuth 返回认证相关的请求选项和 query 参数
func giteeAuthOpts(token string, isOAuth bool, query map[string]string) []request.Opt {
if isOAuth {
query["access_token"] = token
return nil
}
return []request.Opt{request.WithHeader(request.Header{"Authorization": "token " + token})}
}
// GetRepoDescription 获取仓库描述
func (g *Gitee) GetRepoDescription(ctx context.Context, token, repoURL string, isOAuth bool) (string, error) {
owner, repo, err := parseGiteeRepoPath(repoURL)
if err != nil {
return "", err
}
path := fmt.Sprintf("/api/v5/repos/%s/%s", owner, repo)
query := map[string]string{}
client := g.newRequestClientForToken(token)
opts := []request.Opt{request.WithQuery(query)}
opts = append(opts, giteeAuthOpts(token, isOAuth, query)...)
repoInfo, err := request.Get[Repository](client, ctx, path, opts...)
if err != nil {
return "", fmt.Errorf("get repo: %w", err)
}
return repoInfo.Description, nil
}
// GetRepoTree 获取仓库文件树
func (g *Gitee) GetRepoTree(ctx context.Context, token, owner, repo, ref, treePath string, recursive, isOAuth bool) (*GetRepoTreeResp, error) {
if ref == "" {
ref = "master"
}
client := g.newRequestClientForToken(token)
apiPath := fmt.Sprintf("/api/v5/repos/%s/%s/git/trees/%s", owner, repo, url.PathEscape(ref))
query := map[string]string{}
if recursive || treePath != "" {
query["recursive"] = "1"
}
type giteeTreeNode struct {
Path string `json:"path"`
Mode string `json:"mode"`
Type string `json:"type"`
Sha string `json:"sha"`
Size int `json:"size"`
}
type giteeTreeResp struct {
Sha string `json:"sha"`
Tree []*giteeTreeNode `json:"tree"`
}
opts := []request.Opt{request.WithQuery(query)}
opts = append(opts, giteeAuthOpts(token, isOAuth, query)...)
treeResp, err := request.Get[giteeTreeResp](client, ctx, apiPath, opts...)
if err != nil {
return nil, fmt.Errorf("get tree: %w", err)
}
prefix := ""
if treePath != "" {
prefix = strings.TrimSuffix(treePath, "/") + "/"
}
entries := make([]*TreeEntry, 0, len(treeResp.Tree))
for _, node := range treeResp.Tree {
entryPath := node.Path
if prefix != "" && !strings.HasPrefix(entryPath, prefix) {
continue
}
if prefix != "" && !recursive {
rel := strings.TrimPrefix(entryPath, prefix)
if rel == "" || strings.Contains(rel, "/") {
continue
}
}
mode := giteeTypeToMode(node.Type)
name := baseName(entryPath)
entries = append(entries, &TreeEntry{
Mode: mode,
Name: name,
Path: entryPath,
Sha: node.Sha,
Size: node.Size,
})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].Path < entries[j].Path
})
return &GetRepoTreeResp{
Entries: entries,
SHA: treeResp.Sha,
}, nil
}
// GetBlob 获取单文件内容
func (g *Gitee) GetBlob(ctx context.Context, token, owner, repo, ref, path string, isOAuth bool) (*GetBlobResp, error) {
client := g.newRequestClientForToken(token)
parts := strings.Split(strings.Trim(path, "/"), "/")
for i, part := range parts {
parts[i] = url.PathEscape(part)
}
escapedPath := strings.Join(parts, "/")
apiPath := fmt.Sprintf("/api/v5/repos/%s/%s/contents/%s", owner, repo, escapedPath)
query := map[string]string{}
if ref != "" {
query["ref"] = ref
}
type giteeContentResp struct {
Content string `json:"content"`
Encoding string `json:"encoding"`
Sha string `json:"sha"`
Size int `json:"size"`
}
opts := []request.Opt{request.WithQuery(query)}
opts = append(opts, giteeAuthOpts(token, isOAuth, query)...)
contentResp, err := request.Get[giteeContentResp](client, ctx, apiPath, opts...)
if err != nil {
return nil, fmt.Errorf("get file content: %w", err)
}
var content []byte
if contentResp.Encoding == "base64" {
cleaned := strings.ReplaceAll(contentResp.Content, "\n", "")
content, err = base64.StdEncoding.DecodeString(cleaned)
if err != nil {
return nil, fmt.Errorf("decode content: %w", err)
}
} else {
content = []byte(contentResp.Content)
}
return &GetBlobResp{
Content: content,
IsBinary: isBinaryContent(content),
Sha: contentResp.Sha,
Size: contentResp.Size,
}, nil
}
// GetGitLogs 获取提交历史
func (g *Gitee) GetGitLogs(ctx context.Context, token, owner, repo, ref, path string, limit, offset int, isOAuth bool) (*GetGitLogsResp, error) {
client := g.newRequestClientForToken(token)
if limit <= 0 {
limit = 100
}
page := (offset / limit) + 1
apiPath := fmt.Sprintf("/api/v5/repos/%s/%s/commits", owner, repo)
query := map[string]string{
"page": fmt.Sprintf("%d", page),
"per_page": fmt.Sprintf("%d", limit),
}
if ref != "" {
query["sha"] = ref
}
if path != "" {
query["path"] = path
}
type giteeCommitUser struct {
Name string `json:"name"`
Email string `json:"email"`
Date string `json:"date"`
}
type giteeCommitDetail struct {
Author *giteeCommitUser `json:"author"`
Committer *giteeCommitUser `json:"committer"`
Message string `json:"message"`
Tree struct {
Sha string `json:"sha"`
} `json:"tree"`
}
type giteeCommitParent struct {
Sha string `json:"sha"`
}
type giteeCommitResp struct {
Sha string `json:"sha"`
Commit *giteeCommitDetail `json:"commit"`
Parents []*giteeCommitParent `json:"parents"`
}
opts := []request.Opt{request.WithQuery(query)}
opts = append(opts, giteeAuthOpts(token, isOAuth, query)...)
commits, err := request.Get[[]giteeCommitResp](client, ctx, apiPath, opts...)
if err != nil {
return nil, fmt.Errorf("list commits: %w", err)
}
pagedCommits := *commits
skip := offset % limit
if skip > 0 {
if skip >= len(pagedCommits) {
pagedCommits = nil
} else {
pagedCommits = pagedCommits[skip:]
}
}
entries := make([]*GitCommitEntry, 0, len(pagedCommits))
for _, c := range pagedCommits {
entry := &GitCommitEntry{
Commit: &GitCommit{
Sha: c.Sha,
Message: c.Commit.Message,
TreeSha: c.Commit.Tree.Sha,
ParentShas: make([]string, 0, len(c.Parents)),
},
}
if c.Commit.Author != nil {
entry.Commit.Author = &GitUser{
Name: c.Commit.Author.Name,
Email: c.Commit.Author.Email,
When: parseGiteeDate(c.Commit.Author.Date),
}
}
if c.Commit.Committer != nil {
entry.Commit.Committer = &GitUser{
Name: c.Commit.Committer.Name,
Email: c.Commit.Committer.Email,
When: parseGiteeDate(c.Commit.Committer.Date),
}
}
for _, p := range c.Parents {
entry.Commit.ParentShas = append(entry.Commit.ParentShas, p.Sha)
}
entries = append(entries, entry)
}
return &GetGitLogsResp{
Count: len(entries),
Entries: entries,
}, nil
}
// GetRepoArchive 获取仓库压缩包
func (g *Gitee) GetRepoArchive(ctx context.Context, token, owner, repo, ref string, isOAuth bool) (*GetRepoArchiveResp, error) {
if ref == "" {
ref = "master"
}
base := strings.TrimSuffix(g.baseURL, "/")
apiURL := fmt.Sprintf("%s/api/v5/repos/%s/%s/tarball?ref=%s",
base, owner, repo, url.QueryEscape(ref))
if isOAuth {
apiURL += "&access_token=" + url.QueryEscape(token)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
if !isOAuth {
req.Header.Set("Authorization", "token "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("get archive: %w", err)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return nil, fmt.Errorf("get archive failed with status %d: %s", resp.StatusCode, string(body))
}
return &GetRepoArchiveResp{
ContentLength: resp.ContentLength,
ContentType: "application/gzip",
Reader: resp.Body,
}, nil
}
// ListBranches 获取 Gitee 仓库分支列表
func ListBranches(ctx context.Context, token, owner, repo string, page, perPage int, isOAuth bool) ([]*BranchInfo, error) {
client := &http.Client{Timeout: 30 * time.Second}
apiURL := fmt.Sprintf("https://gitee.com/api/v5/repos/%s/%s/branches?page=%d&per_page=%d",
url.PathEscape(owner), url.PathEscape(repo), page, perPage)
if isOAuth {
apiURL += "&access_token=" + url.QueryEscape(token)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
if !isOAuth {
req.Header.Set("Authorization", "token "+token)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request gitee branches api: %w", err)
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("gitee branches api returned status %d: %s", resp.StatusCode, string(body))
}
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}
type giteeBranch struct {
Name string `json:"name"`
}
var branches []giteeBranch
if err := json.Unmarshal(body, &branches); err != nil {
return nil, fmt.Errorf("unmarshal branches: %w", err)
}
result := make([]*BranchInfo, 0, len(branches))
for _, b := range branches {
result = append(result, &BranchInfo{Name: b.Name})
}
return result, nil
}
func giteeTypeToMode(entryType string) int {
switch entryType {
case "tree":
return 4
case "blob":
return 1
default:
return 1
}
}
func baseName(path string) string {
if i := strings.LastIndex(path, "/"); i >= 0 {
return path[i+1:]
}
return path
}
func parseGiteeDate(dateStr string) int64 {
if dateStr == "" {
return 0
}
layouts := []string{
time.RFC3339,
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05+08:00",
}
for _, layout := range layouts {
if t, err := time.Parse(layout, dateStr); err == nil {
return t.Unix()
}
}
return 0
}
func isBinaryContent(content []byte) bool {
if len(content) == 0 {
return false
}
check := content
if len(check) > 8000 {
check = check[:8000]
}
return bytes.Contains(check, []byte{0})
}