Skip to content

Commit

Permalink
Feature/marp 1451 include codeowners in GitHub for each connector (#3)
Browse files Browse the repository at this point in the history
* Add codeowner checking

* Handle feedbacks
  • Loading branch information
nqhoan-axonivy authored Dec 27, 2024
1 parent f6a45d2 commit 3460b88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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<CodeOwner> getAllCodeOwners() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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()));
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand All @@ -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)
Expand Down

0 comments on commit 3460b88

Please sign in to comment.