This commit is contained in:
admju 2024-09-13 12:43:09 +00:00
parent 9f0218a4da
commit bd58ac1e02
7 changed files with 229 additions and 92 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
bin bin
*.base64
env

View File

@ -1,6 +1,25 @@
#!make
include env
export
ifndef OC_VERSION
$(error OC_VERSION is not set)
endif
ifndef PUBLISH_TOKEN
$(error PUBLISH_TOKEN is not set)
endif
# PUBLISH_REPO := "na/oc-version"
PUBLISH_REPO := "ej/oc-test"
PUBLISH_BRANCH := main
.PHONY: publish
publish: publish:
curl -X 'POST' \ @echo Publication de : ${OC_VERSION}
'https://cloud.o-forge.io/api/v1/repos/core/oc-deploy/releases/2/assets?name=oc.json&token=92ad0a4b3d75ec7c5964913b7085d7ddf379247c' \ @(cd publish && \
-H 'accept: application/json' \ PUBLISH_REPO=${PUBLISH_REPO} \
-H 'Content-Type: multipart/form-data' \ PUBLISH_TOKEN=${PUBLISH_TOKEN} \
-F 'attachment=@oc.json;type=application/json' PUBLISH_BRANCH=${PUBLISH_BRANCH} \
go run main.go ${OC_VERSION})

View File

