Skip to content

Commit

Permalink
MARP-1451 include codeowners in GitHub for each connector (#2)
Browse files Browse the repository at this point in the history
* Added Codeowner detecter
* Define codeowner for market
* Add gitignore
* Remove unuse file
* Format codes
* Enhance log file
* Handle feedback
* Handle permission
  • Loading branch information
nqhoan-axonivy authored Dec 26, 2024
1 parent 96c474b commit f6a45d2
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 99 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/missing-file-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,19 @@ jobs:
with:
java-version: 17
distribution: temurin

- name: Setup Maven
uses: stCarolas/setup-maven@v5
cache: maven

- name: Set default values for dryRun and workingOrgs
id: set-defaults
run: |
echo "dryRun=${{ github.event.inputs.dryRun || 'true' }}" >> $GITHUB_ENV
echo "workingOrgs=${{ github.event.inputs.workingOrgs || 'axonivy-market' }}" >> $GITHUB_ENV
echo "workingOrgs=${{ github.event.inputs.workingOrgs || 'axonivy-market' }}" >> $GITHUB_ENV
- name: Build with Maven
working-directory: ./github-repo-manager
run: |
mvn -B clean compile exec:java \
-DDRY_RUN="${{ env.dryRun }}" \
-DGITHUB.TOKEN.FILE="${{ secrets.TOKEN }}" \
-DGITHUB.TOKEN="${{ secrets.TOKEN }}" \
-Dexec.mainClass="com.axonivy.github.file.GitHubMissingFiles" \
-Dexec.args="${{ github.actor }}" \
-DGITHUB.WORKING.ORGANIZATIONS="${{ env.workingOrgs }}"
continue-on-error: true
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
./idea/*
*/idea/*
37 changes: 0 additions & 37 deletions build/public-repo-missing-files/Jenkinsfile

This file was deleted.

4 changes: 3 additions & 1 deletion github-repo-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<groupId>com.axonivy.github</groupId>
<artifactId>github-repo-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static GitHub get() {
}

public static GitHub getGithubToken() {
String token = System.getProperty("GITHUB.TOKEN.FILE");
String token = System.getProperty("GITHUB.TOKEN");
try {
return new GitHubBuilder().withOAuthToken(token).build();
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.axonivy.github.file;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;

public class CodeOwnerFilesDetector extends GitHubMissingFilesDetector {
private static final String CODE_OWNER_FILE_NAME = "CodeOwners.json";
private static final TypeReference<List<CodeOwner>> CODE_OWNER_TYPE_REFERENCE = new TypeReference<>() {
};
private static final String CODE_OWNER_FORMAT = "* %s";
private static final ObjectMapper objectMapper = new ObjectMapper();
private List<CodeOwner> codeOwners;

public CodeOwnerFilesDetector(GitHubFiles.FileMeta fileMeta, String user) throws IOException {
super(fileMeta, user);
}

@Override
protected byte[] loadReferenceFileContent(String repoURL) throws IOException {
if (StringUtils.isBlank(repoURL)) {
return super.loadReferenceFileContent(repoURL);
}

for (var codeOwner : getAllCodeOwners()) {
if (StringUtils.contains(repoURL, codeOwner.product)) {
return String.format(CODE_OWNER_FORMAT, codeOwner.owner).getBytes();
}
}
return new byte[0];
}

private List<CodeOwner> getAllCodeOwners() throws IOException {
if (ObjectUtils.isEmpty(codeOwners)) {
try (var is = CodeOwnerFilesDetector.class.getResourceAsStream(CODE_OWNER_FILE_NAME)) {
codeOwners = objectMapper.readValue(is, CODE_OWNER_TYPE_REFERENCE);
}
}
return codeOwners;
}

record CodeOwner(String product, String owner) {
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.axonivy.github.file;

import java.io.IOException;

import org.apache.commons.io.IOUtils;

import com.axonivy.github.file.GitHubFiles.FileMeta;
import org.apache.commons.io.IOUtils;

import java.io.IOException;

public record FileReference(FileMeta meta, byte[] content) {
public class FileReference {
FileMeta meta;

public FileReference(FileMeta meta) throws IOException {
this(meta, load(meta));
this.meta = meta;
}

private static byte[] load(FileMeta meta) throws IOException {
Expand All @@ -22,4 +21,11 @@ private static byte[] load(FileMeta meta) throws IOException {
}
}

}
public FileMeta meta() {
return meta;
}

public byte[] content() throws IOException {
return load(meta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ public interface GitHubFiles {
"Add Security.md file to repo");
FileMeta CODE_OF_CONDUCT = new FileMeta("CODE_OF_CONDUCT.md", "Add code of conduct file",
"Add_Code_of_Conduct_v2", "Add CODE_OF_CONDUCT.md file to repo");
FileMeta CODE_OWNERS = new FileMeta(".github/CODEOWNERS", "Add code owner file",
"Add_CODEOWNERS", "Add CODEOWNERS file to repo");

public record FileMeta(String filePath, String pullRequestTitle, String branchName, String commitMessage) {
record FileMeta(String filePath, String pullRequestTitle, String branchName, String commitMessage) {

public String filePath() {
return filePath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.axonivy.github.file;

import static com.axonivy.github.file.GitHubFiles.CODE_OF_CONDUCT;
import static com.axonivy.github.file.GitHubFiles.LICENSE;
import static com.axonivy.github.file.GitHubFiles.SECURITY;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.axonivy.github.file.GitHubFiles.FileMeta;

import static com.axonivy.github.file.GitHubFiles.*;

public class GitHubMissingFiles {

private static final List<FileMeta> REQUIRED_FILES = List.of(LICENSE, SECURITY, CODE_OF_CONDUCT);
Expand All @@ -33,6 +31,9 @@ public static void main(String[] args) throws IOException {
var returnedStatus = detector.removeFile(workingOrganizations);
status = returnedStatus != 0 ? returnedStatus : status;
}
var codeOwnerDetector = new CodeOwnerFilesDetector(CODE_OWNERS, user);
var returnedStatus = codeOwnerDetector.requireFile(workingOrganizations);
status = returnedStatus != 0 ? returnedStatus : status;
System.exit(status);
}

Expand Down
Loading

0 comments on commit f6a45d2

Please sign in to comment.