Skip to content

Commit

Permalink
Fix test cases.
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Fiehe <[email protected]>
  • Loading branch information
Christoph Fiehe committed Oct 11, 2024
1 parent c0a11d5 commit 44dc5e4
Show file tree
Hide file tree
Showing 30 changed files with 935 additions and 293 deletions.
76 changes: 41 additions & 35 deletions api/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func apiPublishList(c *gin.Context) {
func apiPublishShow(c *gin.Context) {
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -179,7 +179,7 @@ type publishedRepoCreateParams struct {
// @Failure 500 {object} Error "Internal Error"
// @Router /api/publish/{prefix} [post]
func apiPublishRepoOrSnapshot(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)

var b publishedRepoCreateParams
Expand All @@ -196,7 +196,7 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
}

if len(b.Sources) == 0 {
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("unable to publish: soures are empty"))
AbortWithJSONError(c, http.StatusBadRequest, fmt.Errorf("unable to publish: sources are empty"))
return
}

Expand Down Expand Up @@ -346,7 +346,7 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {
func apiPublishUpdateSwitch(c *gin.Context) {
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := utils.SanitizePath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

var b struct {
ForceOverwrite bool
Expand Down Expand Up @@ -438,24 +438,19 @@ func apiPublishUpdateSwitch(c *gin.Context) {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save to DB: %s", err)
}

updatedComponents := result.UpdatedComponents()
removedComponents := result.RemovedComponents()

if b.SkipCleanup == nil || !*b.SkipCleanup {
publishedStorage := context.GetPublishedStorage(storage)

err = collection.CleanupPrefixComponentFiles(published.Prefix, updatedComponents, publishedStorage, collectionFactory, out)
err = collection.CleanupPrefixComponentFiles(published.Prefix, result.UpdatedComponents(), publishedStorage, collectionFactory, out)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
}

if len(removedComponents) > 0 {
// Cleanup files belonging to a removed component by dropping the component directory from the storage backend.
for _, component := range removedComponents {
err = publishedStorage.RemoveDirs(filepath.Join(prefix, "dists", distribution, component), out)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
}
// Cleanup files belonging to a removed component by dropping the component directory from the storage backend.
for _, component := range result.RemovedComponents() {
err = publishedStorage.RemoveDirs(filepath.Join(prefix, "dists", distribution, component), out)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
}
}
}
Expand Down Expand Up @@ -484,7 +479,7 @@ func apiPublishDrop(c *gin.Context) {

param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -515,9 +510,9 @@ func apiPublishSourcesCreate(c *gin.Context) {
b sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -560,15 +555,15 @@ func apiPublishSourcesCreate(c *gin.Context) {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save to DB: %s", err)
}

return &task.ProcessReturnValue{Code: http.StatusOK, Value: published}, nil
return &task.ProcessReturnValue{Code: http.StatusCreated, Value: published}, nil
})
}

// @Router /api/publish/{prefix}/{distribution}/sources [get]
func apiPublishSourcesList(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -601,9 +596,9 @@ func apiPublishSourcesUpdate(c *gin.Context) {
b []sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -648,26 +643,37 @@ func apiPublishSourcesUpdate(c *gin.Context) {

// @Router /api/publish/{prefix}/{distribution}/sources [delete]
func apiPublishSourcesDelete(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()

published, err := collection.ByStoragePrefixDistribution(storage, prefix, distribution)
if err != nil {
AbortWithJSONError(c, http.StatusNotFound, fmt.Errorf("unable to show: %s", err))
AbortWithJSONError(c, http.StatusNotFound, fmt.Errorf("unable to delete: %s", err))
return
}

err = collection.LoadComplete(published, collectionFactory)
if err != nil {
AbortWithJSONError(c, http.StatusInternalServerError, fmt.Errorf("unable to show: %s", err))
AbortWithJSONError(c, http.StatusInternalServerError, fmt.Errorf("unable to delete: %s", err))
return
}

published.DropRevision()

resources := []string{string(published.Key())}
taskName := fmt.Sprintf("Update published %s repository %s/%s", published.SourceKind, published.StoragePrefix(), published.Distribution)
maybeRunTaskInBackground(c, taskName, resources, func(out aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) {
err = collection.Update(published)
if err != nil {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to save to DB: %s", err)
}

return &task.ProcessReturnValue{Code: http.StatusOK, Value: published}, nil
})
}

// @Router /api/publish/{prefix}/{distribution}/sources/{component} [put]
Expand All @@ -677,10 +683,10 @@ func apiPublishSourceUpdate(c *gin.Context) {
b sourceParams
)

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
component := parseEscapedPath(c.Params.ByName("component"))
distribution := slashEscape(c.Params.ByName("distribution"))
component := slashEscape(c.Params.ByName("component"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -737,10 +743,10 @@ func apiPublishSourceUpdate(c *gin.Context) {
func apiPublishSourceDelete(c *gin.Context) {
var err error

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
component := parseEscapedPath(c.Params.ByName("component"))
distribution := slashEscape(c.Params.ByName("distribution"))
component := slashEscape(c.Params.ByName("component"))

collectionFactory := context.NewCollectionFactory()
collection := collectionFactory.PublishedRepoCollection()
Expand Down Expand Up @@ -776,9 +782,9 @@ func apiPublishSourceDelete(c *gin.Context) {

// @Router /api/publish/{prefix}/{distribution}/update [post]
func apiPublishUpdate(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := parseEscapedPath(c.Params.ByName("distribution"))
distribution := slashEscape(c.Params.ByName("distribution"))

var b struct {
AcquireByHash *bool
Expand Down
7 changes: 5 additions & 2 deletions cmd/publish_source_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ func aptlyPublishSourceAdd(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceAdd() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceAdd,
UsageLine: "add <distribution> <source>",
Short: "add package source to published repository",
Short: "add source to staged source list of published repository",
Long: `
The command adds (in place) one or multiple package sources to a published repository.
The command adds sources to the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be modified. The number of given components must be
Expand Down
6 changes: 3 additions & 3 deletions cmd/publish_source_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func makeCmdPublishSourceDrop() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceDrop,
UsageLine: "drop <distribution>",
Short: "drops revision of published repository",
Short: "drops staged source changes of published repository",
Long: `
Command drops revision of a published repository.
Command drops the staged source changes of the published repository.
Example:
$ aptly publish revision drop wheezy
$ aptly publish source drop wheezy
`,
Flag: *flag.NewFlagSet("aptly-publish-revision-create", flag.ExitOnError),
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/publish_source_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func aptlyPublishSourceRemove(cmd *commander.Command, args []string) error {
for _, component := range components {
name, exists := sources[component]
if !exists {
return fmt.Errorf("unable to remove: Component %q is not part of revision", component)
return fmt.Errorf("unable to remove: component %q does not exist", component)
}
context.Progress().Printf("Removing component %q with source %q [%s]...\n", component, name, published.SourceKind)

Expand All @@ -54,16 +54,19 @@ func aptlyPublishSourceRemove(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceRemove() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceRemove,
UsageLine: "remove <distribution> [[<endpoint>:]<prefix>] <source>",
Short: "remove package source to published repository",
Short: "remove source from staged source list of published repository",
Long: `
The command removes one or multiple components from a published repository.
The command removes sources from the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be removed, e.g.:
Expand Down
7 changes: 5 additions & 2 deletions cmd/publish_source_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,19 @@ func aptlyPublishSourceUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to save to DB: %s", err)
}

context.Progress().Printf("\nYou can run 'aptly publish update %s %s' to update the content of the published repository.\n",
distribution, published.StoragePrefix())

return err
}

func makeCmdPublishSourceUpdate() *commander.Command {
cmd := &commander.Command{
Run: aptlyPublishSourceUpdate,
UsageLine: "update <distribution> <source>",
Short: "update package source to published repository",
Short: "update source in staged source list of published repository",
Long: `
The command updates one or multiple components in a published repository.
The command updates sources in the staged source list of the published repository.
The flag -component is mandatory. Use a comma-separated list of components, if
multiple components should be modified. The number of given components must be
Expand Down
55 changes: 18 additions & 37 deletions cmd/publish_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to switch: %s", err)
}

if published.SourceKind != deb.SourceSnapshot {
return fmt.Errorf("unable to switch: not a published snapshot repository")
}

err = collectionFactory.PublishedRepoCollection().LoadComplete(published, collectionFactory)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
Expand All @@ -57,46 +61,23 @@ func aptlyPublishSwitch(cmd *commander.Command, args []string) error {
return fmt.Errorf("mismatch in number of components (%d) and snapshots (%d)", len(components), len(names))
}

if published.SourceKind == deb.SourceLocalRepo {
localRepoCollection := collectionFactory.LocalRepoCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

localRepo, err := localRepoCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

err = localRepoCollection.LoadComplete(localRepo)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
snapshotCollection := collectionFactory.SnapshotCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

published.UpdateLocalRepo(component, localRepo)
snapshot, err := snapshotCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
} else if published.SourceKind == deb.SourceSnapshot {
snapshotCollection := collectionFactory.SnapshotCollection()
for i, component := range components {
if !utils.StrSliceHasItem(publishedComponents, component) {
return fmt.Errorf("unable to switch: component %s does not exist in published repository", component)
}

snapshot, err := snapshotCollection.ByName(names[i])
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

err = snapshotCollection.LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}

published.UpdateSnapshot(component, snapshot)

err = snapshotCollection.LoadComplete(snapshot)
if err != nil {
return fmt.Errorf("unable to switch: %s", err)
}
} else {
return fmt.Errorf("unknown published repository type")

published.UpdateSnapshot(component, snapshot)
}

signer, err := getSigner(context.Flags())
Expand Down
12 changes: 11 additions & 1 deletion cmd/publish_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"path/filepath"

"github.com/aptly-dev/aptly/deb"
"github.com/smira/commander"
Expand Down Expand Up @@ -76,11 +77,20 @@ func aptlyPublishUpdate(cmd *commander.Command, args []string) error {

skipCleanup := context.Flags().Lookup("skip-cleanup").Value.Get().(bool)
if !skipCleanup {
publishedStorage := context.GetPublishedStorage(storage)
err = collectionFactory.PublishedRepoCollection().CleanupPrefixComponentFiles(published.Prefix, result.UpdatedComponents(),
context.GetPublishedStorage(storage), collectionFactory, context.Progress())
publishedStorage, collectionFactory, context.Progress())
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}

// Cleanup files belonging to a removed component by dropping the component directory from the storage backend.
for _, component := range result.RemovedComponents() {
err = publishedStorage.RemoveDirs(filepath.Join(prefix, "dists", distribution, component), context.Progress())
if err != nil {
return fmt.Errorf("unable to update: %s", err)
}
}
}

context.Progress().Printf("\nPublished %s repository %s has been successfully updated.\n", published.SourceKind, published.String())
Expand Down
Loading

0 comments on commit 44dc5e4

Please sign in to comment.