This repository has been archived on 2025-10-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gitwarrior/internal/gitea/issue.go
Yves Biener c6d5bc1fc8 add(gitea-api): issue and milestone updating
This implements the api patches for updating an issue or milestone for a
given repository and issue or milestone. WIP for #1.
2023-10-18 17:47:42 +02:00

100 lines
2.6 KiB
Go

package gitea
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type PullRequest struct {
Merged bool
Merged_at time.Time
}
type Issue struct {
Id uint
Url string
Html_url string
Number uint
User User
Original_author string
Original_author_id uint
Title string
Body string
Ref string
Assets []string
Labels []string
Milestone Milestone
Assignee User
Assignees []User
State string
Is_locked bool
Comments uint
Created_at time.Time
Updated_at time.Time
Closed_at time.Time
Due_date time.Time
Pull_request PullRequest
Repository Repository
Pin_order uint
}
func (gitea *Gitea) GetIssues(repo Repository) ([]Issue, error) {
url := fmt.Sprintf("%s/repos/%s/issues?state=all", gitea.Url(), repo.Full_name)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
var data []Issue
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) GetIssue(repo Repository, id uint) (Issue, error) {
var issue Issue
url := fmt.Sprintf("%s/repos/%s/issues/%d", gitea.Url(), repo.Full_name, id)
response, err := http.Get(url)
if err != nil {
return issue, 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(&issue); err != nil {
return issue, err
}
return issue, nil
}
func (gitea *Gitea) UpdateIssue(repo Repository, issue Issue) error {
url := fmt.Sprintf("%s/repos/%s/issues/%d", gitea.Url(), repo.Full_name, issue.Id)
var payload map[string]interface{}
payload["assignee"] = issue.Assignee
payload["assignees"] = issue.Assignees
payload["body"] = issue.Body
payload["due_date"] = issue.Due_date
payload["milestone"] = issue.Milestone
payload["ref"] = issue.Ref
payload["state"] = issue.State
payload["title"] = issue.Title
payload["unset_due_date"] = issue.Due_date.Unix() == 0
json, err := json.Marshal(&payload)
if err != nil {
return err
}
_, err = http.NewRequest(http.MethodPatch, url, bytes.NewBuffer(json))
if err != nil {
return err
}
return nil
}