This is an automated email from the ASF dual-hosted git repository.

pvillard31 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 2d6a0e888d5 NIFI-16588 Get the latest commit of a GitHub flow file 
with one request (#11925)
2d6a0e888d5 is described below

commit 2d6a0e888d5df3ec13e2ec5f43c3e3e4816fcb32
Author: Raul Roberto da Silva <[email protected]>
AuthorDate: Tue Nov 17 08:00:52 2026 -0300

    NIFI-16588 Get the latest commit of a GitHub flow file with one request 
(#11925)
---
 .../apache/nifi/github/GitHubRepositoryClient.java | 49 +++++++++++++++
 .../nifi/github/GitHubRepositoryClientTest.java    | 71 ++++++++++++++++++++++
 2 files changed, 120 insertions(+)

diff --git 
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java
 
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java
index 1360428ed85..8302691947d 100644
--- 
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java
+++ 
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java
@@ -32,6 +32,7 @@ import org.kohsuke.github.GHCommit;
 import org.kohsuke.github.GHContent;
 import org.kohsuke.github.GHContentBuilder;
 import org.kohsuke.github.GHContentUpdateResponse;
+import org.kohsuke.github.GHException;
 import org.kohsuke.github.GHMyself;
 import org.kohsuke.github.GHPermissionType;
 import org.kohsuke.github.GHRef;
@@ -337,6 +338,54 @@ public class GitHubRepositoryClient implements 
GitRepositoryClient {
         });
     }
 
