mod(taskwarrior): complete push implementation

Currently the merging process is not working entirely as I would
expect. There are no checks for actual changes between the local version
and the server version. WIP for #3. Configuration file created with
corresponding values like, user name and access code, which are - for
obvious reasons - not commited.
This commit is contained in:
2023-10-25 17:14:54 +02:00
parent 0cb4190540
commit 854dafde01
8 changed files with 160 additions and 47 deletions

View File

@@ -62,7 +62,6 @@ func (gitea *Gitea) GetComment(repo Repository, id uint) (Comment, error) {
return comment, err
}
// FIXME: how to do basic authorization?
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{}{
@@ -73,6 +72,7 @@ func (gitea *Gitea) UpdateComment(repo Repository, comment Comment) error {
}
client := &http.Client{}
request, err := http.NewRequest(http.MethodPatch, url, bytes.NewBuffer(json))
request.SetBasicAuth(gitea.User_name, gitea.Access_code)
request.Header.Set("content-type", "application/json")
if err != nil {
return err
@@ -82,14 +82,14 @@ func (gitea *Gitea) UpdateComment(repo Repository, comment Comment) error {
return err
}
defer result.Body.Close()
if result.StatusCode != 201 {
if result.StatusCode != 200 && result.StatusCode != 204 {
return errors.New(fmt.Sprintf("\tRequest returned status: %s\n", result.Status))
}
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)
url := fmt.Sprintf("%s/repos/%s/issues/%d/comments", gitea.Url(), repo.Full_name, issue.Number)
payload, err := json.Marshal(&map[string]interface{}{
"body": comment.Body,
})
@@ -97,12 +97,22 @@ func (gitea *Gitea) NewComment(repo Repository, issue Issue, comment Comment) (C
if err != nil {
return res, err
}
response, err := http.Post(url, "application/json", bytes.NewBuffer(payload))
client := &http.Client{}
request, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
request.SetBasicAuth(gitea.User_name, gitea.Access_code)
request.Header.Set("content-type", "application/json")
if err != nil {
return res, nil
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
result, err := client.Do(request)
if err != nil {
return res, err
}
defer result.Body.Close()
if result.StatusCode != 201 {
return res, errors.New(fmt.Sprintf("\tRequest returned status: %s\n", result.Status))
}
decoder := json.NewDecoder(result.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 {
@@ -110,3 +120,23 @@ func (gitea *Gitea) NewComment(repo Repository, issue Issue, comment Comment) (C
}
return res, nil
}
func (gitea *Gitea) DeleteComment(repo Repository, comment_id uint) error {
url := fmt.Sprintf("%s/repos/%s/issues/comments/%d", gitea.Url(), repo.Full_name, comment_id)
client := &http.Client{}
request, err := http.NewRequest(http.MethodDelete, url, nil)
request.SetBasicAuth(gitea.User_name, gitea.Access_code)
request.Header.Set("content-type", "application/json")
if err != nil {
return nil
}
result, err := client.Do(request)
if err != nil {
return err
}
defer result.Body.Close()
if result.StatusCode != 204 {
return errors.New(fmt.Sprintf("\tRequest returned status: %s\n", result.Status))
}
return nil
}