This is an automated email from the ASF dual-hosted git repository.
exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 5dee3239241 NIFI-16526 GitLab Flow Registry Client should use the
file's last commit to determine the latest version (#11869)
5dee3239241 is described below
commit 5dee3239241811d48b40d522794a3a42f16cc003
Author: Pierre Villard <[email protected]>
AuthorDate: Mon Nov 2 22:27:51 2026 +0100
NIFI-16526 GitLab Flow Registry Client should use the file's last commit to
determine the latest version (#11869)
Signed-off-by: David Handermann <[email protected]>
---
.../registry/flow/git/AbstractGitFlowRegistryClient.java | 7 ++-----
.../nifi/registry/flow/git/client/GitRepositoryClient.java | 13 +++++++++++++
.../apache/nifi/github/GitHubFlowRegistryClientTest.java | 9 ++-------
.../java/org/apache/nifi/gitlab/GitLabRepositoryClient.java | 7 +++++++
4 files changed, 24 insertions(+), 12 deletions(-)
diff --git
a/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/AbstractGitFlowRegistryClient.java
b/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/AbstractGitFlowRegistryClient.java
index 54089ab53d7..2ed97ef832f 100644
---
a/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/AbstractGitFlowRegistryClient.java
+++
b/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/AbstractGitFlowRegistryClient.java
@@ -413,8 +413,7 @@ public abstract class AbstractGitFlowRegistryClient extends
AbstractFlowRegistry
final String expectedVersion = snapshotMetadata.getVersion();
// Get the current version (latest commit SHA) from the repository
- final List<GitCommit> commits = repositoryClient.getCommits(filePath,
branch);
- final String currentVersion = commits.isEmpty() ? null :
commits.getFirst().id();
+ final String currentVersion =
repositoryClient.getLatestCommitSha(filePath, branch).orElse(null);
// Check for version conflict: if the user expects a specific version
but it doesn't match the current version in the repository,
// another user may have committed changes in the meantime. Reject the
commit unless FORCE_COMMIT is specified.
@@ -562,9 +561,7 @@ public abstract class AbstractGitFlowRegistryClient extends
AbstractFlowRegistry
final String branch = flowLocation.getBranch();
final String filePath = getSnapshotFilePath(flowLocation);
- final List<GitCommit> commits = repositoryClient.getCommits(filePath,
branch);
- final String latestVersion = commits.isEmpty() ? null :
commits.getFirst().id();
- return Optional.ofNullable(latestVersion);
+ return repositoryClient.getLatestCommitSha(filePath, branch);
}
@Override
diff --git
a/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/client/GitRepositoryClient.java
b/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/client/GitRepositoryClient.java
index 65abaf9055f..b7aa4ad8459 100644
---
a/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/client/GitRepositoryClient.java
+++
b/nifi-extension-bundles/nifi-extension-utils/nifi-git-flow-registry/src/main/java/org/apache/nifi/registry/flow/git/client/GitRepositoryClient.java
@@ -81,6 +81,19 @@ public interface GitRepositoryClient {
*/
List<GitCommit> getCommits(String path, String branch) throws IOException,
FlowRegistryException;
+ /**
+ * Retrieves the SHA of the most recent commit that changed the file at
the given path on the given branch.
+ *
+ * @param path the path of the file
+ * @param branch the branch name
+ * @return the SHA of the most recent commit, or empty if the file has no
commits
+ * @throws IOException if an I/O error occurs
+ * @throws FlowRegistryException if a non-I/O error occurs
+ */
+ default Optional<String> getLatestCommitSha(String path, String branch)
throws IOException, FlowRegistryException {
+ return getCommits(path,
branch).stream().findFirst().map(GitCommit::id);
+ }
+
/**
* Retrieves the content for the file at the given path on the given
branch.
*
diff --git
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubFlowRegistryClientTest.java
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubFlowRegistryClientTest.java
index dffcc8069b4..03a8a241bac 100644
---
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubFlowRegistryClientTest.java
+++
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubFlowRegistryClientTest.java
@@ -30,7 +30,6 @@ import org.apache.nifi.registry.flow.RegisterAction;
import org.apache.nifi.registry.flow.RegisteredFlow;
import org.apache.nifi.registry.flow.RegisteredFlowSnapshot;
import org.apache.nifi.registry.flow.RegisteredFlowSnapshotMetadata;
-import org.apache.nifi.registry.flow.git.client.GitCommit;
import org.apache.nifi.registry.flow.git.client.GitCreateContentRequest;
import org.apache.nifi.registry.flow.git.serialize.FlowSnapshotSerializer;
import org.junit.jupiter.api.BeforeEach;
@@ -41,9 +40,7 @@ import org.mockito.ArgumentCaptor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.time.Instant;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -230,8 +227,7 @@ public class GitHubFlowRegistryClientTest {
incomingSnapshot.setFlowContents(new VersionedProcessGroup());
// Mock repository to return a different (newer) commit SHA than what
the user expects
- final GitCommit newerCommit = new GitCommit("new-commit-sha-67890",
"author", "message", Instant.now());
-
doReturn(List.of(newerCommit)).when(repositoryClient).getCommits(any(), any());
+
doReturn(Optional.of("new-commit-sha-67890")).when(repositoryClient).getLatestCommitSha(any(),
any());
// Attempting to commit should throw a FlowRegistryException due to
version conflict
final FlowRegistryException exception =
assertThrows(FlowRegistryException.class, () ->
@@ -264,8 +260,7 @@ public class GitHubFlowRegistryClientTest {
incomingSnapshot.setFlowContents(new VersionedProcessGroup());
// Mock repository to return a different (newer) commit SHA than what
the user expects
- final GitCommit newerCommit = new GitCommit("new-commit-sha-67890",
"author", "message", Instant.now());
-
doReturn(List.of(newerCommit)).when(repositoryClient).getCommits(any(), any());
+
doReturn(Optional.of("new-commit-sha-67890")).when(repositoryClient).getLatestCommitSha(any(),
any());
// Mock blob SHA at the expected commit for atomic operation
doReturn(Optional.of("blob-sha-at-expected-commit")).when(repositoryClient).getContentShaAtCommit(any(),
any());
diff --git
a/nifi-extension-bundles/nifi-gitlab-bundle/nifi-gitlab-extensions/src/main/java/org/apache/nifi/gitlab/GitLabRepositoryClient.java
b/nifi-extension-bundles/nifi-gitlab-bundle/nifi-gitlab-extensions/src/main/java/org/apache/nifi/gitlab/GitLabRepositoryClient.java
index f54275af97d..67928d1474d 100644
---
a/nifi-extension-bundles/nifi-gitlab-bundle/nifi-gitlab-extensions/src/main/java/org/apache/nifi/gitlab/GitLabRepositoryClient.java
+++
b/nifi-extension-bundles/nifi-gitlab-bundle/nifi-gitlab-extensions/src/main/java/org/apache/nifi/gitlab/GitLabRepositoryClient.java
@@ -219,6 +219,13 @@ public class GitLabRepositoryClient implements
GitRepositoryClient {
);
}
+ @Override
+ public Optional<String> getLatestCommitSha(final String path, final String
branch) throws FlowRegistryException {
+ final String resolvedPath = getResolvedPath(path);
+ logger.debug("Getting latest commit for path [{}] on branch [{}] in
repository [{}]", resolvedPath, branch, projectPath);
+ return execute(() ->
gitLab.getRepositoryFileApi().getOptionalFileInfo(projectPath, resolvedPath,
branch).map(RepositoryFile::getLastCommitId));
+ }
+
@Override
public InputStream getContentFromBranch(final String path, final String
branch) throws FlowRegistryException {
final String resolvedPath = getResolvedPath(path);