diff --git a/tests/version_test.go b/tests/version_test.go index bef680f..ec98474 100644 --- a/tests/version_test.go +++ b/tests/version_test.go @@ -57,10 +57,34 @@ func TestSameVersion(t *testing.T) { ctx, client, stop := setup() defer stop() - modID := RunVersionTest(ctx, t, client, "testdata/DuplicateMod.smod", false, "DuplicateMod", "", "") + modID, _ := RunVersionTest(ctx, t, client, "testdata/DuplicateMod.smod", false, "DuplicateMod", "", "") RunVersionTest(ctx, t, client, "testdata/DuplicateMod.smod", false, "DuplicateMod", modID, "this mod already has a version with this name") } +func TestSameSemver(t *testing.T) { + ctx, client, stop := setup() + defer stop() + + modID, versionID := RunVersionTest(ctx, t, client, "testdata/DuplicateMod.smod", false, "DuplicateMod", "", "") + //NEED TO SOMEHOW DELETE THE MOD + + token, _, err := makeUser(ctx) + testza.AssertNoError(t, err) + + deleteRequest := authRequest(`mutation DeleteVersion($versionId: VersionID!) { + deleteVersion(versionId: $versionId) + }`, token) + deleteRequest.Var("versionId", versionID) + + var deleteResponse struct { + DeleteVersion bool + } + testza.AssertNoError(t, client.Run(ctx, deleteRequest, &deleteResponse)) + testza.AssertTrue(t, deleteResponse.DeleteVersion) + + RunVersionTest(ctx, t, client, "testdata/DuplicateMod.smod", false, "DuplicateMod", modID, "this mod already has a version with this semver") +} + func TestModWithMissingDependency(t *testing.T) { RunVersionTestWrapper(t, "testdata/ModWithMissingDependency.smod", false, "ModWithMissingDependency", "ent: mod not found") } @@ -75,11 +99,11 @@ func RunVersionTestWrapper(t *testing.T, modFilePath string, executeVirusCheck b RunVersionTest(ctx, t, client, modFilePath, executeVirusCheck, modReference, "", expectError) } -func RunVersionTest(ctx context.Context, t *testing.T, client *graphql.Client, modFilePath string, executeVirusCheck bool, modReference string, reuseModID string, expectError string) string { +func RunVersionTest(ctx context.Context, t *testing.T, client *graphql.Client, modFilePath string, executeVirusCheck bool, modReference string, reuseModID string, expectError string) (string, string) { if executeVirusCheck && (!viper.IsSet("virustotal.key") || viper.GetString("virustotal.key") == "") { println("missing virustotal key from config, skipping") t.SkipNow() - return "" + return "", "" } viper.Set("skip-virus-check", !executeVirusCheck) @@ -394,5 +418,5 @@ func RunVersionTest(ctx context.Context, t *testing.T, client *graphql.Client, m }) } - return modID + return modID, versionID } diff --git a/workflows/versionupload/extract_mod_info.go b/workflows/versionupload/extract_mod_info.go index 6663fce..965996f 100644 --- a/workflows/versionupload/extract_mod_info.go +++ b/workflows/versionupload/extract_mod_info.go @@ -11,6 +11,7 @@ import ( "go.temporal.io/sdk/temporal" "github.com/satisfactorymodding/smr-api/db" + "github.com/satisfactorymodding/smr-api/db/schema" version2 "github.com/satisfactorymodding/smr-api/generated/ent/version" "github.com/satisfactorymodding/smr-api/util" "github.com/satisfactorymodding/smr-api/validation" @@ -62,6 +63,29 @@ func (*A) ExtractModInfoActivity(ctx context.Context, args ExtractModInfoArgs) ( return nil, temporal.NewNonRetryableApplicationError("this mod already has a version with this name", "fatal", nil) } + if modInfo.Semver != nil { + major := int(modInfo.Semver.Major()) + minor := int(modInfo.Semver.Minor()) + patch := int(modInfo.Semver.Patch()) + + semverCount, err := db.From(schema.SkipSoftDelete(ctx)).Version.Query(). + Where( + version2.ModID(mod.ID), + version2.VersionMajor(major), + version2.VersionMinor(minor), + version2.VersionPatch(patch), + version2.DeletedAtNotNil(), + ). + Count(ctx) + if err != nil { + return nil, temporal.NewNonRetryableApplicationError("database error", "fatal", err) + } + + if semverCount > 0 { + return nil, temporal.NewNonRetryableApplicationError("this mod already has a version with this semver", "fatal", nil) + } + } + // Allow only new 5 versions per 24h versions, err := db.From(ctx).Version.Query(). Order(version2.ByCreatedAt(sql.OrderAsc())).