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 Dec 13, 2023
1 parent 8ee1109 commit e1b29fc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
9 changes: 6 additions & 3 deletions addon/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ func (h *Task) Attach(f *api.File) {
f.Path)
h.report.Files = append(
h.report.Files,
api.Ref{
ID: f.ID,
Name: f.Name,
api.FileRef{
Index: len(h.report.Activity),
Ref: api.Ref{
ID: f.ID,
Name: f.Name,
},
})
h.pushReport()
return
Expand Down
61 changes: 59 additions & 2 deletions api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
tasking "github.com/konveyor/tackle2-hub/task"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"io/ioutil"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"net/http"
"strings"
"time"
"sort"
)

//
Expand Down Expand Up @@ -83,6 +86,11 @@ func (h TaskHandler) Get(ctx *gin.Context) {
}
r := Task{}
r.With(task)
err := r.injectFiles(h.DB(ctx))
if err != nil {
_ = ctx.Error(result.Error)
return
}

h.Respond(ctx, http.StatusOK, r)
}
Expand Down Expand Up @@ -530,7 +538,7 @@ type Task struct {
Purged bool `json:"purged,omitempty" yaml:",omitempty"`
Errors []TaskError `json:"errors,omitempty" yaml:",omitempty"`
Activity []string `json:"activity,omitempty" yaml:",omitempty"`
Files []Ref `json:"files" yaml:",omitempty"`
Files []FileRef `json:"files" yaml:",omitempty"`
}

//
Expand Down Expand Up @@ -599,6 +607,48 @@ func (r *Task) Model() (m *model.Task) {
return
}

//
// injectFiles inject files into the activity.
func (r *Task) injectFiles(db *gorm.DB) (err error) {
injected := 0
sort.Slice(
r.Files,
func(i, j int) bool {
return r.Files[i].Index < r.Files[j].Index
})
for _, ref := range r.Files {
Log.Info("REF", "index", ref.Index)
if ref.Index == 0 {
continue
}
ref.Index += injected
if ref.Index > len(r.Activity) {
continue
}
m := &model.File{}
err = db.First(m, ref.ID).Error
if err != nil {
return
}
b, nErr := ioutil.ReadFile(m.Path)
if nErr != nil {
err = nErr
return
}
var content []string
for _, s := range strings.Split(string(b), "\n") {
content = append(
content,
"> "+s)
}
r.Activity = append(
append(r.Activity[:ref.Index], content...),
r.Activity[ref.Index:]...)
injected += len(content)
}
return
}

//
// TaskReport REST resource.
type TaskReport struct {
Expand All @@ -608,7 +658,7 @@ type TaskReport struct {
Total int `json:"total,omitempty" yaml:",omitempty"`
Completed int `json:"completed,omitempty" yaml:",omitempty"`
Activity []string `json:"activity,omitempty" yaml:",omitempty"`
Files []Ref `json:"files,omitempty" yaml:",omitempty"`
Files []FileRef `json:"files,omitempty" yaml:",omitempty"`
Result interface{} `json:"result,omitempty" yaml:",omitempty" swaggertype:"object"`
TaskID uint `json:"task"`
}
Expand Down Expand Up @@ -663,3 +713,10 @@ func (r *TaskReport) Model() (m *model.TaskReport) {

return
}

//
// FileRef task report file ref.
type FileRef struct {
Ref `yaml:",inline"`
Index int `json:"index,omitempty" yaml:",omitempty"`
}

0 comments on commit e1b29fc

Please sign in to comment.