From 3271246e749fbf30d096c1d2df854abdaa25402b Mon Sep 17 00:00:00 2001 From: admju Date: Wed, 11 Sep 2024 13:44:20 +0000 Subject: [PATCH] publish : init --- Makefile | 6 ++ publish/.gitignore | 3 + publish/go.mod | 3 + publish/main.go | 27 ++++++ publish/occonst/variables.go | 6 ++ publish/releases/assets.go | 166 +++++++++++++++++++++++++++++++++++ publish/releases/release.go | 63 +++++++++++++ 7 files changed, 274 insertions(+) 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/Makefile b/Makefile new file mode 100644 index 0000000..23c6045 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +publish: + curl -X 'POST' \ + 'https://cloud.o-forge.io/api/v1/repos/core/oc-deploy/releases/2/assets?name=oc.json&token=92ad0a4b3d75ec7c5964913b7085d7ddf379247c' \ + -H 'accept: application/json' \ + -H 'Content-Type: multipart/form-data' \ + -F 'attachment=@oc.json;type=application/json' \ No newline at end of file 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..2b72628 --- /dev/null +++ b/publish/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "os" + "oc-publish/releases" +) + +func main() { + + fmt.Println(" >> oc-publish :") + + version := os.Args[1] + fmt.Println(fmt.Sprintf(" << version : %s", version)) + + existe, _ := releases.CheckRelease(version) + fmt.Println(fmt.Sprintf(" << existe : %t ", existe)) + + idRelease, _ := releases.GetReleaseId(version) + fmt.Println(fmt.Sprintf(" << id : %d ", idRelease)) + + idAsset, _ := releases.GetAssetId(idRelease, "oc.json") + fmt.Println(fmt.Sprintf(" << idAsset : %d ", idAsset)) + + fmt.Println(releases.CreateAsset(idRelease, "../bin/oc-deploy")) + +} diff --git a/publish/occonst/variables.go b/publish/occonst/variables.go new file mode 100644 index 0000000..39ece06 --- /dev/null +++ b/publish/occonst/variables.go @@ -0,0 +1,6 @@ +package occonst + +var PUBLISH_URL = "https://cloud.o-forge.io" +var PUBLISH_VERSION = "core/oc-deploy" + +var PUBLISH_TOKEN = "8dd3e2d88ff358d86742697f5a431ef96f6da7c7" \ No newline at end of file diff --git a/publish/releases/assets.go b/publish/releases/assets.go new file mode 100644 index 0000000..8e4b24b --- /dev/null +++ b/publish/releases/assets.go @@ -0,0 +1,166 @@ +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_VERSION, + 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) + fmt.Println(err) + 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) (int, error) { + url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s", + occonst.PUBLISH_URL, + occonst.PUBLISH_VERSION, + idRelease, + "name", + occonst.PUBLISH_TOKEN) + + // request, err := newfileUploadRequest(url, extraParams, "file", "/tmp/doc.pdf") + + err := uploadFile(url, "attachment", filename) + fmt.Println(url, err) + + + // fmt.Println(url) + + // body := []byte("CONTENU") + // req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) + // if err != nil { + // return -1, err + // } + // req.Header.Add("accept", "application/json") + // req.Header.Add("Content-Type", "multipart/form-data") + + // client := &http.Client{} + // res, err := client.Do(req) + // fmt.Println(res, err) + + // cnt, err := io.ReadAll(res.Body) + // fmt.Println(string(cnt), err) + + // if err != nil { + // return -1, err + // } + + // if err != nil { + // return -2, err + // } + + // defer res.Body.Close() + + return 0, nil +} + +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() + + cnt, err := io.ReadAll(response.Body) + fmt.Println(string(cnt), err, writer.FormDataContentType()) + + // Handle the server response... + return nil + } + + +// func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) { +// file, err := os.Open(path) +// 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 +// } \ No newline at end of file diff --git a/publish/releases/release.go b/publish/releases/release.go new file mode 100644 index 0000000..af64a20 --- /dev/null +++ b/publish/releases/release.go @@ -0,0 +1,63 @@ +package releases + +import ( + "fmt" + "io" + "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/tags/%s", + occonst.PUBLISH_URL, + occonst.PUBLISH_VERSION, + version) + + 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 + } + + return data.Name != "", nil +} + +func GetReleaseId(version string) (int, error) { + url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", + occonst.PUBLISH_URL, + occonst.PUBLISH_VERSION, + 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 +} \ No newline at end of file