Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Aug 9, 2024
1 parent 92bde32 commit 277b7e1
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 140 deletions.
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
60 changes: 32 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,37 @@ 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}
f, err := file.Post(issues)
if err != nil {
return
}
r.Issues = api.Ref{ID: f.ID}
f, err = file.Post(deps)
if err != nil {
return
}
r.Dependencies = api.Ref{ID: f.ID}
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

0 comments on commit 277b7e1

Please sign in to comment.