From 197da8386372f84edd7f8f6354512c4e2b220697 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 3 Jun 2024 10:22:53 -0600 Subject: Extract Group type --- cmd/stanuki/main.go | 21 +++++---------------- pkg/gitlab/client.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ pkg/gitlab/group.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 pkg/gitlab/client.go create mode 100644 pkg/gitlab/group.go diff --git a/cmd/stanuki/main.go b/cmd/stanuki/main.go index 3d0da86..7288c7f 100644 --- a/cmd/stanuki/main.go +++ b/cmd/stanuki/main.go @@ -3,27 +3,16 @@ package main import ( "context" "fmt" - "io" - "net/http" "github.com/xlgmokha/x/pkg/env" - "github.com/xlgmokha/x/pkg/x" "gitlab.com/mokhax/stanuki/pkg/gitlab" ) func main() { - token := env.Fetch("GITLAB_TOKEN", "") - url := "https://gitlab.com/api/v4/groups/9970/issues" - client := http.Client{} - request := x.Must(http.NewRequestWithContext(context.TODO(), "GET", url, nil)) - request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", token)) - response := x.Must(client.Do(request)) - if env.Fetch("DUMP", "") != "" { - fmt.Println(string(x.Must(io.ReadAll(response.Body)))) - } else { - issues := x.Must(gitlab.FromIssues(response.Body)) - for _, issue := range issues { - fmt.Printf("%v: %v\n", issue.ID, issue.Title) - } + gl := gitlab.New(context.TODO(), env.Fetch("GITLAB_TOKEN", "")) + + issues := gl.Group(9970).Issues() + for _, issue := range issues { + fmt.Printf("%v: %v\n", issue.ID, issue.Title) } } diff --git a/pkg/gitlab/client.go b/pkg/gitlab/client.go new file mode 100644 index 0000000..e6c79a5 --- /dev/null +++ b/pkg/gitlab/client.go @@ -0,0 +1,45 @@ +package gitlab + +import ( + "bytes" + "context" + "fmt" + "io" + "io/ioutil" + "net/http" + + "github.com/xlgmokha/x/pkg/env" + "github.com/xlgmokha/x/pkg/x" +) + +type GitLab struct { + ctx context.Context + client http.Client + token string + url string +} + +func New(ctx context.Context, token string) *GitLab { + return &GitLab{ + ctx: ctx, + client: http.Client{}, + token: token, + url: "https://gitlab.com/api/v4", + } +} + +func (gl *GitLab) Group(id int) *Group { + return NewGroup(gl, id) +} + +func (gl *GitLab) Get(url string) *http.Response { + request := x.Must(http.NewRequestWithContext(gl.ctx, "GET", url, nil)) + request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", gl.token)) + response := x.Must(gl.client.Do(request)) + if env.Fetch("DUMP", "") != "" { + body := x.Must(io.ReadAll(response.Body)) + fmt.Println(string(body)) + response.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + } + return response +} diff --git a/pkg/gitlab/group.go b/pkg/gitlab/group.go new file mode 100644 index 0000000..8fedc45 --- /dev/null +++ b/pkg/gitlab/group.go @@ -0,0 +1,28 @@ +package gitlab + +import ( + "fmt" + + "github.com/xlgmokha/x/pkg/x" +) + +type Group struct { + client *GitLab + id int + url string +} + +func NewGroup(gl *GitLab, id int) *Group { + return &Group{ + client: gl, + id: id, + url: fmt.Sprintf("%v/groups/%v", gl.url, id), + } +} + +func (group *Group) Issues() []Issue { + response := group.client.Get(group.url + "/issues") + defer response.Body.Close() + + return x.Must(FromIssues(response.Body)) +} -- cgit v1.2.3