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.
148 lines
4.8 KiB
Go
148 lines
4.8 KiB
Go
package gitea
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type PullRequest struct {
|
|
Merged bool `json:"merged"`
|
|
Merged_at time.Time `json:"merged_at"`
|
|
}
|
|
|
|
type IssueState string
|
|
|
|
const CLOSED IssueState = "closed"
|
|
const OPEN IssueState = "open"
|
|
|
|
type Issue struct {
|
|
Id uint `json:"id"`
|
|
Url string `json:"url"`
|
|
Html_url string `json:"html_url"`
|
|
Number uint `json:"number"`
|
|
User User `json:"user"`
|
|
Original_author string `json:"original_author"`
|
|
Original_author_id uint `json:"original_author_id"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
Ref string `json:"ref"`
|
|
Assets []string `json:"assets"`
|
|
Labels []string `json:"labels"`
|
|
Milestone Milestone `json:"milestone"`
|
|
Assignee User `json:"assignee"`
|
|
Assignees []User `json:"assignees"`
|
|
State string `json:"state"`
|
|
Is_locked bool `json:"is_locked"`
|
|
Comments uint `json:"comments"`
|
|
Created_at time.Time `json:"created_at"`
|
|
Updated_at time.Time `json:"updated_at"`
|
|
Closed_at time.Time `json:"closed_at"`
|
|
Due_date time.Time `json:"due_date"`
|
|
Pull_request PullRequest `json:"pull_request"`
|
|
Repository Repository `json:"repository"`
|
|
Pin_order uint `json:"pin_order"`
|
|
}
|
|
|
|
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.Number)
|
|
payload := make(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.Id
|
|
// 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
|
|
}
|
|
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
|
|
}
|
|
result, err := client.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer result.Body.Close()
|
|
if result.StatusCode != 201 {
|
|
return errors.New(fmt.Sprintf("\tRequest returned status: %s\n", result.Status))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (gitea *Gitea) NewIssue(repo Repository, issue Issue) (Issue, error) {
|
|
url := fmt.Sprintf("%s/repos/%s/issues", gitea.Url(), repo.Full_name)
|
|
payload := make(map[string]interface{})
|
|
// payload["assignee"] = issue.Assignee
|
|
// payload["assignees"] = issue.Assignees
|
|
payload["body"] = issue.Body
|
|
payload["closed"] = issue.State == string(CLOSED)
|
|
payload["due_date"] = issue.Due_date
|
|
payload["lables"] = issue.Labels
|
|
payload["milestone"] = issue.Milestone.Id
|
|
// payload["ref"] = issue.Ref
|
|
payload["title"] = issue.Title
|
|
json_payload, err := json.Marshal(&payload)
|
|
var res Issue
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
response, err := http.Post(url, "appliaction/json", bytes.NewBuffer(json_payload))
|
|
if err != nil {
|
|
return res, 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(&res); err != nil {
|
|
return res, err
|
|
}
|
|
return res, nil
|
|
}
|