36 lines
858 B
Go
36 lines
858 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/base64"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
)
|
||
|
|
||
|
func CommitEntry(path string, file string, name string) error {
|
||
|
giteaEndpoint, err := GetGiteaEndpoint()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
giteaToken, err := GetGiteaToken()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
message := map[string]interface{}{
|
||
|
"message": name,
|
||
|
"content": base64.StdEncoding.EncodeToString([]byte(file)),
|
||
|
}
|
||
|
bytesRepresentation, err := json.Marshal(message)
|
||
|
if err != nil {
|
||
|
return errors.New("failed to marshal json before committing")
|
||
|
}
|
||
|
// TODO: handle file updating
|
||
|
resp, err := http.Post(giteaEndpoint+url.QueryEscape(path)+"?access_token="+giteaToken, "application/json", bytes.NewBuffer(bytesRepresentation))
|
||
|
if err != nil || resp.StatusCode != 201 {
|
||
|
return errors.New("failed to create file in repo")
|
||
|
}
|
||
|
return nil
|
||
|
}
|