Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Upload files instead of multi-part form. #742

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 20 additions & 32 deletions api/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (h AnalysisHandler) AppList(ctx *gin.Context) {
// @description - dependencies: file that multiple api.TechDependency resources.
// @tags analyses
// @produce json
// @success 201 {object} api.Analysis
// @success 201 {object} api.AnalysisManifest
// @router /application/{id}/analyses [post]
// @param id path int true "Application ID"
func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
Expand All @@ -339,35 +339,14 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
}
//
// Analysis
input, err := ctx.FormFile(FileField)
manifest := &AnalysisManifest{}
err := h.Bind(ctx, manifest)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
reader, err := input.Open()
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
defer func() {
_ = reader.Close()
}()
encoding := input.Header.Get(ContentType)
d, err := h.Decoder(ctx, encoding, reader)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
r := Analysis{}
err = d.Decode(&r)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
r.Commit = manifest.Commit
analysis := r.Model()
analysis.ApplicationID = id
analysis.CreateUser = h.BaseHandler.CurrentUser(ctx)
Expand All @@ -380,13 +359,14 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
}
//
// Issues
input, err = ctx.FormFile(IssueField)
file := &model.File{}
err = db.First(file, manifest.Issues.ID).Error
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
reader, err = input.Open()
reader, err := os.Open(file.Path)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
Expand All @@ -395,8 +375,8 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
defer func() {
_ = reader.Close()
}()
encoding = input.Header.Get(ContentType)
d, err = h.Decoder(ctx, encoding, reader)
encoding := ctx.Request.Header.Get(ContentType)
d, err := h.Decoder(ctx, encoding, reader)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
Expand Down Expand Up @@ -425,13 +405,14 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
}
//
// Dependencies
input, err = ctx.FormFile(DepField)
file = &model.File{}
err = db.First(file, manifest.Dependencies.ID).Error
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
return
}
reader, err = input.Open()
reader, err = os.Open(file.Path)
if err != nil {
err = &BadRequestError{err.Error()}
_ = ctx.Error(err)
Expand All @@ -440,7 +421,7 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
defer func() {
_ = reader.Close()
}()
encoding = input.Header.Get(ContentType)
encoding = ctx.Request.Header.Get(ContentType)
d, err = h.Decoder(ctx, encoding, reader)
if err != nil {
err = &BadRequestError{err.Error()}
Expand Down Expand Up @@ -2371,6 +2352,13 @@ type DepAppReport struct {
} `json:"dependency"`
}

// AnalysisManifest resource.
type AnalysisManifest struct {
Commit string `json:"commit,omitempty" yaml:",omitempty"`
Issues Ref `json:"issues"`
Dependencies Ref `json:"dependencies"`
}

// FactMap map.
type FactMap map[string]any

Expand Down
63 changes: 35 additions & 28 deletions binding/application.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package binding

import (
"bytes"
"errors"
"io"
"net/http"
"strconv"

mime "github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/binding"
liberr "github.com/jortel/go-utils/error"
"github.com/konveyor/tackle2-hub/api"
"gopkg.in/yaml.v2"
)

// Application API.
Expand Down Expand Up @@ -317,29 +313,40 @@ type Analysis struct {
}

// Create an analysis report.
func (h *Analysis) Create(r *api.Analysis, encoding string, issues, deps io.Reader) (err error) {
// The encoding must be:
// - application/json
// - application/x-yaml
// The `issues` is the path to a file containing api.Issue(s)
// as individual documents.
// The `deps` is the path to a file containing api.TechDependency(s)
// as individual documents.
func (h *Analysis) Create(commit, encoding, issues, deps string) (err error) {
switch encoding {
case "":
encoding = binding.MIMEJSON
case binding.MIMEJSON,
binding.MIMEYAML:
default:
err = liberr.New(
"Encoding: %s not supported",
encoding)
}
r := api.AnalysisManifest{Commit: commit}
file := File{client: h.client}
// post issues.
f, err := file.Post(issues)
if err != nil {
return
}
r.Issues = api.Ref{ID: f.ID}
// post deps.
f, err = file.Post(deps)
if err != nil {
return
}
r.Dependencies = api.Ref{ID: f.ID}
// post manifest.
path := Path(api.AppAnalysesRoot).Inject(Params{api.ID: h.appId})
b, _ := yaml.Marshal(r)
err = h.client.FileSend(
path,
http.MethodPost,
[]Field{
{
Name: api.FileField,
Reader: bytes.NewReader(b),
Encoding: mime.MIMEYAML,
},
{
Name: api.IssueField,
Encoding: encoding,
Reader: issues,
},
{
Name: api.DepField,
Encoding: encoding,
Reader: deps,
},
},
r)
err = h.client.Encoding(encoding).Post(path, r)
return
}
Loading
Loading