hugo-micropub/gitea.go

134 lines
3.5 KiB
Go
Raw Normal View History

2019-11-07 10:00:24 +00:00
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
2019-11-14 17:46:49 +00:00
"io/ioutil"
2019-11-07 10:00:24 +00:00
"net/http"
"net/url"
2019-12-12 15:29:26 +00:00
"strings"
2019-11-07 10:00:24 +00:00
)
2019-12-12 15:29:26 +00:00
type GiteaCommitRequest struct {
Content string `json:"content"`
Message string `json:"message"`
SHA string `json:"sha,omitempty"`
}
type GiteaReadRequest struct {
Type string `json:"type"`
Content string `json:"content"`
SHA string `json:"sha"`
}
type GiteaErrorResponse struct {
Message string `json:"message"`
}
func CreateFile(path string, file string, message string) error {
request := &GiteaCommitRequest{
Content: base64.StdEncoding.EncodeToString([]byte(file)),
Message: message,
2019-11-07 10:00:24 +00:00
}
2019-12-12 15:29:26 +00:00
bytesRepresentation, err := json.Marshal(request)
2019-11-07 10:00:24 +00:00
if err != nil {
return errors.New("failed to marshal json before committing")
}
2019-12-06 09:32:30 +00:00
resp, err := http.Post(GiteaEndpoint+url.QueryEscape(path)+"?access_token="+GiteaToken, "application/json", bytes.NewBuffer(bytesRepresentation))
2019-11-07 10:00:24 +00:00
if err != nil || resp.StatusCode != 201 {
return errors.New("failed to create file in repo")
}
return nil
}
2019-11-14 17:46:49 +00:00
2019-12-12 15:29:26 +00:00
func UpdateFile(path string, file string, message string) error {
existingFile, exists, err := ReadFile(path)
if err != nil {
return err
}
if !exists {
// File doesn't exist, create it
return CreateFile(path, file, message)
}
request := &GiteaCommitRequest{
Content: base64.StdEncoding.EncodeToString([]byte(file)),
Message: message,
SHA: existingFile.SHA,
}
bytesRepresentation, err := json.Marshal(request)
if err != nil {
return errors.New("failed to marshal json before committing")
}
req, err := http.NewRequest(http.MethodPut, GiteaEndpoint+url.QueryEscape(path)+"?access_token="+GiteaToken, bytes.NewBuffer(bytesRepresentation))
if err != nil {
return errors.New("error making update request")
}
req.Header.Set("Content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
return errors.New("failed to update file in repo")
}
return nil
}
func ReadFile(path string) (response *GiteaReadRequest, exists bool, err error) {
exists = false
2019-12-06 09:32:30 +00:00
resp, err := http.Get(GiteaEndpoint + url.QueryEscape(path) + "?access_token=" + GiteaToken)
2019-11-14 17:46:49 +00:00
if err != nil || resp.StatusCode != 200 {
2019-12-12 15:29:26 +00:00
if resp != nil {
defer resp.Body.Close()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
err = errors.New("failed reading Gitea error response")
return
}
errorResponse := &GiteaErrorResponse{}
marshalErr := json.Unmarshal(body, &errorResponse)
if marshalErr != nil {
err = errors.New("failed parsing Gitea error response")
return
}
exists = !strings.Contains(errorResponse.Message, "does not exist")
if !exists {
return
}
}
2019-11-14 17:46:49 +00:00
err = errors.New("failed to read file in repo")
return
}
2019-12-12 15:29:26 +00:00
exists = true
2019-11-14 17:46:49 +00:00
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
err = errors.New("failed reading file in repo")
return
}
2019-12-12 15:29:26 +00:00
response = &GiteaReadRequest{}
err = json.Unmarshal(body, &response)
2019-11-14 17:46:49 +00:00
if err != nil {
err = errors.New("failed parsing Gitea response")
return
}
2019-12-12 15:29:26 +00:00
return
}
func ReadFileContent(path string) (fileContent string, err error) {
giteaResponseBody, exists, err := ReadFile(path)
if err != nil {
return
}
if !exists {
// File doesn't exist, nothing to read
err = errors.New("file does not exist")
return
}
2019-11-14 17:46:49 +00:00
decodedBytes, err := base64.StdEncoding.DecodeString(giteaResponseBody.Content)
if err != nil {
err = errors.New("failed decoding file content")
}
fileContent = string(decodedBytes)
return
}