Skip to content

Commit

Permalink
[api/repos] Add copy API
Browse files Browse the repository at this point in the history
  • Loading branch information
vindict-gh authored and neolynx committed Jun 15, 2024
1 parent 0f5eec3 commit dbfc0e4
Showing 1 changed file with 136 additions and 1 deletion.
137 changes: 136 additions & 1 deletion api/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -416,7 +417,141 @@ func apiReposPackageFromDir(c *gin.Context) {

// POST /repos/:name/copy/:src/:file
func apiReposCopyPackage(c *gin.Context) {
// TODO
dstRepoName := c.Params.ByName("name")
srcRepoName := c.Params.ByName("src")

jsonBody := struct {
WithDeps bool `json:"with-deps,omitempty"`
DryRun bool `json:"dry-run,omitempty"`
}{
WithDeps: false,
DryRun: false,
}

err := c.Bind(&jsonBody)
if err != nil {
return
}

Check warning on line 434 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L419-L434

Added lines #L419 - L434 were not covered by tests

collectionFactory := context.NewCollectionFactory()
dstRepo, err := collectionFactory.LocalRepoCollection().ByName(dstRepoName)
if err != nil {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("dest repo error: %s", err))
return
}

Check warning on line 441 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L436-L441

Added lines #L436 - L441 were not covered by tests

err = collectionFactory.LocalRepoCollection().LoadComplete(dstRepo)
if err != nil {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("dest repo error: %s", err))
return
}

Check warning on line 447 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L443-L447

Added lines #L443 - L447 were not covered by tests

var (
srcRefList *deb.PackageRefList
srcRepo *deb.LocalRepo
)

srcRepo, err = collectionFactory.LocalRepoCollection().ByName(srcRepoName)
if err != nil {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("src repo error: %s", err))
return
}

Check warning on line 458 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L449-L458

Added lines #L449 - L458 were not covered by tests

if srcRepo.UUID == dstRepo.UUID {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("dest and source are identical"))
return
}

Check warning on line 463 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L460-L463

Added lines #L460 - L463 were not covered by tests

err = collectionFactory.LocalRepoCollection().LoadComplete(srcRepo)
if err != nil {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("src repo error: %s", err))
return
}

Check warning on line 469 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L465-L469

Added lines #L465 - L469 were not covered by tests

srcRefList = srcRepo.RefList()
taskName := fmt.Sprintf("Copy packages from repo %s to repo %s", srcRepoName, dstRepoName)
resources := []string{string(dstRepo.Key()), string(srcRepo.Key())}

maybeRunTaskInBackground(c, taskName, resources, func(_ aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) {
reporter := &aptly.RecordingResultReporter{
Warnings: []string{},
AddedLines: []string{},
RemovedLines: []string{},
}

dstList, err := deb.NewPackageListFromRefList(dstRepo.RefList(), collectionFactory.PackageCollection(), context.Progress())
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to load packages in dest: %s", err)

}

Check warning on line 486 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L471-L486

Added lines #L471 - L486 were not covered by tests

srcList, err := deb.NewPackageListFromRefList(srcRefList, collectionFactory.PackageCollection(), context.Progress())
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to load packages in src: %s", err)
}

Check warning on line 491 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L488-L491

Added lines #L488 - L491 were not covered by tests

srcList.PrepareIndex()

var architecturesList []string

if jsonBody.WithDeps {
dstList.PrepareIndex()

// Calculate architectures
if len(context.ArchitecturesList()) > 0 {
architecturesList = context.ArchitecturesList()
} else {
architecturesList = dstList.Architectures(false)
}

Check warning on line 505 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L493-L505

Added lines #L493 - L505 were not covered by tests

sort.Strings(architecturesList)

if len(architecturesList) == 0 {
return &task.ProcessReturnValue{Code: http.StatusUnprocessableEntity, Value: nil}, fmt.Errorf("unable to determine list of architectures, please specify explicitly")
}

Check warning on line 511 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L507-L511

Added lines #L507 - L511 were not covered by tests
}

// srcList.Filter|FilterWithProgress only accept query list
queries := make([]deb.PackageQuery, 1)
queries[0], err = query.Parse(c.Params.ByName("file"))
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusUnprocessableEntity, Value: nil}, fmt.Errorf("unable to parse query: %s", err)
}

Check warning on line 519 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L515-L519

Added lines #L515 - L519 were not covered by tests

toProcess, err := srcList.FilterWithProgress(queries, jsonBody.WithDeps, dstList, context.DependencyOptions(), architecturesList, context.Progress())
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("filter error: %s", err)
}

Check warning on line 524 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L521-L524

Added lines #L521 - L524 were not covered by tests

err = toProcess.ForEach(func(p *deb.Package) error {
err = dstList.Add(p)
if err != nil {
return err
}

Check warning on line 530 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L526-L530

Added lines #L526 - L530 were not covered by tests

name := fmt.Sprintf("added %s-%s(%s)", p.Name, p.Version, p.Architecture)
reporter.AddedLines = append(reporter.AddedLines, name)
return nil

Check warning on line 534 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L532-L534

Added lines #L532 - L534 were not covered by tests
})
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("error processing dest add: %s", err)
}

Check warning on line 538 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L536-L538

Added lines #L536 - L538 were not covered by tests

if jsonBody.DryRun {
reporter.Warning("Changes not saved, as dry run has been requested")
} else {
dstRepo.UpdateRefList(deb.NewPackageRefListFromPackageList(dstList))

err = collectionFactory.LocalRepoCollection().Update(dstRepo)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save: %s", err)
}

Check warning on line 548 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L540-L548

Added lines #L540 - L548 were not covered by tests
}

return &task.ProcessReturnValue{Code: http.StatusOK, Value: gin.H{
"Report": reporter,
}}, nil

Check warning on line 553 in api/repos.go

View check run for this annotation

Codecov / codecov/patch

api/repos.go#L551-L553

Added lines #L551 - L553 were not covered by tests
})
}

// POST /repos/:name/include/:dir/:file
Expand Down

0 comments on commit dbfc0e4

Please sign in to comment.