+    /**
+     * Gets the SHA of the most recent commit that changed the given path on 
the given branch with a single request.
+     * The default implementation lists a page of commits and resolves each 
commit author, which costs an
+     * additional request per commit not already cached, while this value is 
requested for every versioned
+     * Process Group on every synchronization with the registry.
+     *
+     * @param path the path
+     * @param branch the branch
+     * @return the SHA of the latest commit for the path, or empty when no 
commit changed the path
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    @Override
+    public Optional<String> getLatestCommitSha(final String path, final String 
branch) throws IOException, FlowRegistryException {
+        final String resolvedPath = getResolvedPath(path);
+        logger.debug("Getting latest commit for [{}] from branch [{}] in repo 
[{}]", resolvedPath, branch, repository.getName());
+
+        return execute(() -> {
+            try {
+                return getLatestCommitSha(repository, resolvedPath, branch);
+            } catch (final FileNotFoundException fnf) {
+                throwPathOrBranchNotFound(fnf, resolvedPath, 
BRANCH_REF_PATTERN.formatted(branch));
+                return null;
+            }
+        });
+    }
+
+    static Optional<String> getLatestCommitSha(final GHRepository repository, 
final String resolvedPath, final String branch) throws IOException {
+        // The full branch reference is the starting point of the listing: no 
separate request resolves it, and GitHub does
+        // not fall back to a tag or commit with the same name, so an unknown 
branch still fails with Not Found
+        final PagedIterator<GHCommit> commits = repository.queryCommits()
+                .path(resolvedPath)
+                .from(BRANCH_REF_PATTERN.formatted(branch))
+                .pageSize(1)
+                .list()
+                .iterator();
+        try {
+            return commits.hasNext() ? Optional.of(commits.next().getSHA1()) : 
Optional.empty();
+        } catch (final GHException e) {
+            // Iterators wrap request failures, such as Not Found for an 
unknown branch, which callers handle as IOException
+            if (e.getCause() instanceof IOException ioException) {
+                throw ioException;
+            }
+            throw e;
+        }
+    }
+
     /**
      * Gets the top-level directory names, which are the directories at the 
root of the repo, or within the prefix if specified.
      *
diff --git 
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubRepositoryClientTest.java
 
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubRepositoryClientTest.java
index e603df2e063..48c532a6896 100644
--- 
a/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubRepositoryClientTest.java
+++ 
b/nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubRepositoryClientTest.java
@@ -19,18 +19,35 @@ package org.apache.nifi.github;
 
 import org.junit.jupiter.api.Test;
 import org.kohsuke.github.GHCommit;
+import org.kohsuke.github.GHCommitQueryBuilder;
+import org.kohsuke.github.GHException;
 import org.kohsuke.github.GHFileNotFoundException;
+import org.kohsuke.github.GHRepository;
 import org.kohsuke.github.GHUser;
 import org.kohsuke.github.GitUser;
+import org.kohsuke.github.PagedIterable;
+import org.kohsuke.github.PagedIterator;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.Optional;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.RETURNS_SELF;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 class GitHubRepositoryClientTest {
 
+    private static final String PATH = "bucket/flow.json";
+    private static final String BRANCH = "main";
+    private static final String BRANCH_REF = "refs/heads/main";
+    private static final String COMMIT_SHA = "0123456789abcdef";
+
     @Test
     void testResolveCommitAuthorReturnsLoginWhenAccountResolvable() throws 
IOException {
         final GHUser ghUser = mock(GHUser.class);
@@ -84,4 +101,58 @@ class GitHubRepositoryClientTest {
         final String author = 
GitHubRepositoryClient.resolveCommitAuthor(ghCommit, shortInfo);
         assertEquals("unknown", author);
     }
+
+    @Test
+    void testGetLatestCommitShaQueriesOneCommitFromBranchWithoutAuthorLookup() 
throws IOException {
+        final GHCommit ghCommit = mock(GHCommit.class);
+        when(ghCommit.getSHA1()).thenReturn(COMMIT_SHA);
+        final PagedIterator<GHCommit> iterator = mockIterator();
+        when(iterator.hasNext()).thenReturn(true);
+        when(iterator.next()).thenReturn(ghCommit);
+        final GHRepository repository = mockRepository(iterator);
+
+        assertEquals(Optional.of(COMMIT_SHA), 
GitHubRepositoryClient.getLatestCommitSha(repository, PATH, BRANCH));
+
+        final GHCommitQueryBuilder queryBuilder = repository.queryCommits();
+        verify(queryBuilder).path(PATH);
+        verify(queryBuilder).from(BRANCH_REF);
+        verify(queryBuilder).pageSize(1);
+        verify(ghCommit).getSHA1();
+        verify(repository, never()).getRef(anyString());
+    }
+
+    @Test
+    void testGetLatestCommitShaEmptyWhenPathHasNoCommits() throws IOException {
+        final PagedIterator<GHCommit> iterator = mockIterator();
+        when(iterator.hasNext()).thenReturn(false);
+
+        assertEquals(Optional.empty(), 
GitHubRepositoryClient.getLatestCommitSha(mockRepository(iterator), PATH, 
BRANCH));
+    }
+
+    @Test
+    void testGetLatestCommitShaUnwrapsNotFound() {
+        final PagedIterator<GHCommit> iterator = mockIterator();
+        final FileNotFoundException notFound = new FileNotFoundException("Not 
Found");
+        when(iterator.hasNext()).thenThrow(new GHException("Failed to 
retrieve", notFound));
+
+        final FileNotFoundException thrown = 
assertThrows(FileNotFoundException.class,
+                () -> 
GitHubRepositoryClient.getLatestCommitSha(mockRepository(iterator), PATH, 
BRANCH));
+        assertEquals(notFound, thrown);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static PagedIterator<GHCommit> mockIterator() {
+        return mock(PagedIterator.class);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static GHRepository mockRepository(final PagedIterator<GHCommit> 
iterator) {
+        final PagedIterable<GHCommit> iterable = mock(PagedIterable.class);
+        when(iterable.iterator()).thenReturn(iterator);
+        final GHCommitQueryBuilder queryBuilder = 
mock(GHCommitQueryBuilder.class, RETURNS_SELF);
+        when(queryBuilder.list()).thenReturn(iterable);
+        final GHRepository repository = mock(GHRepository.class);
+        when(repository.queryCommits()).thenReturn(queryBuilder);
+        return repository;
+    }
 }

Reply via email to