From 3460b8859a3678c20f2e60a445e2dac13dde845a Mon Sep 17 00:00:00 2001 From: Hoan Nguyen <83745591+nqhoan-axonivy@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:30:12 +0700 Subject: [PATCH] Feature/marp 1451 include codeowners in GitHub for each connector (#3) * Add codeowner checking * Handle feedbacks --- .../github/file/CodeOwnerFilesDetector.java | 11 +++++- .../file/GitHubMissingFilesDetector.java | 37 ++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/github-repo-manager/src/main/java/com/axonivy/github/file/CodeOwnerFilesDetector.java b/github-repo-manager/src/main/java/com/axonivy/github/file/CodeOwnerFilesDetector.java index 974d346..0c37fc1 100644 --- a/github-repo-manager/src/main/java/com/axonivy/github/file/CodeOwnerFilesDetector.java +++ b/github-repo-manager/src/main/java/com/axonivy/github/file/CodeOwnerFilesDetector.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.kohsuke.github.GHContent; import java.io.IOException; import java.util.List; @@ -20,6 +21,14 @@ public CodeOwnerFilesDetector(GitHubFiles.FileMeta fileMeta, String user) throws super(fileMeta, user); } + @Override + protected boolean hasSimilarContent(GHContent existingFile) throws IOException { + // The code owners has a lot of rulesets, and we should not override the existing config + try (var inputStream = existingFile.read()) { + return StringUtils.isNoneBlank(new String(inputStream.readAllBytes())); + } + } + @Override protected byte[] loadReferenceFileContent(String repoURL) throws IOException { if (StringUtils.isBlank(repoURL)) { @@ -31,7 +40,7 @@ protected byte[] loadReferenceFileContent(String repoURL) throws IOException { return String.format(CODE_OWNER_FORMAT, codeOwner.owner).getBytes(); } } - return new byte[0]; + return null; } private List getAllCodeOwners() throws IOException { diff --git a/github-repo-manager/src/main/java/com/axonivy/github/file/GitHubMissingFilesDetector.java b/github-repo-manager/src/main/java/com/axonivy/github/file/GitHubMissingFilesDetector.java index 192aae2..762925d 100644 --- a/github-repo-manager/src/main/java/com/axonivy/github/file/GitHubMissingFilesDetector.java +++ b/github-repo-manager/src/main/java/com/axonivy/github/file/GitHubMissingFilesDetector.java @@ -1,17 +1,16 @@ package com.axonivy.github.file; -import java.io.IOException; -import java.io.Reader; -import java.util.List; -import java.util.Objects; - +import com.axonivy.github.DryRun; +import com.axonivy.github.GitHubProvider; +import com.axonivy.github.file.GitHubFiles.FileMeta; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.CharSequenceReader; import org.kohsuke.github.*; -import com.axonivy.github.DryRun; -import com.axonivy.github.GitHubProvider; -import com.axonivy.github.file.GitHubFiles.FileMeta; +import java.io.IOException; +import java.io.Reader; +import java.util.List; +import java.util.Objects; public class GitHubMissingFilesDetector { @@ -81,8 +80,12 @@ private GHContent getFileContent(String path, GHRepository repo) { } } - private boolean hasSimilarContent(GHContent existingFile) throws IOException { - Reader targetContent = new CharSequenceReader(new String(loadReferenceFileContent(existingFile.getGitUrl()))); + protected boolean hasSimilarContent(GHContent existingFile) throws IOException { + var fileContent = loadReferenceFileContent(existingFile.getGitUrl()); + if (fileContent == null) { + return true; + } + Reader targetContent = new CharSequenceReader(new String(fileContent)); Reader actualContent; try (var inputStream = existingFile.read()) { actualContent = new CharSequenceReader(new String(inputStream.readAllBytes())); @@ -106,13 +109,17 @@ private void handleMissingFile(GHRepository repo) throws IOException { } private void addMissingFile(GHRepository repo) throws IOException { + var fileContent = loadReferenceFileContent(repo.getUrl().toString()); + if (fileContent == null) { + return; + } var defaultBranch = repo.getBranch(repo.getDefaultBranch()); String refURL = createBranchIfMissing(repo, BRANCH_PREFIX + reference.meta().branchName(), defaultBranch.getSHA1()); try { repo.createContent() .branch(refURL) .path(reference.meta().filePath()) - .content(loadReferenceFileContent(repo.getUrl().toString())) + .content(fileContent) .message(reference.meta().commitMessage()) .commit(); } catch (GHFileNotFoundException notFoundException) { @@ -134,7 +141,7 @@ private void createNewPullRequest(GHRepository repo, String refURL) throws IOExc } } - private String createBranchIfMissing(GHRepository repo, String branchName, String sha) throws IOException { + private String createBranchIfMissing(GHRepository repo, String branchName, String sha) { String createdBranch = branchName; var isBranchExisted = false; try { @@ -169,7 +176,7 @@ private void handleOtherContent(GHRepository repo) throws IOException { isNotSync = true; LOG.info("DRYRUN: "); LOG.info("Repo {0} has {1} but the content is different from required file {2}.", - repo.getFullName(), reference.meta().filePath(), reference.meta().filePath()); + repo.getFullName(), reference.meta().filePath(), reference.meta().filePath()); } else { updateFile(repo); LOG.info("Repo {0} {1} synced.", repo.getFullName(), reference.meta().filePath()); @@ -181,6 +188,10 @@ private void handleOtherContent(GHRepository repo) throws IOException { } private void updateFile(GHRepository repo) throws IOException { + var fileContent = loadReferenceFileContent(repo.getUrl().toString()); + if (fileContent == null) { + return; + } var headBranch = repo.getBranch(repo.getDefaultBranch()); String refURL = createBranchIfMissing(repo, BRANCH_PREFIX + reference.meta().branchName(), headBranch.getSHA1()); repo.getFileContent(reference.meta().filePath(), refURL)