@ -3,25 +3,81 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"io/ioutil"
"encoding/base64"
"oc-publish/releases" "oc-publish/releases"
"oc-publish/occonst"
) )
func main() { func main() {
fmt.Println(" >> oc-publish :")
version := os.Args[1] version := os.Args[1]
fmt.Println(fmt.Sprintf(" << version : %s", version))
existe, _ := releases.CheckRelease(version) fmt.Printf(" >> oc-publish :\n")
fmt.Println(fmt.Sprintf(" << existe : %t ", existe))
idRelease, _ := releases.GetReleaseId(version) fmt.Printf(" << Url : %s/%s\n", occonst.PUBLISH_URL, occonst.PUBLISH_REPO)
fmt.Println(fmt.Sprintf(" << id : %d ", idRelease))
idAsset, _ := releases.GetAssetId(idRelease, "oc.json") fmt.Printf(" << version : %s %s\n", version, occonst.PUBLISH_BRANCH)
fmt.Println(fmt.Sprintf(" << idAsset : %d ", idAsset))
fmt.Println(releases.CreateAsset(idRelease, "../bin/oc-deploy")) vversion := fmt.Sprintf("v%s", version)
existe, _ := releases.CheckRelease(vversion)
if existe == false {
err := releases.CreateRelease(vversion, occonst.PUBLISH_BRANCH)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
idRelease, _ := releases.GetReleaseId(vversion)
if existe == true {
fmt.Println(fmt.Sprintf(" << Release existante : %d ", idRelease))
} else {
fmt.Println(fmt.Sprintf(" << Release crée : %d ", idRelease))
}
assetname := "oc-deploy.base64"
binary := fmt.Sprintf("../bin/oc-deploy")
binary64 := fmt.Sprintf("../%s", assetname)
err := createBinaryFile(binary, binary64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
idAsset, _ := releases.GetAssetId(idRelease, assetname)
if idAsset == 0 {
fmt.Println(fmt.Sprintf(" << Ajout Asset : %s", assetname))
err := releases.CreateAsset(idRelease, binary64, assetname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println(fmt.Sprintf(" << Mise à jour : %s (idAsset=%d) ", assetname, idAsset))
err := releases.UpdateAsset(idRelease, idAsset, binary64, assetname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
} }
func createBinaryFile(source string, dest string) error {
fout, _ := os.Create(dest)
defer fout.Close()
byteValue, err := ioutil.ReadFile(source)
if err != nil {
fmt.Println("64e", err)
return err
}
data64 := base64.StdEncoding.EncodeToString(byteValue)
fout.Write([]byte(data64))
return nil
}

View File

@ -1,6 +1,18 @@
package occonst package occonst
var PUBLISH_URL = "https://cloud.o-forge.io" import (
var PUBLISH_VERSION = "core/oc-deploy" "os"
)
var PUBLISH_TOKEN = "" var PUBLISH_URL = getenv("PUBLISH_URL", "https://cloud.o-forge.io")
var PUBLISH_REPO = getenv("PUBLISH_REPO", "core/oc-deploy")
var PUBLISH_TOKEN = getenv("PUBLISH_TOKEN", "")
var PUBLISH_BRANCH = getenv("PUBLISH_BRANCH", "main")
func getenv(key string, defaut string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaut
}
return value
}

6
publish/release.txt Normal file
View File

@ -0,0 +1,6 @@
Version %s d'OpenCloud
```
wget %s/%s/releases/download/%s/oc-deploy.base64 -O - | base64 -d > oc-deploy
chmod u+x ./oc-deploy
```

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"mime/multipart" "mime/multipart"
"io" "io"
"io/ioutil"
"encoding/json" "encoding/json"
"net/http" "net/http"
"bytes" "bytes"
@ -21,7 +22,7 @@ type assetStruct struct {
func GetAssetId(idRelease int, name string) (int, error) { func GetAssetId(idRelease int, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_REPO,
idRelease) idRelease)
res, err := http.Get(url) res, err := http.Get(url)
@ -36,7 +37,6 @@ func GetAssetId(idRelease int, name string) (int, error) {
var data []assetStruct var data []assetStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
fmt.Println(err)
if err != nil { if err != nil {
return -3, err return -3, err
} }
@ -54,48 +54,52 @@ func GetAssetId(idRelease int, name string) (int, error) {
// -H 'accept: application/json' \ // -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \ // -H 'Content-Type: multipart/form-data' \
// -F 'attachment=oc-deploy' // -F 'attachment=oc-deploy'
func CreateAsset(idRelease int, filename string) (int, error) { func CreateAsset(idRelease int, filename string, name string) (error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_REPO,
idRelease, idRelease,
"name", name,
occonst.PUBLISH_TOKEN) occonst.PUBLISH_TOKEN)
// request, err := newfileUploadRequest(url, extraParams, "file", "/tmp/doc.pdf")
err := uploadFile(url, "attachment", filename) err := uploadFile(url, "attachment", filename)
fmt.Println(url, err)
return err
}
// fmt.Println(url) func UpdateAsset(idRelease int, idAsset int, filename string, name string) (error) {
// body := []byte("CONTENU") url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets/%d?token=%s",
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) occonst.PUBLISH_URL,
// if err != nil { occonst.PUBLISH_REPO,
// return -1, err idRelease,
// } idAsset,
// req.Header.Add("accept", "application/json") occonst.PUBLISH_TOKEN)
// req.Header.Add("Content-Type", "multipart/form-data")
// client := &http.Client{} // Create client
// res, err := client.Do(req) client := &http.Client{}
// fmt.Println(res, err)
// cnt, err := io.ReadAll(res.Body) // Create request
// fmt.Println(string(cnt), err) req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
// if err != nil { // Fetch Request
// return -1, err resp, err := client.Do(req)
// } if err != nil {
return err
}
defer resp.Body.Close()
// if err != nil { // Read Response Body
// return -2, err respBody, err := ioutil.ReadAll(resp.Body)
// } if err != nil {
return err
}
fmt.Println(string(respBody))
// defer res.Body.Close() return CreateAsset(idRelease, filename, name)
return 0, nil
} }
func uploadFile(url string, paramName string, filePath string) error { func uploadFile(url string, paramName string, filePath string) error {
@ -125,42 +129,17 @@ func uploadFile(url string, paramName string, filePath string) error {
response, err := client.Do(request) response, err := client.Do(request)
if err != nil { if err != nil {
fmt.Println(109, err)
return err return err
} }
defer response.Body.Close() defer response.Body.Close()
cnt, err := io.ReadAll(response.Body) if response.StatusCode > 400 {
fmt.Println(string(cnt), err, writer.FormDataContentType()) return fmt.Errorf(response.Status)
// Handle the server response...
return nil
} }
_, err1 := io.ReadAll(response.Body)
// func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) { // Handle the server response...
// file, err := os.Open(path) return err1
// if err != nil { }
// return nil, err
// }
// defer file.Close()
// body := &bytes.Buffer{}
// writer := multipart.NewWriter(body)
// part, err := writer.CreateFormFile(paramName, filepath.Base(path))
// if err != nil {
// return nil, err
// }
// _, err = io.Copy(part, file)
// // for key, val := range params {
// // _ = writer.WriteField(key, val)
// // }
// // err = writer.Close()
// // if err != nil {
// // return nil, err
// // }
// req, err := http.NewRequest("POST", uri, body)
// req.Header.Set("Content-Type", writer.FormDataContentType())
// return req, err
// }

View File

@ -3,6 +3,8 @@ package releases
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings"
"encoding/json" "encoding/json"
"net/http" "net/http"
@ -15,10 +17,9 @@ type checkStruct struct {
} }
func CheckRelease(version string) (bool, error) { func CheckRelease(version string) (bool, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_REPO)
version)
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
@ -29,19 +30,25 @@ func CheckRelease(version string) (bool, error) {
return false, err return false, err
} }
var data checkStruct var data []checkStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
if err != nil { if err != nil {
return false, err return false, err
} }
return data.Name != "", nil for _, ele := range data {
if ele.Name == version {
return true, nil
}
}
// return data.Name != "", nil
return false, nil
} }
func GetReleaseId(version string) (int, error) { func GetReleaseId(version string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_REPO,
version) version)
res, err := http.Get(url) res, err := http.Get(url)
@ -61,3 +68,59 @@ func GetReleaseId(version string) (int, error) {
return data.Id, nil 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)
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)
body := fmt.Sprintf(`{
"body": "%s",
"draft": false,
"name": "%s",
"prerelease": false,
"tag_name": "%s",
"target_commitish": "%s"
}`, releasetxt, version, version, branch)
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)
// cnt, err1 := io.ReadAll(response.Body)
// fmt.Println(string(cnt))
if err1 != nil {
return err1
}
return nil
}