add(gitea-api): comment getter and update functions

This implements the getter api functions and update functions for
comments. WIP for #1.
This commit is contained in:
2023-10-18 22:26:00 +02:00
parent c6d5bc1fc8
commit 5ab0dd6da6
3 changed files with 87 additions and 0 deletions

72
internal/gitea/comment.go Normal file
View File

@@ -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
}

View File

@@ -0,0 +1 @@
package gitea