Compare commits

...

1 Commits

Author SHA1 Message Date
admju
aded3f3b40 init 2024-09-13 12:49:49 +00:00
10 changed files with 351 additions and 3 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
oc.json

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
ifndef OC_VERSION
$(error OC_VERSION is not set)
endif
PUBLISH_REPO := "na/oc-version"
PUBLISH_TOKEN := "61bcebe3a6ffa67ae88adeb49825d94d212889eb"
PUBLISH_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
.PHONY: publish
publish:
@echo Publication de : ${OC_VERSION}
@(cd publish && \
PUBLISH_REPO=${PUBLISH_REPO} \
PUBLISH_TOKEN=${PUBLISH_TOKEN} \
PUBLISH_BRANCH=${PUBLISH_BRANCH} \
go run main.go ${OC_VERSION})

View File

@ -1,3 +1,3 @@
--- ---
version: 0.0.1 version: 0.1.0

View File

@ -12,8 +12,10 @@ tools:
version: v3.16.0 version: v3.16.0
opencloud: opencloud:
- name: opencloud-0.1.0 - repository:
repository: https://harbor.dtf/dev name: opencloud-0.1.0
url: https://harbor.dtf/dev
opts: --insecure-skip-tls-verify
charts: charts:
- name: oc-mongo - name: oc-mongo
version: 0.1.0 version: 0.1.0

3
publish/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
go.sum
*_
.coverage.*

3
publish/go.mod Normal file
View File

@ -0,0 +1,3 @@
module oc-publish
go 1.22.2

70
publish/main.go Normal file
View File

@ -0,0 +1,70 @@
package main
import (
"fmt"
"os"
"io"
"encoding/base64"
"oc-publish/releases"
"oc-publish/occonst"
)
func main() {
version := os.Args[1]
fmt.Printf(" >> oc-publish :\n")
fmt.Printf(" << Url : %s/%s\n", occonst.PUBLISH_URL, occonst.PUBLISH_REPO)
fmt.Printf(" << version : %s %s\n", version, occonst.PUBLISH_BRANCH)
vversion := fmt.Sprintf("v%s", version)
existe, _ := releases.CheckRelease(vversion)
fmt.Printf(" << existe : %t\n", existe)
if existe == false {
err := releases.CreateRelease(vversion, occonst.PUBLISH_BRANCH)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
idRelease, _ := releases.GetReleaseId(vversion)
fmt.Println(fmt.Sprintf(" << id : %d ", idRelease))
ficversion := fmt.Sprintf("../oc_%s.yml", version)
ficjson := fmt.Sprintf("../oc.json")
createJsonFile(ficversion, ficjson)
idAsset, _ := releases.GetAssetId(idRelease, "oc.json")
if idAsset == 0 {
releases.CreateAsset(idRelease, ficjson)
} else {
releases.UpdateAsset(idRelease, idAsset, ficjson)
fmt.Println(fmt.Sprintf(" << idAsset : %d ", idAsset))
}
}
func createJsonFile(ficversion string, jsonfile string) error {
fout, _ := os.Create(jsonfile)
defer fout.Close()
fin, err := os.Open(ficversion)
if err != nil {
return err
}
defer fin.Close()
byteValue, err := io.ReadAll(fin)
// data := "abc123!?$*&()'-=@~"
data64 := base64.StdEncoding.EncodeToString(byteValue)
content := fmt.Sprintf(`{"value": "%s"}`, data64)
fout.Write([]byte(content))
fout.Write([]byte("\n"))
return nil
}

View File

@ -0,0 +1,18 @@
package occonst
import (
"os"
)
var PUBLISH_URL = getenv("PUBLISH_URL", "https://cloud.o-forge.io")
var PUBLISH_REPO = getenv("PUBLISH_REPO", "core/oc-version")
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
}

117
publish/releases/assets.go Normal file
View File

@ -0,0 +1,117 @@
package releases
import (
"fmt"
"os"
"path/filepath"
"mime/multipart"
"io"
"encoding/json"
"net/http"
"bytes"
"oc-publish/occonst"
)
type assetStruct struct {
Name string `json:"name"`
Id int `json:"id"`
}
func GetAssetId(idRelease int, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
idRelease)
res, err := http.Get(url)
if err != nil {
return -1, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return -2, err
}
var data []assetStruct
err = json.Unmarshal(body, &data)
if err != nil {
return -3, err
}
for _, ele := range data {
if ele.Name == name {
return ele.Id, nil
}
}
return 0, nil
}
// curl -X 'POST' \
// 'https://cloud.o-forge.io/api/v1/repos/core/oc-deploy/releases/2/assets?name=zzzz' \
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'attachment=oc-deploy'
func CreateAsset(idRelease int, filename string, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
idRelease,
name,
occonst.PUBLISH_TOKEN)
err := uploadFile(url, "attachment", filename)
return 0, err
}
func UpdateAsset(idRelease int, idAsset int,filename string, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s",
occonst.PUBLISH_URL,
occonst.PUBLISH_REPO,
idRelease,
name,
occonst.PUBLISH_TOKEN)
err := uploadFile(url, "attachment", filename)
return 0, err
}
func uploadFile(url string, paramName string, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(filePath))
if err != nil {
return err
}
_, err = io.Copy(part, file)
err = writer.Close()
if err != nil {
return err
}
request, err := http.NewRequest("POST", url, body)
request.Header.Add("Content-Type", writer.FormDataContentType())
request.Header.Add("accept", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
_, err := io.ReadAll(response.Body)
// Handle the server response...
return nil
}

117
publish/releases/release.go Normal file
View File

@ -0,0 +1,117 @@
package releases
import (
"fmt"
"io"
"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)
body := fmt.Sprintf(`{
"body": "Version %s",
"draft": false,
"name": "%s",
"prerelease": false,
"tag_name": "%s",
"target_commitish": "%s"
}`, version, 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)
if err1 != nil {
return err1
}
return nil
}