Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve tag conflicts for versioned artifacts #920

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pkg/controlplane/handlers_githubwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/google/go-github/v53/github"
"github.com/google/uuid"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"golang.org/x/oauth2"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -650,6 +651,50 @@ func upsertVersionedArtifact(
log.Printf("error removing older artifact versions: %v", err)
}

// To avoid conflicts, we search for all existing entries that have the incoming tag in their Tags field.
// If found, the existing artifact is updated by removing the incoming tag from its tags column.
// Loop through all incomming tags
for _, incomingTag := range versionedArtifact.Version.Tags {
rdimitrov marked this conversation as resolved.
Show resolved Hide resolved
// Search artifact versions having the incomming tag (there should be at most 1 or no matches at all)
existingArtifactVersions, err := qtx.ListArtifactVersionsByArtifactIDAndTag(ctx,
db.ListArtifactVersionsByArtifactIDAndTagParams{ArtifactID: dbArtifact.ID,
Tags: sql.NullString{Valid: true, String: incomingTag},
Limit: sql.NullInt32{Valid: false, Int32: 0}})
if err != nil {
rdimitrov marked this conversation as resolved.
Show resolved Hide resolved
if errors.Is(err, sql.ErrNoRows) {
// There're no tagged versions matching the incoming tag, all okay
continue
}
rdimitrov marked this conversation as resolved.
Show resolved Hide resolved
// Unexpected failure
return nil, nil, fmt.Errorf("failed during repository synchronization: %w", err)
}
// Loop through all artifact versions that matched the incoming tag
for _, existing := range existingArtifactVersions {
if !existing.Tags.Valid {
continue
}
// Rebuild the Tags list removing anything that would conflict
newTags := slices.DeleteFunc(strings.Split(existing.Tags.String, ","), func(in string) bool { return in == incomingTag })
// Update the versioned artifact row in the store (we should't change anything else except the tags value)
_, err := qtx.UpsertArtifactVersion(ctx, db.UpsertArtifactVersionParams{
ArtifactID: existing.ArtifactID,
Version: existing.Version,
Tags: sql.NullString{
String: strings.Join(newTags, ","),
Valid: true,
},
Sha: existing.Sha,
CreatedAt: existing.CreatedAt,
SignatureVerification: existing.SignatureVerification.RawMessage,
GithubWorkflow: existing.GithubWorkflow.RawMessage,
})
if err != nil {
return nil, nil, fmt.Errorf("error upserting artifact version: %w", err)
}
}
}

// Proceed storing the new versioned artifact
dbVersion, err := qtx.UpsertArtifactVersion(ctx, db.UpsertArtifactVersionParams{
ArtifactID: dbArtifact.ID,
Version: versionedArtifact.Version.VersionId,
Expand Down