Skip to content

Commit

Permalink
fix: Wrong version order in package diff
Browse files Browse the repository at this point in the history
* fix: Incorrect version diffing

* fix: Add request methods to README
  • Loading branch information
matbme authored Jul 15, 2024
1 parent 69d0f44 commit d5e7271
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 72 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Install dependencies
run: |
Expand All @@ -38,7 +38,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Install test dependencies
run: |
Expand All @@ -47,4 +47,4 @@ jobs:
- name: Test
run: |
go test -v ./...
go test -v ./.../...
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ all: clean differ
differ:
go build -o differ -tags="sonic avx" .

test:
go test -v ./.../...

.PHONY: clean

clean:
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ $ podman run --env 'admin_user=user' --env 'admin_password=password' differ path
Simple check to see if the server is running correctly.
*Method:* `GET`
*Endpoint:* `http://[base_url]/status`
*Parameters:* None
Expand All @@ -71,6 +72,7 @@ For example, a distro can ship both GNOME and a KDE versions as separate images,
Creates a new image in the dabatase. Every release (see subsection below) is attached to an image.
*Method:* `POST`
*Endpoint:* `http://[base_url]/images/new`
*Parameters:*
Expand All @@ -93,7 +95,8 @@ Creates a new image in the dabatase. Every release (see subsection below) is att
Retrieves information about an image given its name.
**Endpoint:* `http://[base_url]/images/[name]`
*Method:* `GET`
*Endpoint:* `http://[base_url]/images/[name]`
*Parameters:* None
Expand All @@ -119,6 +122,7 @@ Retrieves information about an image given its name.
Retrieves information about all images.
*Method:* `GET`
*Endpoint:* `http://[base_url]/images`
*Parameters:* None
Expand Down Expand Up @@ -156,6 +160,7 @@ A release is a new version of some image, where packages can be added, removed,
Creates a new release for the given image.
*Method:* `POST`
*Endpoint:*`http://[base_url]/images/[image]/new`
*Parameters:*
Expand Down Expand Up @@ -184,6 +189,7 @@ Creates a new release for the given image.
Retrieves the most recent release from the image.
*Method:* `GET`
*Endpoint:* `http://[base_url]/images/[image]/latest`
*Parameters:* None
Expand All @@ -208,6 +214,7 @@ Retrieves the most recent release from the image.
Searches for a specific release by its digest.
*Method:* `GET`
*Endpoint:* `http://[base_url]/images/[image]/[digest]`
*Parameters:* None
Expand All @@ -234,6 +241,7 @@ Searches for a specific release by its digest.
The most important endpoint in the API. Given two digests, generates a list of changed packages. This information is cached so future queries are nearly instant.
*Method:* `GET`
*Endpoint:* `http://[base_url]/images/[image]/diff`
*Parameters:*
Expand Down
2 changes: 1 addition & 1 deletion core/handlers/handle_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func HandleGetReleaseDiff(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
added, upgraded, downgraded, removed := newRelease.DiffPackages(oldRelease)
added, upgraded, downgraded, removed := oldRelease.DiffPackages(newRelease)

cacheDiffEntry := struct {
Added, Upgraded, Downgraded, Removed []diff.PackageDiff
Expand Down
40 changes: 23 additions & 17 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"sync"
)

const (
Upgraded = -1
Downgraded = 1
Added = 2
)

type Package map[string]string

type PackageDiff struct {
Expand Down Expand Up @@ -51,15 +57,15 @@ func CompareVersions(a, b string) int {
// If a has component but b doesn't, package was upgraded, unless it's prerelease
if aOk && !bOk {
if comp == "prerelease" {
compResult = -1
compResult = Downgraded
} else {
compResult = 1
compResult = Upgraded
}
break
}
// If b has component but a doesn't, package was downgraded
if !aOk && bOk {
compResult = -1
compResult = Downgraded
break
}

Expand All @@ -73,11 +79,10 @@ func CompareVersions(a, b string) int {
} else {
abComp = cmp.Compare(aValue, bValue)
}
if abComp == 0 {
continue
if abComp != 0 {
compResult = abComp
break
}
compResult = abComp
break
}

return compResult
Expand All @@ -86,7 +91,8 @@ func CompareVersions(a, b string) int {
func processPackage(pkg, oldVersion, newVersion string, c chan<- struct {
PackageDiff
int
}, wg *sync.WaitGroup) {
}, wg *sync.WaitGroup,
) {
defer wg.Done()

result := CompareVersions(oldVersion, newVersion)
Expand All @@ -103,27 +109,27 @@ func DiffPackages(oldPackages, newPackages Package) ([]PackageDiff, []PackageDif
c := make(chan struct {
PackageDiff
int
}, len(oldPackages))
}, len(newPackages))

for pkg, oldVersion := range oldPackages {
for pkg, newVersion := range newPackages {
wg.Add(1)

if newVersion, ok := newPackages[pkg]; ok {
if oldVersion, ok := oldPackages[pkg]; ok {
go processPackage(pkg, oldVersion, newVersion, c, &wg)
} else {
c <- struct {
PackageDiff
int
}{PackageDiff{pkg, oldVersion, ""}, 2}
}{PackageDiff{pkg, newVersion, ""}, Added}
wg.Done()
}
}

removed := []PackageDiff{}
wg.Add(1)
go func() {
for pkg, version := range newPackages {
if _, ok := oldPackages[pkg]; !ok {
for pkg, version := range oldPackages {
if _, ok := newPackages[pkg]; !ok {
removed = append(removed, PackageDiff{pkg, "", version})
}
}
Expand All @@ -140,11 +146,11 @@ func DiffPackages(oldPackages, newPackages Package) ([]PackageDiff, []PackageDif
downgraded := []PackageDiff{}
for pkgResult := range c {
switch pkgResult.int {
case -1:
case Downgraded:
downgraded = append(downgraded, pkgResult.PackageDiff)
case 1:
case Upgraded:
upgraded = append(upgraded, pkgResult.PackageDiff)
case 2:
case Added:
added = append(added, pkgResult.PackageDiff)
}
}
Expand Down
49 changes: 49 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package diff

import (
"fmt"
"slices"
"testing"
)

func TestDiffPackages(t *testing.T) {
newPackages := Package{
"libei1": "1.2.1-1",
"xdg-desktop-portal-gnome": "46.2-1",
"libdisplay-info1": "0.1.1-1",
}

oldPackages := Package{
"libwinpr2-2t64": "2.11.5+dfsg1-1",
"xdg-desktop-portal-gnome": "44.2-4+b1",
"libdisplay-info1": "0.1.1-2+b1",
}

expected := struct {
added, upgraded, downgraded, removed []PackageDiff
}{
[]PackageDiff{{Name: "libei1", NewVersion: "1.2.1-1"}},
[]PackageDiff{{Name: "xdg-desktop-portal-gnome", NewVersion: "46.2-1", PreviousVersion: "44.2-4+b1"}},
[]PackageDiff{{Name: "libdisplay-info1", NewVersion: "0.1.1-1", PreviousVersion: "0.1.1-2+b1"}},
[]PackageDiff{{Name: "libwinpr2-2t64", PreviousVersion: "2.11.5+dfsg1-1"}},
}

added, upgraded, downgraded, removed := DiffPackages(oldPackages, newPackages)

if !slices.Equal(expected.added, added) {
fmt.Printf("Incorrect parsing of added. Expected %v, got %v\n", expected.added, added)
t.Fail()
}
if !slices.Equal(expected.upgraded, upgraded) {
fmt.Printf("Incorrect parsing of upgraded. Expected %v, got %v\n", expected.upgraded, upgraded)
t.Fail()
}
if !slices.Equal(expected.downgraded, downgraded) {
fmt.Printf("Incorrect parsing of downgraded. Expected %v, got %v\n", expected.downgraded, downgraded)
t.Fail()
}
if !slices.Equal(expected.removed, removed) {
fmt.Printf("Incorrect parsing of removed. Expected %v, got %v\n", expected.removed, removed)
t.Fail()
}
}
50 changes: 0 additions & 50 deletions types/release_test.go

This file was deleted.

0 comments on commit d5e7271

Please sign in to comment.