Skip to content

Commit

Permalink
Merge pull request #13 from gemoc/generate_syncRepord
Browse files Browse the repository at this point in the history
Generate sync repord
  • Loading branch information
dvojtise authored Sep 13, 2024
2 parents ddc080e + ab7acd7 commit d4d12c9
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
packages: write

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Maven Build

on:
push:
branches: [ master ]
# branches: [ master ]
pull_request:
branches: [ master ]

Expand All @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Stage result files
run: mkdir staging && cp sync-git-submodules-branches/target/*.jar staging
- name: Upload Artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: Package
path: staging
Expand Down
13 changes: 13 additions & 0 deletions sync-git-submodules-branches/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,18 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
6 changes: 3 additions & 3 deletions sync-git-submodules-branches/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.gemoc.git-sync-tools</groupId>
<artifactId>sync-git-submodules-branches-plugin</artifactId>
<version>1.0.4</version>
<version>1.1.0</version>
<packaging>maven-plugin</packaging>

<name>sync-git-submodules-branches Maven Plugin</name>
Expand Down Expand Up @@ -107,12 +107,12 @@
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<!--<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</execution>-->
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.gemoc.sync_git_submodules_branches;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Set;

Expand All @@ -25,6 +27,8 @@ public static void main(String[] args) throws Exception {
.addOption("g", "gitURL", true, "git URL that will be cloned")
.addOption("c", "committerName", true, "name of the committer who'll sign the commit")
.addOption("e", "committerEmail", true, "email of the committer who'll sign the commit")
.addOption("d", "dryRun", false, "dryRun, do not commit and push the update")
.addOption("r", "reportFile", true, "file name tha will containt the markdown report")
.addOption("i", "inactivityThreshold", true, "number of days since the last commit of a specific branch before considering the branch as old/unmaintained/inactive (-1 for infinite duration)");


Expand All @@ -41,6 +45,8 @@ public static void main(String[] args) throws Exception {
String committerName = cmd.hasOption("c") ? cmd.getOptionValue("c") : "";
String committerEmail = cmd.hasOption("e") ? cmd.getOptionValue("e") : "";
String inactivityThreshold = cmd.hasOption("i") ? cmd.getOptionValue("i") : "90";
String reportFilePath = cmd.hasOption("r") ? cmd.getOptionValue("r") : "syncReport.md";
boolean dryRun = cmd.hasOption("d");

if(parentGitURL.isEmpty()) {
HelpFormatter formatter = new HelpFormatter();
Expand All @@ -66,13 +72,24 @@ public static void main(String[] args) throws Exception {
Set<String> relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(Integer.parseInt(inactivityThreshold));
gitManager.deleteBranchesNotIn(relevantBranches);
gitManager.createMissingParentBranches(relevantBranches);
gitManager.updateAllBranchesModules();

StringBuffer sb = new StringBuffer();
gitManager.updateAllBranchesModules(sb, dryRun);
FileUtils.write(outputDirectory, sb.toString(), Charset.defaultCharset());
writeReport(new File(reportFilePath), sb);
if(directoryPath.isEmpty()) {
// must delete the temp dir
System.out.println("Deleting temp directory "+outputDirectory);
FileUtils.deleteDirectory(outputDirectory);
}
}

protected static void writeReport(File reportFile, StringBuffer content) throws IOException {
// Ensure the parent directory exists
File parentDir = reportFile.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
FileUtils.write(reportFile, content.toString(), Charset.defaultCharset());
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.gemoc.sync_git_submodules_branches;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Set;

import org.apache.commons.io.FileUtils;

/*
* Copyright 2001-2005 The Apache Software Foundation.
*
Expand Down Expand Up @@ -38,11 +42,17 @@ public class SyncGitSubmodulesBranchesMojo
extends AbstractMojo
{
/**
* Location of the file.
* Location of the git repository.
*/
@Parameter( defaultValue = "${project.build.directory}/syncgitsubmodules_repo", property = "outputDir", required = true )
private File outputDirectory;

/**
* Location of the report file.
*/
@Parameter( defaultValue = "${project.build.directory}/syncReport.md", property = "reportFile", required = true )
private File reportFile;

@Parameter(property="parentGitURL", required = true)
private String parentGitURL;

Expand All @@ -59,6 +69,9 @@ public class SyncGitSubmodulesBranchesMojo
@Parameter(defaultValue = "", property="committerName")
private String committerName;

@Parameter(defaultValue = "false", property="dryRun")
private boolean dryRun;

