oc-version/publish/releases/release.go

127 lines
2.8 KiB
Go
Raw Normal View History

2024-09-13 14:49:49 +02:00
package releases
import (
"fmt"
"io"
2024-09-20 21:28:09 +02:00
"io/ioutil"
2024-09-13 14:49:49 +02:00
"strings"
"encoding/json"
"net/http"
"oc-publish/occonst"
)
type checkStruct struct {
Name string `json:"name"`
Id int `json:"id"`
}
func CheckRelease(version string) (bool, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO)
res, err := http.Get(url)
if err != nil {
return false, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return false, err
}
var data []checkStruct
err = json.Unmarshal(body, &data)
if err != nil {
return false, err
}
for _, ele := range data {
if ele.Name == version {
return true, nil
}
}
// return data.Name != "", nil
return false, nil
}
func GetReleaseId(version string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
version)
res, err := http.Get(url)
if err != nil {
return 0, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return 0, err
}
var data checkStruct
err = json.Unmarshal(body, &data)
if err != nil {
return 0, err
}
return data.Id, nil
}
// curl -X 'POST' \
// 'https://cloud.o-forge.io/api/v1/repos/na/oc-version/releases?token=sss' \
// -H 'accept: application/json' \
// -H 'Content-Type: application/json' \
// -d '{
// "body": "string",
// "draft": true,
// "name": "string",
// "prerelease": true,
// "tag_name": "string",
// "target_commitish": "string"
// }'
func CreateRelease(version string, branch string) (error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases?token=%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
occonst.PUBLISH_TOKEN)
2024-09-20 21:28:09 +02:00
releasebytes, err := ioutil.ReadFile("release.txt")
releasetxt := string(releasebytes)
releasetxt = strings.Replace(releasetxt, "\n", "\\n", -1)
releasetxt = fmt.Sprintf(releasetxt, version, occonst.PUBLISH_URL, occonst.PUBLISH_REPO, version)
2024-09-13 14:49:49 +02:00
body := fmt.Sprintf(`{
2024-09-20 21:28:09 +02:00
"body": "%s",
2024-09-13 14:49:49 +02:00
"draft": false,
"name": "%s",
"prerelease": false,
"tag_name": "%s",
"target_commitish": "%s"
2024-09-20 21:28:09 +02:00
}`, releasetxt, version, version, branch)
2024-09-13 14:49:49 +02:00
request, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return err
}
request.Header.Add("accept", "application/json")
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
_, err1 := io.ReadAll(response.Body)
2024-09-20 21:28:09 +02:00
// cnt, err1 := io.ReadAll(response.Body)
// fmt.Println(string(cnt))
2024-09-13 14:49:49 +02:00
if err1 != nil {
return err1
}
return nil
}