101 lines
3.1 KiB
Go
101 lines
3.1 KiB
Go
package gitea
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Comment struct {
|
|
Id uint `json:"id"`
|
|
Body string `json:"body"`
|
|
Issue_url string `json:"issue_url"`
|
|
Assets []string `json:"assets"`
|
|
Html_url string `json:"html_url"`
|
|
User User `json:"user"`
|
|
Original_author string `json:"original_author"`
|
|
Original_author_id uint `json:"original_author_id"`
|
|
Pull_request_url string `json:"pull_request_url"`
|
|
Created_at time.Time `json:"created_at"`
|
|
Updated_at time.Time `json:"updated_at"`
|
|
}
|
|
|
|
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
|
|
}
|
|
for _, comment := range data {
|
|
comment.Body = strings.ReplaceAll(comment.Body, "\r\n", "\n")
|
|
}
|
|
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
|
|
}
|
|
comment.Body = strings.ReplaceAll(comment.Body, "\r\n", "\n")
|
|
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
|
|
}
|
|
|
|
func (gitea *Gitea) NewComment(repo Repository, issue Issue, comment Comment) (Comment, error) {
|
|
url := fmt.Sprintf("%s/repos/%s/issues/%d/comments", gitea.Url(), repo.Full_name, issue.Id)
|
|
payload, err := json.Marshal(&map[string]interface{}{
|
|
"body": comment.Body,
|
|
})
|
|
var res Comment
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
response, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
return res, nil
|
|
}
|
|
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(&res); err != nil {
|
|
return res, err
|
|
}
|
|
return res, nil
|
|
}
|