From aded3f3b40794e2fe8c50836a65e39df766bf238 Mon Sep 17 00:00:00 2001 From: admju Date: Fri, 13 Sep 2024 12:49:49 +0000 Subject: [PATCH] init --- .gitignore | 1 + Makefile | 17 +++++ latest.yml | 2 +- oc_0.1.0.yml | 6 +- publish/.gitignore | 3 + publish/go.mod | 3 + publish/main.go | 70 +++++++++++++++++++++ publish/occonst/variables.go | 18 ++++++ publish/releases/assets.go | 117 +++++++++++++++++++++++++++++++++++ publish/releases/release.go | 117 +++++++++++++++++++++++++++++++++++ 10 files changed, 351 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 publish/.gitignore create mode 100644 publish/go.mod create mode 100644 publish/main.go create mode 100644 publish/occonst/variables.go create mode 100644 publish/releases/assets.go create mode 100644 publish/releases/release.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d00889 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +oc.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..db23f6f --- /dev/null +++ b/Makefile @@ -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}) + diff --git a/latest.yml b/latest.yml index f810a2b..c551ffc 100644 --- a/latest.yml +++ b/latest.yml @@ -1,3 +1,3 @@ --- -version: 0.0.1 +version: 0.1.0 diff --git a/oc_0.1.0.yml b/oc_0.1.0.yml index d4331d5..79d3f12 100644 --- a/oc_0.1.0.yml +++ b/oc_0.1.0.yml @@ -12,8 +12,10 @@ tools: version: v3.16.0 opencloud: - - name: opencloud-0.1.0 - repository: https://harbor.dtf/dev + - repository: + name: opencloud-0.1.0 + url: https://harbor.dtf/dev + opts: --insecure-skip-tls-verify charts: - name: oc-mongo version: 0.1.0 diff --git a/publish/.gitignore b/publish/.gitignore new file mode 100644 index 0000000..f27c847 --- /dev/null +++ b/publish/.gitignore @@ -0,0 +1,3 @@ +go.sum +*_ +.coverage.* diff --git a/publish/go.mod b/publish/go.mod new file mode 100644 index 0000000..72918e1 --- /dev/null +++ b/publish/go.mod @@ -0,0 +1,3 @@ +module oc-publish + +go 1.22.2 diff --git a/publish/main.go b/publish/main.go new file mode 100644 index 0000000..d389be1 --- /dev/null +++ b/publish/main.go @@ -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 +} \ No newline at end of file diff --git a/publish/occonst/variables.go b/publish/occonst/variables.go new file mode 100644 index 0000000..089e045 --- /dev/null +++ b/publish/occonst/variables.go @@ -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 +} diff --git a/publish/releases/assets.go b/publish/releases/assets.go new file mode 100644 index 0000000..e1bc448 --- /dev/null +++ b/publish/releases/assets.go @@ -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 + } diff --git a/publish/releases/release.go b/publish/releases/release.go new file mode 100644 index 0000000..a1f346c --- /dev/null +++ b/publish/releases/release.go @@ -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 +}