@@ -647,8 +647,12 @@ private ArtifactVersionMetaDataDto createArtifactVersionRaw(Handle handle, boole
647
647
}
648
648
649
649
// Update system generated branches
650
- createOrUpdateBranchRaw (handle , gav , BranchId .LATEST , true );
651
- createOrUpdateSemverBranchesRaw (handle , gav );
650
+ if (isDraft ) {
651
+ createOrUpdateBranchRaw (handle , gav , BranchId .DRAFTS , true );
652
+ } else {
653
+ createOrUpdateBranchRaw (handle , gav , BranchId .LATEST , true );
654
+ createOrUpdateSemverBranchesRaw (handle , gav );
655
+ }
652
656
653
657
// Create any user defined branches
654
658
if (branches != null && !branches .isEmpty ()) {
@@ -2046,20 +2050,29 @@ public void updateArtifactVersionState(String groupId, String artifactId, String
2046
2050
handle .setRollback (true );
2047
2051
}
2048
2052
2049
- int rowCount = handle .createUpdate (sqlStatements .updateArtifactVersionStateByGAV ())
2050
- .bind (0 , newState .name ()).bind (1 , normalizeGroupId (groupId )).bind (2 , artifactId )
2051
- .bind (3 , version ).execute ();
2052
- if (rowCount == 0 ) {
2053
- throw new VersionNotFoundException (groupId , artifactId , version );
2054
- }
2053
+ Optional <VersionState > res = handle
2054
+ .createQuery (sqlStatements .selectArtifactVersionStateForUpdate ())
2055
+ .bind (0 , normalizeGroupId (groupId )).bind (1 , artifactId ).bind (2 , version )
2056
+ .map (VersionStateMapper .instance ).findOne ();
2057
+ VersionState currentState = res
2058
+ .orElseThrow (() -> new VersionNotFoundException (groupId , artifactId , version ));
2059
+
2060
+ handle .createUpdate (sqlStatements .updateArtifactVersionStateByGAV ()).bind (0 , newState .name ())
2061
+ .bind (1 , normalizeGroupId (groupId )).bind (2 , artifactId ).bind (3 , version ).execute ();
2062
+
2055
2063
String modifiedBy = securityIdentity .getPrincipal ().getName ();
2056
2064
Date modifiedOn = new Date ();
2065
+ handle .createUpdate (sqlStatements .updateArtifactVersionModifiedByOn ()).bind (0 , modifiedBy )
2066
+ .bind (1 , modifiedOn ).bind (2 , normalizeGroupId (groupId )).bind (3 , artifactId )
2067
+ .bind (4 , version ).execute ();
2057
2068
2058
- rowCount = handle .createUpdate (sqlStatements .updateArtifactVersionModifiedByOn ())
2059
- .bind (0 , modifiedBy ).bind (1 , modifiedOn ).bind (2 , normalizeGroupId (groupId ))
2060
- .bind (3 , artifactId ).bind (4 , version ).execute ();
2061
- if (rowCount == 0 ) {
2062
- throw new VersionNotFoundException (groupId , artifactId , version );
2069
+ // If transitioning from DRAFT state to something else, then we need to maintain
2070
+ // the system branches.
2071
+ if (currentState == VersionState .DRAFT ) {
2072
+ GAV gav = new GAV (groupId , artifactId , version );
2073
+ createOrUpdateBranchRaw (handle , gav , BranchId .LATEST , true );
2074
+ createOrUpdateSemverBranchesRaw (handle , gav );
2075
+ removeVersionFromBranchRaw (handle , gav , BranchId .DRAFTS );
2063
2076
}
2064
2077
2065
2078
return null ;
@@ -3633,6 +3646,19 @@ private void createOrUpdateBranchRaw(Handle handle, GAV gav, BranchId branchId,
3633
3646
appendVersionToBranchRaw (handle , gav , branchId , gav .getVersionId ());
3634
3647
}
3635
3648
3649
+ /**
3650
+ * Removes a version from the given branch.
3651
+ *
3652
+ * @param handle
3653
+ * @param gav
3654
+ * @param branchId
3655
+ */
3656
+ private void removeVersionFromBranchRaw (Handle handle , GAV gav , BranchId branchId ) {
3657
+ handle .createUpdate (sqlStatements .deleteVersionFromBranch ()).bind (0 , gav .getRawGroupIdWithNull ())
3658
+ .bind (1 , gav .getRawArtifactId ()).bind (2 , branchId .getRawBranchId ())
3659
+ .bind (3 , gav .getRawVersionId ()).execute ();
3660
+ }
3661
+
3636
3662
private void updateBranchModifiedTimeRaw (Handle handle , GA ga , BranchId branchId ) {
3637
3663
String user = securityIdentity .getPrincipal ().getName ();
3638
3664
Date now = new Date ();
0 commit comments