Skip to content

Commit

Permalink
fix: store upload, due new apis
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed May 1, 2024
1 parent 070a3f8 commit 01cc082
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 63 deletions.
109 changes: 60 additions & 49 deletions account-api/producer_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
type SoftwareVersionList []SoftwareVersion

type ExtensionBinary struct {
Id int `json:"id"`
Name string `json:"name"`
RemoteLink string `json:"remoteLink"`
Version string `json:"version"`
Status struct {
Id int `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
Status struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -36,24 +35,36 @@ type ExtensionBinary struct {
} `json:"locale"`
Text string `json:"text"`
} `json:"changelogs"`
CreationDate string `json:"creationDate"`
LastChangeDate string `json:"lastChangeDate"`
Archives []struct {
Id int `json:"id"`
RemoteLink string `json:"remoteLink"`
ShopwareMajorVersion interface{} `json:"shopwareMajorVersion"`
IoncubeEncrypted bool `json:"ioncubeEncrypted"`
ManifestRemoteLink interface{} `json:"manifestRemoteLink"`
} `json:"archives"`
IonCubeEncrypted bool `json:"ionCubeEncrypted"`
LicenseCheckRequired bool `json:"licenseCheckRequired"`
HasActiveCodeReviewWarnings bool `json:"hasActiveCodeReviewWarnings"`
CreationDate string `json:"creationDate"`
LastChangeDate string `json:"lastChangeDate"`
IonCubeEncrypted bool `json:"ionCubeEncrypted"`
LicenseCheckRequired bool `json:"licenseCheckRequired"`
HasActiveCodeReviewWarnings bool `json:"hasActiveCodeReviewWarnings"`
}

type ExtensionUpdate struct {
Id int
SoftwareVersions []string `json:"softwareVersions"`
IonCubeEncrypted bool `json:"ionCubeEncrypted"`
LicenseCheckRequired bool `json:"licenseCheckRequired"`
Changelogs []ExtensionUpdateChangelog `json:"changelogs"`
}

type ExtensionUpdateChangelog struct {
Locale string `json:"locale"`
Text string `json:"text"`
}

type ExtensionCreate struct {
SoftwareVersions []string `json:"softwareVersions"`
Changelogs []ExtensionUpdateChangelog `json:"changelogs"`
Version string `json:"version"`
}

func (e ProducerEndpoint) GetExtensionBinaries(ctx context.Context, extensionId int) ([]*ExtensionBinary, error) {
errorFormat := "GetExtensionBinaries: %v"

r, err := e.c.NewAuthenticatedRequest(ctx, "GET", fmt.Sprintf("%s/plugins/%d/binaries", ApiUrl, extensionId), nil)
r, err := e.c.NewAuthenticatedRequest(ctx, "GET", fmt.Sprintf("%s/producers/%d/plugins/%d/binaries", ApiUrl, e.producerId, extensionId), nil)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}
Expand All @@ -71,15 +82,15 @@ func (e ProducerEndpoint) GetExtensionBinaries(ctx context.Context, extensionId
return binaries, nil
}

func (e ProducerEndpoint) UpdateExtensionBinaryInfo(ctx context.Context, extensionId int, binary ExtensionBinary) error {
func (e ProducerEndpoint) UpdateExtensionBinaryInfo(ctx context.Context, extensionId int, update ExtensionUpdate) error {
errorFormat := "UpdateExtensionBinaryInfo: %v"

content, err := json.Marshal(binary)
content, err := json.Marshal(update)
if err != nil {
return fmt.Errorf(errorFormat, err)
}

r, err := e.c.NewAuthenticatedRequest(ctx, "PUT", fmt.Sprintf("%s/plugins/%d/binaries/%d", ApiUrl, extensionId, binary.Id), bytes.NewReader(content))
r, err := e.c.NewAuthenticatedRequest(ctx, "PUT", fmt.Sprintf("%s/producers/%d/plugins/%d/binaries/%d", ApiUrl, e.producerId, extensionId, update.Id), bytes.NewReader(content))
if err != nil {
return fmt.Errorf(errorFormat, err)
}
Expand All @@ -89,51 +100,30 @@ func (e ProducerEndpoint) UpdateExtensionBinaryInfo(ctx context.Context, extensi
return err
}

func (e ProducerEndpoint) CreateExtensionBinaryFile(ctx context.Context, extensionId int, zipPath string) (*ExtensionBinary, error) {
errorFormat := "CreateExtensionBinaryFile: %v"

var b bytes.Buffer
w := multipart.NewWriter(&b)

fileWritter, err := w.CreateFormFile("file", filepath.Base(zipPath))
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

zipFile, err := os.Open(zipPath)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

_, err = io.Copy(fileWritter, zipFile)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}
func (e ProducerEndpoint) CreateExtensionBinary(ctx context.Context, extensionId int, create ExtensionCreate) (*ExtensionBinary, error) {
errorFormat := "CreateExtensionBinary: %v"

err = w.Close()
createPayload, err := json.Marshal(create)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

r, err := e.c.NewAuthenticatedRequest(ctx, "POST", fmt.Sprintf("%s/plugins/%d/binaries", ApiUrl, extensionId), &b)
r, err := e.c.NewAuthenticatedRequest(ctx, "POST", fmt.Sprintf("%s/producers/%d/plugins/%d/binaries", ApiUrl, e.producerId, extensionId), bytes.NewReader(createPayload))
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

r.Header.Set("content-type", w.FormDataContentType())

content, err := e.c.doRequest(r)
if err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

// For some reasons this API responses a array of binaries
var binary []*ExtensionBinary
var binary *ExtensionBinary
if err := json.Unmarshal(content, &binary); err != nil {
return nil, fmt.Errorf(errorFormat, err)
}

return binary[0], nil
return binary, nil
}

func (e ProducerEndpoint) UpdateExtensionBinaryFile(ctx context.Context, extensionId, binaryId int, zipPath string) error {
Expand Down Expand Up @@ -161,7 +151,7 @@ func (e ProducerEndpoint) UpdateExtensionBinaryFile(ctx context.Context, extensi
return fmt.Errorf(errorFormat, err)
}

r, err := e.c.NewAuthenticatedRequest(ctx, "POST", fmt.Sprintf("%s/plugins/%d/binaries/%d/file", ApiUrl, extensionId, binaryId), &b)
r, err := e.c.NewAuthenticatedRequest(ctx, "POST", fmt.Sprintf("%s/producers/%d/plugins/%d/binaries/%d/file", ApiUrl, e.producerId, extensionId, binaryId), &b)
if err != nil {
return fmt.Errorf(errorFormat, err)
}
Expand Down Expand Up @@ -428,3 +418,24 @@ func (list SoftwareVersionList) FilterOnVersion(constriant *version.Constraints)

return newList
}

func (list SoftwareVersionList) FilterOnVersionStringList(constriant *version.Constraints) []string {
newList := make([]string, 0)

for _, swVersion := range list {
if !swVersion.Selectable {
continue
}

v, err := version.NewVersion(swVersion.Name)
if err != nil {
continue
}

if constriant.Check(v) {
newList = append(newList, swVersion.Name)
}
}

return newList
}
43 changes: 29 additions & 14 deletions cmd/account/account_producer_extension_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ var accountCompanyProducerExtensionUploadCmd = &cobra.Command{
}
}

if foundBinary == nil {
foundBinary, err = p.CreateExtensionBinaryFile(cmd.Context(), ext.Id, path)
if err != nil {
return fmt.Errorf("create extension binary: %w", err)
}
} else {
logging.FromContext(cmd.Context()).Infof("Found a zip with version %s already. Updating it", zipVersion)
}

changelog, err := zipExt.GetChangelog()
if err != nil {
return err
Expand All @@ -85,12 +76,36 @@ var accountCompanyProducerExtensionUploadCmd = &cobra.Command{
return err
}

foundBinary.Version = zipVersion.String()
foundBinary.Changelogs[0].Text = changelog.German
foundBinary.Changelogs[1].Text = changelog.English
foundBinary.CompatibleSoftwareVersions = avaiableVersions.FilterOnVersion(constraint)
if foundBinary == nil {
create := account_api.ExtensionCreate{
Version: zipVersion.String(),
SoftwareVersions: avaiableVersions.FilterOnVersionStringList(constraint),
Changelogs: []account_api.ExtensionUpdateChangelog{
{Locale: "de_DE", Text: changelog.German},
{Locale: "en_GB", Text: changelog.English},
},
}

foundBinary, err = p.CreateExtensionBinary(cmd.Context(), ext.Id, create)
if err != nil {
return fmt.Errorf("create extension binary: %w", err)
}

logging.FromContext(cmd.Context()).Infof("Created new binary with version %s", zipVersion)
} else {
logging.FromContext(cmd.Context()).Infof("Found a zip with version %s already. Updating it", zipVersion)
}

update := account_api.ExtensionUpdate{
Id: foundBinary.Id,
SoftwareVersions: avaiableVersions.FilterOnVersionStringList(constraint),
Changelogs: []account_api.ExtensionUpdateChangelog{
{Locale: "de_DE", Text: changelog.German},
{Locale: "en_GB", Text: changelog.English},
},
}

err = p.UpdateExtensionBinaryInfo(cmd.Context(), ext.Id, *foundBinary)
err = p.UpdateExtensionBinaryInfo(cmd.Context(), ext.Id, update)
if err != nil {
return err
}
Expand Down

0 comments on commit 01cc082

Please sign in to comment.