Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/marp 1451 include codeowners in GitHub for each connector #3

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.input.CharSequenceReader;
tvtphuc-axonivy marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand All @@ -20,6 +22,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)) {
Expand All @@ -31,7 +41,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<CodeOwner> getAllCodeOwners() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,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()));
Expand All @@ -106,13 +110,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) {
Expand Down Expand Up @@ -181,6 +189,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)
Expand Down