Publish
This commit is contained in:
		| @@ -6,6 +6,7 @@ import ( | ||||
|     "path/filepath" | ||||
|     "mime/multipart" | ||||
|     "io" | ||||
|     "io/ioutil" | ||||
|     "encoding/json" | ||||
|     "net/http" | ||||
|     "bytes" | ||||
| @@ -53,7 +54,7 @@ func GetAssetId(idRelease int, name string) (int, error) { | ||||
| //   -H 'accept: application/json' \ | ||||
| //   -H 'Content-Type: multipart/form-data' \ | ||||
| //   -F 'attachment=oc-deploy' | ||||
| func CreateAsset(idRelease int, filename string, name 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", | ||||
|                         occonst.PUBLISH_URL, | ||||
|                         occonst.PUBLISH_REPO, | ||||
| @@ -63,20 +64,42 @@ func CreateAsset(idRelease int, filename string, name string) (int, error) { | ||||
|  | ||||
|     err := uploadFile(url, "attachment", filename) | ||||
|  | ||||
|     return 0, err  | ||||
|     return 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) | ||||
| func UpdateAsset(idRelease int, idAsset int, filename string, name string) (error) { | ||||
|  | ||||
|   err := uploadFile(url, "attachment", filename) | ||||
|   url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets/%d?token=%s", | ||||
|             occonst.PUBLISH_URL, | ||||
|             occonst.PUBLISH_REPO, | ||||
|             idRelease, | ||||
|             idAsset, | ||||
|             occonst.PUBLISH_TOKEN) | ||||
|  | ||||
|   return 0, err | ||||
|     // Create client | ||||
|     client := &http.Client{} | ||||
|  | ||||
|     // Create request | ||||
|     req, err := http.NewRequest("DELETE", url, nil) | ||||
|     if err != nil { | ||||
|         return err | ||||
|     } | ||||
|  | ||||
|     // Fetch Request | ||||
|     resp, err := client.Do(req) | ||||
|     if err != nil { | ||||
|         return err | ||||
|     } | ||||
|     defer resp.Body.Close() | ||||
|  | ||||
|     // Read Response Body | ||||
|     respBody, err := ioutil.ReadAll(resp.Body) | ||||
|     if err != nil { | ||||
|         return err | ||||
|     } | ||||
|     fmt.Println(string(respBody)) | ||||
|  | ||||
|   return CreateAsset(idRelease, filename, name) | ||||
| } | ||||
|  | ||||
| func uploadFile(url string, paramName string, filePath string) error { | ||||
| @@ -85,7 +108,7 @@ func uploadFile(url string, paramName string, filePath string) error { | ||||
|       return err | ||||
|     } | ||||
|     defer file.Close() | ||||
|    | ||||
|  | ||||
|     body := &bytes.Buffer{} | ||||
|     writer := multipart.NewWriter(body) | ||||
|     part, err := writer.CreateFormFile(paramName, filepath.Base(filePath)) | ||||
| @@ -104,14 +127,19 @@ func uploadFile(url string, paramName string, filePath string) error { | ||||
|     request.Header.Add("accept", "application/json") | ||||
|     client := &http.Client{} | ||||
|     response, err := client.Do(request) | ||||
|    | ||||
|  | ||||
|     if err != nil { | ||||
|       fmt.Println(109, err) | ||||
|       return err | ||||
|     } | ||||
|     defer response.Body.Close() | ||||
|    | ||||
|     _, err := io.ReadAll(response.Body) | ||||
|     if response.StatusCode > 400 { | ||||
|         return fmt.Errorf(response.Status) | ||||
|   } | ||||
|  | ||||
|     _, err1 := io.ReadAll(response.Body) | ||||
|  | ||||
|     // Handle the server response... | ||||
|     return nil | ||||
|     return err1 | ||||
|   } | ||||
|   | ||||
| @@ -3,6 +3,7 @@ package releases | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"io/ioutil" | ||||
|     "strings" | ||||
|     "encoding/json" | ||||
|     "net/http" | ||||
| @@ -86,14 +87,19 @@ func CreateRelease(version string, branch string) (error) { | ||||
| 						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": "Version %s", | ||||
|     "body": "%s", | ||||
|     "draft": false, | ||||
|     "name": "%s", | ||||
|     "prerelease": false, | ||||
|     "tag_name": "%s", | ||||
|     "target_commitish": "%s" | ||||
| }`, version, version, version, branch) | ||||
| }`, releasetxt, version, version, branch) | ||||
|  | ||||
|     request, err := http.NewRequest("POST", url, strings.NewReader(body)) | ||||
|     if err != nil { | ||||
| @@ -110,6 +116,9 @@ func CreateRelease(version string, branch string) (error) { | ||||
|     defer response.Body.Close() | ||||
|  | ||||
|     _, err1 := io.ReadAll(response.Body) | ||||
|     // cnt, err1 := io.ReadAll(response.Body) | ||||
|     // fmt.Println(string(cnt)) | ||||
|  | ||||
|     if err1 != nil { | ||||
|         return err1 | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user