diff --git a/cmd/pull_request/close.go b/cmd/pull_request/close.go new file mode 100644 index 0000000..f6ecf6d --- /dev/null +++ b/cmd/pull_request/close.go @@ -0,0 +1,16 @@ +package pull_request + +import ( + "gitee_cli/internal/api/pull_request" + "github.com/spf13/cobra" +) + +var CloseCmd = &cobra.Command{ + Use: "close", + Short: "Close pull request", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + iid := args[0] + pull_request.Close(iid) + }, +} diff --git a/cmd/pull_request/pr.go b/cmd/pull_request/pr.go index 973d8ed..3d075b9 100644 --- a/cmd/pull_request/pr.go +++ b/cmd/pull_request/pr.go @@ -14,4 +14,6 @@ func init() { Pr.AddCommand(ListCmd) Pr.AddCommand(CreateCmd) Pr.AddCommand(CommentCmd) + Pr.AddCommand(CloseCmd) + Pr.AddCommand(ReviewCmd) } diff --git a/cmd/pull_request/review.go b/cmd/pull_request/review.go new file mode 100644 index 0000000..0dc4ea9 --- /dev/null +++ b/cmd/pull_request/review.go @@ -0,0 +1,16 @@ +package pull_request + +import ( + "gitee_cli/internal/api/pull_request" + "github.com/spf13/cobra" +) + +var ReviewCmd = &cobra.Command{ + Use: "review", + Short: "Review a pull request", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + iid := args[0] + pull_request.Review(iid) + }, +} diff --git a/cmd/root.go b/cmd/root.go index eb9f4c3..6fe3534 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var RootCmd = &cobra.Command{ Use: "gitee", Short: "Gitee In terminal", Long: "Gitee CLI is a tool which interact with gitee server seamlessly via terminal", - Version: "0.0.3", + Version: "0.0.4", } func init() { diff --git a/go.mod b/go.mod index 1478569..b9bf41e 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/charmbracelet/glamour v0.7.0 github.com/charmbracelet/lipgloss v0.10.0 github.com/fatih/color v1.16.0 - github.com/olekukonko/tablewriter v0.0.5 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/spf13/cobra v1.8.0 github.com/withfig/autocomplete-tools/integrations/cobra v1.2.1 @@ -38,6 +37,7 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect + github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/yuin/goldmark v1.7.0 // indirect diff --git a/internal/api/enterprises/enterprises.go b/internal/api/enterprises/enterprises.go index 3355160..551931f 100644 --- a/internal/api/enterprises/enterprises.go +++ b/internal/api/enterprises/enterprises.go @@ -17,7 +17,7 @@ func List() ([]Enterprise, error) { url := "https://api.gitee.com/enterprises/list" giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - _, err := giteeClient.Do() + err := giteeClient.Do() if err != nil || giteeClient.IsFail() { return nil, err } @@ -38,7 +38,7 @@ func List() ([]Enterprise, error) { func Find(path string) (Enterprise, error) { url := fmt.Sprintf("https://gitee.com/api/v5/enterprises/%s", path) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) - if _, err := giteeClient.Do(); err != nil { + if err := giteeClient.Do(); err != nil { return Enterprise{}, errors.New("查询企业失败!") } data, _ := giteeClient.GetRespBody() diff --git a/internal/api/issue/issue.go b/internal/api/issue/issue.go index 7719b70..bb36f3f 100644 --- a/internal/api/issue/issue.go +++ b/internal/api/issue/issue.go @@ -4,18 +4,18 @@ import ( "encoding/json" "errors" "fmt" - "gitee_cli/utils/http_utils" "gitee_cli/internal/api/issue_state" + "gitee_cli/utils/http_utils" ) const Endpoint = "https://api.gitee.com/enterprises/%d/issues" type Issue struct { - Id int `json:"id"` - Ident string `json:"ident"` - Title string `json:"title"` - Url string `json:"issue_url"` - Description string `json:"description"` + Id int `json:"id"` + Ident string `json:"ident"` + Title string `json:"title"` + Url string `json:"issue_url"` + Description string `json:"description"` IssueState issue_state.IssueState `json:"issue_state"` } @@ -24,7 +24,7 @@ func Find(enterpriseId int, params map[string]string) ([]Issue, error) { giteeClient := http_utils.NewGiteeClient("GET", url, params, nil) giteeClient.SetCookieAuth() - _, err := giteeClient.Do() + err := giteeClient.Do() if err != nil || giteeClient.IsFail() { return []Issue{}, err } diff --git a/internal/api/issue_state/issue_state.go b/internal/api/issue_state/issue_state.go index 0589d8a..51a37e4 100644 --- a/internal/api/issue_state/issue_state.go +++ b/internal/api/issue_state/issue_state.go @@ -16,7 +16,7 @@ func ListWithIssue(entId int, issueId int) ([]IssueState, error) { url := fmt.Sprintf("https://api.gitee.com/enterprises/%d/issues/%d/issue_states", entId, issueId) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - if _, err := giteeClient.Do(); err != nil { + if err := giteeClient.Do(); err != nil { return nil, err } diff --git a/internal/api/issue_type/issue_type.go b/internal/api/issue_type/issue_type.go index 99f51e3..07981fb 100644 --- a/internal/api/issue_type/issue_type.go +++ b/internal/api/issue_type/issue_type.go @@ -40,7 +40,7 @@ func List(issueType int, entPath string) ([]IssueType, error) { url := fmt.Sprintf("https://api.gitee.com/enterprises/%d/issue_types/enterprise_issue_types?category=%s&page=1&per_page=1000&state=1", ent.Id, category) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - if _, err := giteeClient.Do(); err != nil { + if err := giteeClient.Do(); err != nil { return nil, err } @@ -72,7 +72,7 @@ func FetchTemplate(issueTypeId, entId int) (string, error) { url := fmt.Sprintf("https://api.gitee.com/enterprises/%d/issue_types/%d", entId, issueTypeId) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - if _, err := giteeClient.Do(); err != nil { + if err := giteeClient.Do(); err != nil { return "", errors.New("获取模板失败!") } issueType := IssueType{} diff --git a/internal/api/member/member.go b/internal/api/member/member.go index 50bf266..c13e476 100644 --- a/internal/api/member/member.go +++ b/internal/api/member/member.go @@ -18,7 +18,7 @@ func Find(enterpriseId int, params map[string]string) ([]Member, error) { giteeClient := http_utils.NewGiteeClient("GET", url, params, nil) giteeClient.SetCookieAuth() - _, err := giteeClient.Do() + err := giteeClient.Do() if err != nil || giteeClient.IsFail() { return []Member{}, err } diff --git a/internal/api/pull_request/pull_request.go b/internal/api/pull_request/pull_request.go index 9e74ba7..34d4907 100644 --- a/internal/api/pull_request/pull_request.go +++ b/internal/api/pull_request/pull_request.go @@ -7,6 +7,8 @@ import ( "gitee_cli/config" "gitee_cli/utils/git_utils" "gitee_cli/utils/http_utils" + "github.com/fatih/color" + "os" "os/exec" "strconv" "strings" @@ -50,7 +52,7 @@ func Note(iid int, note string) error { payload := map[string]string{"body": note} giteeClient := http_utils.NewGiteeClient("POST", url, nil, payload) - giteeClient, _ = giteeClient.Do() + giteeClient.Do() if giteeClient.IsFail() { return errors.New(fmt.Sprintf("评论 pr %d 失败!", iid)) } @@ -199,10 +201,12 @@ func CreatePr(baseRepo, baseRef, headRef, title, body, assignees, testers string "testers": testers, "draft": draft, "prune_source_branch": prune, + "assignees_number": len(strings.Split(assignees, ",")), + "testers_number": len(strings.Split(testers, ",")), } giteeClient := http_utils.NewGiteeClient("POST", url, nil, payload) - giteeClient, err := giteeClient.Do() + err := giteeClient.Do() if err != nil { return PullRequest{}, errors.New("GiteeCilent 异常!") @@ -273,7 +277,7 @@ func CreateLightPr(baseRepo, baseRef, prTitle string) (PullRequest, error) { func Detail(iid, repoPath string) (PullRequest, error) { url := fmt.Sprintf("https://gitee.com/api/v5/repos/%s/pulls/%s", repoPath, iid) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) - if _, err := giteeClient.Do(); err != nil || giteeClient.IsFail() { + if err := giteeClient.Do(); err != nil || giteeClient.IsFail() { return PullRequest{}, errors.New("获取 Pull Request 详情失败") } @@ -289,10 +293,40 @@ func FetchPatchContent(iid, repoPath string) (string, error) { url := fmt.Sprintf("https://gitee.com/%s/pulls/%s.diff", repoPath, iid) giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - if _, err := giteeClient.Do(); err != nil || giteeClient.IsFail() { + if err := giteeClient.Do(); err != nil || giteeClient.IsFail() { return "", errors.New("获取 Pull Request 补丁内容失败") } data, _ := giteeClient.GetRespBody() return string(data), nil } + +func Close(iid string) { + pathWithNamespace, err := git_utils.ParseCurrentRepo() + if err != nil { + pathWithNamespace = config.Conf.DefaultPathWithNamespace + } + url := fmt.Sprintf("https://gitee.com/api/v5/repos/%s/pulls/%s", pathWithNamespace, iid) + payload := map[string]string{"state": "closed"} + giteeClient := http_utils.NewGiteeClient("PATCH", url, nil, payload) + if giteeClient.Do(); giteeClient.IsFail() { + color.Red("关闭 PR 失败!") + os.Exit(1) + } + color.Green("关闭 PR 成功🏅") +} + +func Review(iid string) { + pathWithNamespace, err := git_utils.ParseCurrentRepo() + if err != nil { + pathWithNamespace = config.Conf.DefaultPathWithNamespace + } + url := fmt.Sprintf("https://gitee.com/api/v5/repos/%s/pulls/%s/review", pathWithNamespace, iid) + giteeClient := http_utils.NewGiteeClient("POST", url, nil, nil) + giteeClient.Do() + if giteeClient.IsFail() { + color.Red("审查通过失败!") + os.Exit(1) + } + color.Green("审查通过成功🏅") +} diff --git a/internal/api/user/user.go b/internal/api/user/user.go index 44c631d..2bb9942 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -74,7 +74,7 @@ func BasicUser() (User, error) { url := "https://api.gitee.com/enterprises/users" giteeClient := http_utils.NewGiteeClient("GET", url, nil, nil) giteeClient.SetCookieAuth() - _, err := giteeClient.Do() + err := giteeClient.Do() if err != nil || giteeClient.IsFail() { return User{}, errors.New("查询用户失败!") } diff --git a/utils/http_utils/gitee_client.go b/utils/http_utils/gitee_client.go index 9f034b5..67c7a32 100644 --- a/utils/http_utils/gitee_client.go +++ b/utils/http_utils/gitee_client.go @@ -45,12 +45,11 @@ func NewGiteeClient(method, urlString string, query map[string]string, payload i } } -func (g *GiteeClient) SetHeaders(headers map[string]string) *GiteeClient { +func (g *GiteeClient) SetHeaders(headers map[string]string) { g.Headers = headers - return g } -func (g *GiteeClient) Do() (*GiteeClient, error) { +func (g *GiteeClient) Do() error { // 多次调用首先置空 g.Response = nil _payload, _ := json.Marshal(g.Payload) @@ -75,10 +74,10 @@ func (g *GiteeClient) Do() (*GiteeClient, error) { var err error if resp, err = client.Do(req); err != nil { - return g, err + return err } g.Response = resp - return g, nil + return nil } func (g *GiteeClient) IsSuccess() bool {