repro-run/gitea-ci/gitea/commit_status.go

57 lines
1.1 KiB
Go
Raw Normal View History

2023-01-23 00:58:44 +00:00
package gitea
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"net/url"
"path"
)
type CommitStatus struct {
Context string `json:"context"`
Description string `json:"description"`
// State holds the state of a CommitStatus
// It can be "pending", "success", "error", "failure", and "warning"
State string `json:"state"`
TargetUrl string `json:"target_url"`
}
func (g Gitea) CreateCommitStatus(repo, commit string, status CommitStatus) (err error) {
ur, err := url.Parse(g.Url)
if err != nil {
return
}
ur.Path = path.Join("/api/v1/repos/", repo, "/statuses/", commit)
payload, err := json.Marshal(status)
if err != nil {
return
}
req, err := http.NewRequest("POST", ur.String(), bytes.NewReader(payload))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
g.setAuth(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return
}
if res.StatusCode != http.StatusCreated {
byt, err := io.ReadAll(res.Body)
log.Printf("%s %v", byt, err)
return errors.New("not 200")
}
// err = json.NewDecoder(res.Body).Decode(&u)
return
}