From 5ab0dd6da639c44fd614cca83c83d2ac13766f35 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Wed, 18 Oct 2023 22:26:00 +0200 Subject: [PATCH] add(gitea-api): comment getter and update functions This implements the getter api functions and update functions for comments. WIP for #1. --- cmd/gitw/main.go | 14 +++++++ internal/gitea/comment.go | 72 ++++++++++++++++++++++++++++++++++ internal/gitea/comment_test.go | 1 + 3 files changed, 87 insertions(+) create mode 100644 internal/gitea/comment.go create mode 100644 internal/gitea/comment_test.go diff --git a/cmd/gitw/main.go b/cmd/gitw/main.go index 8085d15..ebc88e4 100644 --- a/cmd/gitw/main.go +++ b/cmd/gitw/main.go @@ -20,10 +20,24 @@ func main() { fmt.Fprintln(os.Stderr, err) os.Exit(-1) } + comments, err := server.GetComments(repository) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } fmt.Printf("%#v\n", issue) fmt.Println("---") fmt.Printf("%#v\n", milestone) + fmt.Println("---") + + for _, comment := range comments { + // NOTE: filter comments to only include comments which are related to the current issue + // Order them by their Id's as that's the order they are in the issue (most likely) + if comment.Issue_url == issue.Html_url { + fmt.Printf("%#v\n", comment) + } + } // necessary configurations (see config file gitw.json) // - access code for gitea diff --git a/internal/gitea/comment.go b/internal/gitea/comment.go new file mode 100644 index 0000000..5fcc538 --- /dev/null +++ b/internal/gitea/comment.go @@ -0,0 +1,72 @@ +package gitea + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +type Comment struct { + Id uint + Body string + Issue_url string + Assets []string + Html_url string + User User + Original_author string + Original_author_id uint + Pull_request_url string + Created_at time.Time + Updated_at time.Time +} + +func (gitea *Gitea) GetComments(repo Repository) ([]Comment, error) { + url := fmt.Sprintf("%s/repos/%s/issues/comments", gitea.Url(), repo.Full_name) + response, err := http.Get(url) + if err != nil { + return nil, err + } + defer response.Body.Close() + var data []Comment + decoder := json.NewDecoder(response.Body) + // NOTE: remove this if I do not want to store everything from the json result in the struct + decoder.DisallowUnknownFields() // remain if every field shall be extracted + if err = decoder.Decode(&data); err != nil { + return nil, err + } + return data, nil +} + +func (gitea *Gitea) GetComment(repo Repository, id uint) (Comment, error) { + url := fmt.Sprintf("%s/repos/%s/issues/comments/%d", gitea.Url(), repo.Full_name, id) + response, err := http.Get(url) + var comment Comment + if err != nil { + return comment, err + } + defer response.Body.Close() + decoder := json.NewDecoder(response.Body) + // NOTE: remove this if I do not want to store everything from the json result in the struct + decoder.DisallowUnknownFields() // remain if every field shall be extracted + if err = decoder.Decode(&comment); err != nil { + return comment, err + } + return comment, err +} + +func (gitea *Gitea) UpdateComment(repo Repository, comment Comment) error { + url := fmt.Sprintf("%s/repos/%s/issues/comments/%d", gitea.Url(), repo.Full_name, comment.Id) + json, err := json.Marshal(&map[string]interface{}{ + "body": comment.Body, + }) + if err != nil { + return err + } + _, err = http.NewRequest(http.MethodPatch, url, bytes.NewBuffer(json)) + if err != nil { + return err + } + return nil +} diff --git a/internal/gitea/comment_test.go b/internal/gitea/comment_test.go new file mode 100644 index 0000000..3ac0cb7 --- /dev/null +++ b/internal/gitea/comment_test.go @@ -0,0 +1 @@ +package gitea