Rarasinhoo commented on code in PR #11925:
URL: https://github.com/apache/nifi/pull/11925#discussion_r4593376272


##########
nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java:
##########
@@ -337,6 +338,53 @@ public List<GitCommit> getCommits(final String path, final 
String branch) throws
         });
     }
 
+    /**
+     * 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 branch name is passed as the starting point of the listing, so 
no separate request resolves the branch reference
+        final PagedIterator<GHCommit> commits = repository.queryCommits()
+                .path(resolvedPath)
+                .from(branch)

Review Comment:
   Good catch, thanks. Changed in 4b3ef044: the query now starts from 
`refs/heads/<branch>`, still one request. Checked against GitHub: an existing 
branch returns its latest commit for the path, an unknown branch and a commit 
SHA passed as the branch name both return Not Found, which maps to the existing 
"Path or Branch not found" error. The unit test now verifies the full reference.



##########
nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/test/java/org/apache/nifi/github/GitHubRepositoryClientTest.java:
##########
@@ -84,4 +96,62 @@ void 
testResolveCommitAuthorReturnsPlaceholderWhenNoAuthorAvailable() throws IOE
         final String author = 
GitHubRepositoryClient.resolveCommitAuthor(ghCommit, shortInfo);
         assertEquals("unknown", author);
     }
+
+    private static final String PATH = "bucket/flow.json";

Review Comment:
   Done in 4b3ef044, the constants are now at the top of the class.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to