diff --git a/cmd/test_repository_summary.go b/cmd/test_repository_summary.go new file mode 100644 index 0000000000000000000000000000000000000000..8835ca88d25943cd215d378c7310f71c3e29fabf --- /dev/null +++ b/cmd/test_repository_summary.go @@ -0,0 +1,97 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os" + "strconv" + + "gitee.com/oscstudio/go-gitee-sdk" +) + +func main() { + if len(os.Args) < 3 { + log.Fatal("Usage: go run cmd/test_repository_summary.go [enterpriseID] [projectID] [ref]") + log.Fatal("Example: go run cmd/test_repository_summary.go 123 456 master") + log.Fatal("Note: ref parameter is optional, can be empty string") + } + + enterpriseID, err := strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatalf("Invalid enterpriseID: %v", err) + } + + projectID := os.Args[2] + ref := "" + if len(os.Args) > 3 { + ref = os.Args[3] + } + + client := gitee.NewClient( + os.Getenv("GITEE_API_URL"), + ) + client.SetBearerToken(os.Getenv("GITEE_TOKEN")) + + ctx := context.Background() + + // Call the repository summary API + summary, err := client.GetRepositorySummary(ctx, enterpriseID, projectID, ref) + if err != nil { + log.Fatalf("Failed to get repository summary: %v", err) + } + + // Print basic information + fmt.Println("=== Repository Overview Data ===") + fmt.Printf("Enterprise ID: %d\n", enterpriseID) + fmt.Printf("Project ID: %s\n", projectID) + if ref != "" { + fmt.Printf("Branch/Tag: %s\n", ref) + } + fmt.Printf("Current Branch/Tag: %s\n", summary.Ref) + fmt.Printf("Releases Count: %d\n", summary.ReleasesCount) + fmt.Printf("Create Lightweight PR: %t\n", summary.CreateLightweightPR) + fmt.Printf("Online Edit Enabled: %t\n", summary.OnlineEditEnabled) + fmt.Printf("Can Download ZIP: %t\n", summary.CanDownloadZip) + fmt.Printf("Can Create Issue: %t\n", summary.CanCreateIssue) + + // Print repository URL information + fmt.Println("\n=== Repository URL Information ===") + fmt.Printf("HTTPS URL: %s\n", summary.HTTPSURL) + fmt.Printf("SSH URL: %s\n", summary.SSHURL) + if summary.SVNURL != nil { + fmt.Printf("SVN URL: %v\n", summary.SVNURL) + } else { + fmt.Printf("SVN URL: Not supported\n") + } + if summary.SVNAndSSHURL != nil { + fmt.Printf("SVN+SSH URL: %v\n", summary.SVNAndSSHURL) + } else { + fmt.Printf("SVN+SSH URL: Not supported\n") + } + + // Print programming language information + if len(summary.Languages) > 0 { + fmt.Println("\n=== Programming Language Statistics ===") + for i, lang := range summary.Languages { + fmt.Printf("%d. %s: %.1f%% (%s)\n", i+1, lang.Language, lang.Percent, lang.Color) + } + } else { + fmt.Println("\n=== Programming Language Statistics ===") + fmt.Println("No programming language information available") + } + + // Print protection rule information + if summary.DefaultProtectionRule != nil { + fmt.Println("\n=== Default Protection Rule ===") + fmt.Printf("%+v\n", summary.DefaultProtectionRule) + } else { + fmt.Println("\n=== Default Protection Rule ===") + fmt.Println("No default protection rule") + } + + fmt.Println("\n=== Complete JSON Output ===") + jsonOutput, _ := json.MarshalIndent(summary, "", " ") + fmt.Println(string(jsonOutput)) +} diff --git a/projects.go b/projects.go index 7d73fd297d2708f356543d9eba4435bd93cd4ff7..5821e8b70993f0400fce92aba4d34cfd33f1e9e3 100644 --- a/projects.go +++ b/projects.go @@ -47,3 +47,52 @@ func (c *Client) GetProjectPushConfig(ctx context.Context, enterpriseId int, pro return config, nil } + +// RepositorySummary represents repository overview data +type RepositorySummary struct { + Ref string `json:"ref"` + ReleasesCount int `json:"releases_count"` + DefaultProtectionRule interface{} `json:"default_protection_rule"` + Languages []RepositoryLanguage `json:"languages"` + CreateLightweightPR bool `json:"create_lightweight_pr"` + OnlineEditEnabled bool `json:"online_edit_enabled"` + HTTPSURL string `json:"https_url"` + SSHURL string `json:"ssh_url"` + SVNURL interface{} `json:"svn_url"` + SVNAndSSHURL interface{} `json:"svn_and_ssh_url"` + CanDownloadZip bool `json:"can_download_zip"` + CanCreateIssue bool `json:"can_create_issue"` +} + +// RepositoryLanguage represents programming language information +type RepositoryLanguage struct { + Language string `json:"language"` + Color string `json:"color"` + Percent float64 `json:"percent"` +} + +// GetRepositorySummary retrieves repository overview data +func (c *Client) GetRepositorySummary(ctx context.Context, enterpriseId int, projectId string, ref string) (*RepositorySummary, error) { + url := fmt.Sprintf("%s/enterprises/%d/projects/%s/summary", + c.APIBase, enterpriseId, projectId) + + req, err := c.NewRequest(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + // Add ref parameter to query if provided + if ref != "" { + q := req.URL.Query() + q.Add("ref", ref) + req.URL.RawQuery = q.Encode() + } + + summary := &RepositorySummary{} + err = c.SendWithAuth(req, summary) + if err != nil { + return nil, err + } + + return summary, nil +}