Code Migration Process – GitHub - Overview
- Developer creates a branch, writes code, etc.
- Developer commits branch when work is completed
- Production migration – Developer does a pull request to pull branch into master.
- After two approvals are complete, a developer can merge the code
- Developer merges code into master
- Git is run in production against to clone the repository from master... I'm writing a custom program using JGit to validate the merge has been completed into master.
- Code is deployed to production
Problem:
I’m trying to pro grammatically check if a developer has merged their code into master. I cannot get this step to work. All of the examples I’ve reviewed have commits to the branch and master. Thereafter the examples subsequently check to see if the branch is committed. This is not a viable option for my program because the merge happens in the GitHub after approvals have been completed.
When I clone the repository JGit and attempt to check the if other branch has been merged, the parse commit fails and returns null. The inverse is true as well, if I clone the branch and check if master has a commit it fails. It’s obvious that I’m doing something wrong here. Sample code of what I'm talking about:
try( RevWalk revWalk = new RevWalk( repository ) ) {
RevCommit masterHead = revWalk.parseCommit( repository.resolve( "refs/heads/master" );
RevCommit otherHead = revWalk.parseCommit( repository.resolve( "refs/heads/other-branch" );
if( revWalk.isMergedInto( otherHead, masterHead ) ) {
...
}
}
I also attempted to look at the repository state but that doesn’t work either. If anyone has ideas besides using the git command line tool, I would appreciate the help.
Misc Info
Java version 1.7 - This is what we have production.
Jar Version - org.eclipse.jgit-4.5.4.201711221230-r.jar
Examples I reviewed:
https://gist.github.com/rherrmann/433adb44b3d15ed0f0c7
https://www.codeaffine.com/2015/12/15/getting-started-with-jgit/
How to determine with JGit which branches have been merged to master?
My Code
public class GitHubTest
{
public Repository repository;
public GitHubTest()
{
}
public static ArrayList<String> fetchGitBranches(String gitUrl)
{
Collection<Ref> refs;
ArrayList<String> branches = new ArrayList<String>();
try {
refs = Git.lsRemoteRepository().setHeads(true)
.setRemote(gitUrl)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("token", "not applicable stack exchange"))
.call();
for (Ref ref: refs) {
branches.add(ref.getName().substring(ref.getName().lastIndexOf("/") + 1, ref.getName().length()));
System.out.println(ref.getName());
}
Collections.sort(branches);
} catch (InvalidRemoteException e) {
e.printStackTrace();
} catch (TransportException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
return branches;
}
public void getRepositoryCode()
{
try {
Git git =
Git.cloneRepository()
.setURI("https://github.com/FICOCorp/iww-test-create-1552931848516.git")
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("token", "not applicable stack exchange"))
.setBranch("IKE_TEST2")
.setDirectory(new File("C:\\z_git")).call();
} catch (TransportException e) {
e.printStackTrace();
} catch (InvalidRemoteException e) {
e.printStackTrace();
} catch (GitAPIException e) {
e.printStackTrace();
}
}
public void setRespository(String repo)
{
try {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
repository =
builder.findGitDir(new File(repo)).readEnvironment()
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
public void isBranchMerged( String branch)
{
try {
Git git = new Git(repository);
System.out.println("git.getRepository().getFullBranch() ="+git.getRepository().getFullBranch() );
RepositoryState r = git.getRepository().getRepositoryState();
System.out.println("r.canCommit(); =" + r.canCommit());
//System.out.println("r.canCommit(); ="+r. );
if (r == RepositoryState.MERGING) {
System.out.println("RepositoryState.MERGING = Y");
}
else {
System.out.println("RepositoryState.MERGING = N");
}
if (r == RepositoryState.MERGING_RESOLVED) {
System.out.println("RepositoryState.MERGING_RESOLVED = Y");
}
else {
System.out.println("RepositoryState.MERGING_RESOLVED = N");
}
Ref master = repository.findRef("master");
// Get the object the reference points to
ObjectId masterTip = master.getObjectId();
} catch (Exception e) {
e.printStackTrace();
System.out.println( e.getMessage());
}
}
public static void main(String[] args)
{
GitHubTest gitHubTest = new GitHubTest();
gitHubTest.getRepositoryCode();
ArrayList<String> al = gitHubTest.fetchGitBranches("https://github.com/FICOCorp/iww-test-create-1552931848516.git");
for (String s: al) {
System.out.println("Known Branches " + s);
}
//gitHubTest.getRepositoryDetails();
gitHubTest.setRespository("C:\\z_git\\.git");
gitHubTest.isBranchMerged("IKE_TEST2");
//gitHubTest.listUncommittedChanges();
}
}
Screenshots