bbende commented on code in PR #8765:
URL: https://github.com/apache/nifi/pull/8765#discussion_r1594087217


##########
nifi-extension-bundles/nifi-github-bundle/nifi-github-extensions/src/main/java/org/apache/nifi/github/GitHubRepositoryClient.java:
##########
@@ -0,0 +1,404 @@
+/*
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.nifi.github;
+
+import org.apache.nifi.registry.flow.FlowRegistryException;
+import org.kohsuke.github.GHCommit;
+import org.kohsuke.github.GHContent;
+import org.kohsuke.github.GHContentUpdateResponse;
+import org.kohsuke.github.GHRef;
+import org.kohsuke.github.GHRepository;
+import org.kohsuke.github.GitHub;
+import org.kohsuke.github.GitHubBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Client to encapsulate access to a GitHub Repository through the Hub4j 
GitHub client.
+ */
+public class GitHubRepositoryClient {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(GitHubRepositoryClient.class);
+
+    private static final String BRANCH_REF_PATTERN = "refs/heads/%s";
+    private static final int COMMIT_PAGE_SIZE = 50;
+
+    private final GHRepository repository;
+    private final String repoPath;
+    private final GitHubAuthenticationType authenticationType;
+
+    private GitHubRepositoryClient(final Builder builder) throws IOException {
+        final GitHubBuilder gitHubBuilder = new 
GitHubBuilder().withEndpoint(builder.apiUrl);
+
+        authenticationType = builder.authenticationType;
+        switch (authenticationType) {
+            case PERSONAL_ACCESS_TOKEN -> 
gitHubBuilder.withOAuthToken(builder.personalAccessToken);
+            case APP_INSTALLATION_TOKEN -> 
gitHubBuilder.withAppInstallationToken(builder.appInstallationToken);
+        }
+
+        final GitHub gitHub = gitHubBuilder.build();
+        try {
+            repository = gitHub.getRepository(builder.repoOwner + "/" + 
builder.repoName);
+        } catch (final IOException e) {
+            LOGGER.error("Unable to access GitHub repository [{}]", 
builder.repoName, e);
+            throw e;
+        }
+        repoPath = builder.repoPath;
+    }
+
+    /**
+     * @return the authentication type this client is configured with
+     */
+    public GitHubAuthenticationType getAuthenticationType() {
+        return authenticationType;
+    }
+
+    /**
+     * Creates the content specified by the given builder.
+     *
+     * @param request the request for the content to create
+     * @return the update response
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    public GHContentUpdateResponse createContent(final 
GitHubCreateContentRequest request) throws IOException, FlowRegistryException {
+        final String branch = request.getBranch();
+        final String resolvedPath = getResolvedPath(request.getPath());
+        LOGGER.debug("Creating content at path [{}] on branch [{}] in repo 
[{}] ", resolvedPath, branch, repository.getName());
+        return execute(() -> {
+            try {
+                return repository.createContent()
+                        .branch(branch)
+                        .path(resolvedPath)
+                        .content(request.getContent())
+                        .message(request.getMessage())
+                        .sha(request.getExistingContentSha())
+                        .commit();
+            } catch (final FileNotFoundException fnf) {
+                throwPathOrBranchNotFound(resolvedPath, branch);
+                return null;
+            }
+        });
+    }
+
+    /**
+     * Gets the names of all the branches in the repo.
+     *
+     * @return the set of all branches in the repo
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    public Set<String> getBranches() throws IOException, FlowRegistryException 
{
+        LOGGER.debug("Getting branches for repo [{}]", repository.getName());
+        return execute(() -> repository.getBranches().keySet());
+    }
+
+    /**
+     * Gets an InputStream to read the latest content of the given path from 
the given branch.
+     * The returned stream already contains the contents of the requested file.
+     *
+     * @param path the path to the content
+     * @param branch the branch
+     * @return an input stream containing the contents of the path
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    public InputStream getContentFromBranch(final String path, final String 
branch) throws IOException, FlowRegistryException {
+        final String resolvedPath = getResolvedPath(path);
+        final String branchRef = BRANCH_REF_PATTERN.formatted(branch);
+        LOGGER.debug("Getting content for [{}] from branch [{}] in repo [{}] 
", resolvedPath, branch, repository.getName());
+
+        return execute(() -> {
+            try {
+                final GHContent ghContent = 
repository.getFileContent(resolvedPath, branchRef);
+                return ghContent.read();
+            } catch (final FileNotFoundException e) {
+                throwPathOrBranchNotFound(resolvedPath, branchRef);
+                return null;
+            }
+        });
+    }
+
+    /**
+     * Gets the content of the given path from the given commit.
+     * The returned stream already contains the contents of the requested file.
+     *
+     * @param path the path to the content
+     * @param commitSha the commit SHA
+     * @return an input stream containing the contents of the path
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    public InputStream getContentFromCommit(final String path, final String 
commitSha) throws IOException, FlowRegistryException {
+        final String resolvedPath = getResolvedPath(path);
+        LOGGER.debug("Getting content for [{}] from commit [{}] in repo [{}] 
", resolvedPath, commitSha, repository.getName());
+
+        return execute(() -> {
+            try {
+                final GHContent ghContent = 
repository.getFileContent(resolvedPath, commitSha);
+                return ghContent.read();
+            } catch (final FileNotFoundException fnf) {
+                throw new FlowRegistryException("Path [" + resolvedPath + "] 
or Commit [" + commitSha + "] not found");
+            }
+        });
+    }
+
+    /**
+     * Gets the commits for a given path on a given branch.
+     *
+     * @param path the path
+     * @param branch the branch
+     * @return the list of commits for the given path
+     *
+     * @throws IOException if an I/O error happens calling GitHub
+     * @throws FlowRegistryException if a non I/O error happens calling GitHub
+     */
+    public List<GHCommit> getCommits(final String path, final String branch) 
throws IOException, FlowRegistryException {
+        final String resolvedPath = getResolvedPath(path);
+        final String branchRef = BRANCH_REF_PATTERN.formatted(branch);
+        LOGGER.debug("Getting commits for [{}] from branch [{}] in repo [{}]", 
resolvedPath, branch, repository.getName());
+
+        return execute(() -> {
+            try {
+                final GHRef branchGhRef = repository.getRef(branchRef);
+                return repository.queryCommits()
+                        .path(resolvedPath)
+                        .from(branchGhRef.getObject().getSha())
+                        .pageSize(COMMIT_PAGE_SIZE)

Review Comment:
   Yes the comments on `toList()` say:
   ```
   Eagerly walk {@link Iterable} and return the result in a list.
   ```
   So it will page through the results using page size and return all of them 
in a list.



-- 
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