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
}

View File

@@ -1,16 +1,33 @@
package gitea
import (
"encoding/json"
"fmt"
"os"
)
var API_PATH = "/api/v1"
type Gitea struct {
base_url string
Base_url string `json:"base_url"`
User_name string `json:"user_name"`
Access_code string `json:"access_code"`
}
func NewGitea(base_url string) (gitea Gitea) {
gitea.base_url = base_url
func NewGitea() (gitea Gitea) {
configJson, err := os.ReadFile("./configs/gitw.json")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
err = json.Unmarshal(configJson, &gitea)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
return
}
func (gitea *Gitea) Url() string {
return gitea.base_url + API_PATH
return gitea.Base_url + API_PATH
}

View File

@@ -81,9 +81,8 @@ func (gitea *Gitea) GetIssue(repo Repository, id uint) (Issue, error) {
return issue, nil
}
// FIXME: how to do basic authorization?
func (gitea *Gitea) UpdateIssue(repo Repository, issue Issue) error {
url := fmt.Sprintf("%s/repos/%s/issues/%d", gitea.Url(), repo.Full_name, issue.Id)
url := fmt.Sprintf("%s/repos/%s/issues/%d", gitea.Url(), repo.Full_name, issue.Number)
payload := make(map[string]interface{})
// payload["assignee"] = issue.Assignee
// payload["assignees"] = issue.Assignees
@@ -100,6 +99,7 @@ func (gitea *Gitea) UpdateIssue(repo Repository, issue Issue) 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

View File

@@ -35,7 +35,7 @@ func (milestone *Milestone) IntoTask(repository Repository) (task taskwarrior.Ta
task.Due = taskwarrior.GoTimeToTaskTime(milestone.Due_on)
task.Entry = taskwarrior.GoTimeToTaskTime(milestone.Created_at)
task.Modified = taskwarrior.GoTimeToTaskTime(milestone.Updated_at)
task.AppendComment(milestone.Description, milestone.Updated_at)
task.AppendComment(0, milestone.Description, milestone.Updated_at)
return
}
@@ -55,7 +55,7 @@ func (milestone *Milestone) MergeTask(task taskwarrior.Task) taskwarrior.Task {
}
}
if len(task.Annotations) < 1 {
task.AppendComment(milestone.Description, milestone.Created_at)
task.AppendComment(0, milestone.Description, milestone.Created_at)
} else {
task.Annotations[0].Description = diff.Prompt(task.Annotations[0].Description, milestone.Description, task.Annotations[0].Description)
task.Annotations[0].Entry = taskwarrior.GoTimeToTaskTime(time.Now().In(time.Local))
@@ -63,7 +63,7 @@ func (milestone *Milestone) MergeTask(task taskwarrior.Task) taskwarrior.Task {
} else {
// NOTE: there are no modifications between the last received update and the current version, hence we just accept theirs fully
task.Description = milestone.Title
if milestone.State == "closed" {
if milestone.State == string(CLOSED) {
// otherwise do not update the value
task.Status = "completed"
task.End = taskwarrior.GoTimeToTaskTime(milestone.Closed_at)
@@ -72,7 +72,7 @@ func (milestone *Milestone) MergeTask(task taskwarrior.Task) taskwarrior.Task {
task.Annotations[0].Description = milestone.Description
task.Annotations[0].Entry = taskwarrior.GoTimeToTaskTime(milestone.Updated_at)
} else {
task.AppendComment(milestone.Description, milestone.Updated_at)
task.AppendComment(0, milestone.Description, milestone.Updated_at)
}
}
task.Last_gitw_update = taskwarrior.GoTimeToTaskTime(time.Now().In(time.Local))
@@ -114,7 +114,6 @@ func (gitea *Gitea) GetMilestone(repo Repository, id uint) (Milestone, error) {
return milestone, nil
}
// FIXME: how to do basic authorization?
func (gitea *Gitea) UpdateMilestone(repo Repository, milestone Milestone) error {
url := fmt.Sprintf("%s/repos/%s/milestones/%d", gitea.Url(), repo.Full_name, milestone.Id)
payload := make(map[string]interface{})
@@ -128,6 +127,7 @@ func (gitea *Gitea) UpdateMilestone(repo Repository, milestone Milestone) 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
@@ -137,7 +137,7 @@ func (gitea *Gitea) UpdateMilestone(repo Repository, milestone Milestone) error
return err
}
defer result.Body.Close()
if result.StatusCode != 201 {
if result.StatusCode != 200 {
return errors.New(fmt.Sprintf("\tRequest returned status: %s\n", result.Status))
}
return nil