/**
* number of days since the last commit of a specific branch
* The branch will be considered old/unmaintained /inactive
Expand Down Expand Up @@ -91,12 +104,28 @@ public void execute()
Set<String> relevantBranches = gitManager.collectAllSubmodulesActiveRemoteBranches(inactivityThreshold);
gitManager.deleteBranchesNotIn(relevantBranches);
gitManager.createMissingParentBranches(relevantBranches);
gitManager.updateAllBranchesModules();
StringBuffer sb = new StringBuffer();
gitManager.updateAllBranchesModules(sb, dryRun);
writeReport(sb);

} catch (Exception e) {
getLog().error( e);
throw new MojoExecutionException(e.getMessage(), e);
}


}

protected void writeReport(StringBuffer content) throws MojoExecutionException {
// Ensure the parent directory exists
File parentDir = reportFile.getParentFile();
if (parentDir != null && !parentDir.exists()) {
parentDir.mkdirs();
}
try {
FileUtils.write(reportFile, content.toString(), Charset.defaultCharset());
} catch (IOException e) {
throw new MojoExecutionException("Error writing to file", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,15 @@ public void createBranchForModules(Git parentgit, String missingParentBranch)
}
}

public void updateAllBranchesModules() throws IOException, GitAPIException, GitSyncError, ConfigInvalidException {
/**
*
* @param dryRun report only, do not perform changes
* @throws IOException
* @throws GitAPIException
* @throws GitSyncError
* @throws ConfigInvalidException
*/
public void updateAllBranchesModules(StringBuffer reportBuffer, boolean dryRun) throws IOException, GitAPIException, GitSyncError, ConfigInvalidException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();

try (Repository parentRepository = builder.setMustExist(true).setGitDir(new File(localGitFolder + "/.git"))
Expand All @@ -394,12 +402,15 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS
for (Ref ref : call) {
if (ref.getName().startsWith("refs/remotes/origin/")) {
updateBranchesForModules(parentgit,
ref.getName().substring("refs/remotes/origin/".length()));
ref.getName().substring("refs/remotes/origin/".length()),
reportBuffer,
dryRun);
}
}
}
}
}


/**
*
Expand All @@ -411,9 +422,13 @@ public void updateAllBranchesModules() throws IOException, GitAPIException, GitS
* @throws IOException
* @throws ConfigInvalidException
*/
public void updateBranchesForModules(Git parentgit, String consideredBranch)
public void updateBranchesForModules(Git parentgit, String consideredBranch, StringBuffer reportBuffer, boolean dryRun)
throws GitAPIException, GitSyncError, IOException, ConfigInvalidException {
logger.info("updateBranchesForModules branch = " + consideredBranch);
reportBuffer.append(String.format("**Branch %s**\n", consideredBranch));
reportBuffer.append("\n"
+ "| Module | Branch |\n"
+ "|:---------- |:---------- |\n");
// switch parentGit to branch
checkoutBranch(parentgit, consideredBranch);

Expand Down Expand Up @@ -442,6 +457,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
}
}
logger.info(String.format(" tracking module %-32s on branch "+trackedBranchName, walk.getModuleName()));


// Make sure the parent repo knows that its submodule now tracks a branch:
FileBasedConfig modulesConfig = new FileBasedConfig(new File(
Expand Down Expand Up @@ -475,6 +491,7 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
logger.debug("\t\tUntracked: " + status.getUntracked());
logger.debug("\t\tUntrackedFolders: " + status.getUntrackedFolders());
}
String branchModifier = "";
if(status.getAdded().size() + status.getChanged().size() +status.getRemoved().size() > 0) {
String msg;
PersonIdent committer;
Expand All @@ -493,13 +510,19 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
msg = "Updating submodule "+walk.getModuleName()+" to track head of branch "+trackedBranchName;
committer = defaultCommitter;
}
logger.debug("\t\tgit commit -m \""+msg+"\"");
parentgit.commit()
.setMessage(msg)
.setAllowEmpty(false)
.setCommitter(committer)
.call();
branchModifier = "🔄";
if(! dryRun) {
logger.debug("\t\tgit commit -m \""+msg+"\"");
parentgit.commit()
.setMessage(msg)
.setAllowEmpty(false)
.setCommitter(committer)
.call();
} else {
logger.info("\t\t[DRYRUN] git commit -m \""+msg+"\"");
}
}
reportBuffer.append(String.format("| %-32s | %-16s %s |\n", walk.getModuleName(), trackedBranchName, branchModifier));
}
}

Expand All @@ -508,22 +531,27 @@ public void updateBranchesForModules(Git parentgit, String consideredBranch)
logger.info(
"\tupdating submodules: " + s);
}*/
Iterable<PushResult> pushResps = parentgit.push()
.setCredentialsProvider(credentialProvider)
.call();
for (PushResult pushRes : pushResps) {
for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) {
if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) {
logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK);
} else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) {
logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE);

} else {
logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult);
if(!dryRun) {
Iterable<PushResult> pushResps = parentgit.push()
.setCredentialsProvider(credentialProvider)
.call();
for (PushResult pushRes : pushResps) {
for (RemoteRefUpdate pushResult : pushRes.getRemoteUpdates()) {
if(pushResult.getStatus() == RemoteRefUpdate.Status.OK) {
logger.info("push branch "+consideredBranch+" => "+RemoteRefUpdate.Status.OK);
} else if(pushResult.getStatus() == RemoteRefUpdate.Status.UP_TO_DATE) {
logger.info("nothing to push for branch "+consideredBranch+" => "+RemoteRefUpdate.Status.UP_TO_DATE);

} else {
logger.error("PB pushing branch "+consideredBranch+" => "+pushRes.getMessages()+"\" "+pushResult);
}
}
validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates());
}
validateRemoteRefUpdates("push submodule tracking branch", pushRes.getRemoteUpdates());
} else {
logger.info("\t\t[DRYRUN] not pushing branch "+consideredBranch);
}
reportBuffer.append("\n");
}
}

Expand Down

0 comments on commit d4d12c9

Please sign in to comment.