Skip to content

Commit

Permalink
JENKINS-28596 Fix for hardcoded branch name:
Browse files Browse the repository at this point in the history
When resolving accumulated commit messages, branch name was hardcoded to
master.

This change will allow custom named integration branch.
  • Loading branch information
Bue Petersen committed Jun 29, 2015
1 parent 8702ef4 commit ea943f3
Show file tree
Hide file tree
Showing 8 changed files with 741 additions and 8 deletions.
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
Expand All @@ -23,6 +24,8 @@
public class GetAllCommitsFromBranchCallback extends RepositoryListenerAwareCallback<String> {
public final ObjectId id;
public final String branch;
private static final Logger logger = Logger.getLogger(GetAllCommitsFromBranchCallback.class.getName());
private static final String LOG_PREFIX = "[PREINT] ";

public GetAllCommitsFromBranchCallback(TaskListener listener, final ObjectId id, final String branch ) {
super(listener);
Expand All @@ -31,7 +34,8 @@ public GetAllCommitsFromBranchCallback(TaskListener listener, final ObjectId id,
}

@Override
public String invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException {
public String invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException {
logger.entering("GetAllCommitsFromBranchCallback", "invoke", new Object[]{channel, repo});
StringBuilder sb = new StringBuilder();
RevWalk walk = new RevWalk(repo);

Expand All @@ -41,9 +45,10 @@ public String invoke(Repository repo, VirtualChannel channel) throws IOException
// walk tree starting from the integration commit
walk.markStart(commit);

logger.info(String.format(LOG_PREFIX + "Collecting commit message until reaching branch %s", branch));
// limit the tree walk to keep away from master commits
// Reference for this idea is: https://wiki.eclipse.org/JGit/User_Guide#Restrict_the_walked_revision_graph
ObjectId to = repo.resolve("master");
ObjectId to = repo.resolve(branch);
walk.markUninteresting(walk.parseCommit(to));

// build the complete commit message, to look like squash commit msg
Expand All @@ -57,7 +62,10 @@ public String invoke(Repository repo, VirtualChannel channel) throws IOException
sb.append(String.format("%n"));

Integer secondsSinceUnixEpoch = rev.getCommitTime();
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ZZZZ"); //yyyy-MM-dd'T'hh:mm:ssZZZZ");
// Note that the git log shows different date formats, depending on configuration.
// The choices in the git commit message below matches the squashed commit message
// that git generates on a Ubuntu Linux 14.04 with default git installation.
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM d kk:mm:ss yyyy ZZZZ");
Date commitTime = new Date(secondsSinceUnixEpoch * 1000L); // seconds to milis
String asString = formatter.format(commitTime);
sb.append(String.format("Date: %s", asString ));
Expand Down Expand Up @@ -86,6 +94,7 @@ public String invoke(Repository repo, VirtualChannel channel) throws IOException

walk.dispose();

logger.exiting("GetAllCommitsFromBranchCallback", "invoke", new Object[]{channel, repo});
return sb.toString();
}
}

Large diffs are not rendered by default.

Expand Up @@ -51,7 +51,9 @@ public StaticGitRepositoryTestBase() {
testMethodName_vs_staticGitRepoName.put( "commitMessagesWithDoubleQuotesSquashedWindows", "commitMessagesWithDoubleQuotes_windows");
testMethodName_vs_staticGitRepoName.put( "commitMessagesWithDoubleQuotesAccumulatedWindows", "commitMessagesWithDoubleQuotes_windows");
testMethodName_vs_staticGitRepoName.put( "authorOfLastCommitUsedIfMoreThanOneCommitSquashStrategy", "useAuthorOfLastCommit");
testMethodName_vs_staticGitRepoName.put( "authorOfLastCommitUsedIfMoreThanOneCommitAccumulatedStrategy", "useAuthorOfLastCommit");
testMethodName_vs_staticGitRepoName.put( "authorOfLastCommitUsedIfMoreThanOneCommitAccumulatedStrategy", "useAuthorOfLastCommit");
testMethodName_vs_staticGitRepoName.put( "customIntegrationBranchSquashStrategy", "customIntegrationBranch");
testMethodName_vs_staticGitRepoName.put( "customIntegrationBranchAccumulatedStrategy", "customIntegrationBranch");
}

public File tempFolder;
Expand Down Expand Up @@ -121,7 +123,7 @@ public void setUp() throws Exception {
.setCloneAllBranches(true)
.setNoCheckout(false)
.call().close();
// Open it to add stuff below
// Open it
gitrepo = Git.open(workingRepoPath);
}

Expand All @@ -132,6 +134,8 @@ public void setUp() throws Exception {
*/
@After
public void tearDown() throws Exception {
gitrepo.close();
bareRepository.close();
// Repos reside inside temporary folder pr. test, clean up
TestUtilsFactory.destroyDirectory(tempFolder);
}
Expand Down
Expand Up @@ -240,6 +240,11 @@ public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule r
return configurePretestedIntegrationPlugin(rule, type, Collections.singletonList(new UserRemoteConfig("file://" + repo.getDirectory().getAbsolutePath(), null, null, null)), null, true);
}

public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule rule, STRATEGY_TYPE type, Repository repo, boolean runOnSlave, String integrationBranch) throws Exception {
return configurePretestedIntegrationPlugin(rule, type, Collections.singletonList(new UserRemoteConfig("file://" + repo.getDirectory().getAbsolutePath(), null, null, null)), null, runOnSlave, integrationBranch);
}


public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule rule, STRATEGY_TYPE type, Repository repo, boolean runOnSlave) throws Exception {
return configurePretestedIntegrationPlugin(rule, type, Collections.singletonList(new UserRemoteConfig("file://" + repo.getDirectory().getAbsolutePath(), null, null, null)), null, runOnSlave);
}
Expand All @@ -251,8 +256,13 @@ public static void triggerProject( FreeStyleProject project ) throws Exception
scmTrigger.start(project, true);
scmTrigger.new Runner().run();
}

public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule rule, STRATEGY_TYPE type, List<UserRemoteConfig> repoList, String repoName, boolean runOnSlave) throws Exception {
return configurePretestedIntegrationPlugin(rule, type, repoList, repoName, runOnSlave, "master");

}

public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule rule, STRATEGY_TYPE type, List<UserRemoteConfig> repoList, String repoName, boolean runOnSlave, String integrationBranch) throws Exception {
FreeStyleProject project = rule.createFreeStyleProject();
if (runOnSlave) {
DumbSlave onlineSlave = rule.createOnlineSlave();
Expand All @@ -262,9 +272,9 @@ public static FreeStyleProject configurePretestedIntegrationPlugin(JenkinsRule r
GitBridge gitBridge = null;

if(type == STRATEGY_TYPE.SQUASH) {
gitBridge = new GitBridge(new SquashCommitStrategy(), "master", repoName);
gitBridge = new GitBridge(new SquashCommitStrategy(), integrationBranch, repoName);
} else {
gitBridge = new GitBridge(new AccumulatedCommitStrategy(), "master", repoName);
gitBridge = new GitBridge(new AccumulatedCommitStrategy(), integrationBranch, repoName);
}

project.getBuildWrappersList().add(new PretestedIntegrationBuildWrapper(gitBridge));
Expand Down Expand Up @@ -714,8 +724,10 @@ public static boolean checkForLineInFile(File file, String stringToCheck) throws

return result;
} catch (FileNotFoundException e1) {
System.out.println(String.format("TestUtilsFactory.checkForLineInFile throwed an exception: %s", e1.toString()));
return false;
} catch (IOException ep) {
System.out.println(String.format("TestUtilsFactory.checkForLineInFile throwed an exception: %s", ep.toString()));
return false;
}
}
Expand Down
203 changes: 203 additions & 0 deletions src/test/resources/customIntegrationBranch-repo_description.log
@@ -0,0 +1,203 @@
###############################################################################
# Repository creation log and tools version for this test repository
###############################################################################

Git version:
git version 1.9.1

Linux:
Linux orange-one 3.13.0-49-generic #83-Ubuntu SMP Fri Apr 10 20:11:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
LSB Version: core-2.0-amd64:core-2.0-noarch:core-3.0-amd64:core-3.0-noarch:core-3.1-amd64:core-3.1-noarch:core-3.2-amd64:core-3.2-noarch:core-4.0-amd64:core-4.0-noarch:core-4.1-amd64:core-4.1-noarch:security-4.0-amd64:security-4.0-noarch:security-4.1-amd64:security-4.1-noarch


###############################################################################
# Creating basis repository
###############################################################################
After basis repository and push of master branch to origin:
--------------------------------------------------------------------------------
--- git log graph: ---
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (1 second ago) (HEAD, origin/master, master)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (2 seconds ago)
Initial commit on master branch - added README - Praqma Support

--- git log: ---
commit 2086dc9f6d23931f3b8176de640bd6e15f5afc08
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:41 2015 +0200

Second commit on on master branch - updated README

commit 8ae8f122666d7340ef2923c3a49150f86f3dcfd0
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:40 2015 +0200

Initial commit on master branch - added README




###############################################################################
# Now creating a branch, that will serve as our custom integration branch...
###############################################################################
After custom integration branch and push to origin:
--------------------------------------------------------------------------------
--- git log graph: ---
* c1b3225 - Wed, 3 Jun 2015 14:03:42 +0200 (2 seconds ago) (HEAD, origin/customIntegrationBranch, customIntegrationBranch)
| Updated readme file on new custom integration branch - Praqma Support
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (3 seconds ago) (origin/master, master)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (4 seconds ago)
Initial commit on master branch - added README - Praqma Support

--- git log: ---
commit c1b32254ce62190bd7650b7d44e9aec4983d5c0a
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:42 2015 +0200

Updated readme file on new custom integration branch

commit 2086dc9f6d23931f3b8176de640bd6e15f5afc08
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:41 2015 +0200

Second commit on on master branch - updated README

commit 8ae8f122666d7340ef2923c3a49150f86f3dcfd0
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:40 2015 +0200

Initial commit on master branch - added README




###############################################################################
# Adding a commit on master, after we started to use custom integration branch
# to test with a more complex git graph
###############################################################################
After last push to origin of master branch:
--------------------------------------------------------------------------------
--- git log graph: ---
* 31bc93a - Wed, 3 Jun 2015 14:03:44 +0200 (1 second ago) (HEAD, origin/master, master)
| Last line in readme, added from last commit on master. We integrate to another branch from here on. - Praqma Support
| * c1b3225 - Wed, 3 Jun 2015 14:03:42 +0200 (3 seconds ago) (origin/customIntegrationBranch, customIntegrationBranch)
|/ Updated readme file on new custom integration branch - Praqma Support
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (4 seconds ago)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (5 seconds ago)
Initial commit on master branch - added README - Praqma Support

--- git log: ---
commit 31bc93a3e3e5b9539b9ce02786d773bbedd66ec9
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:44 2015 +0200

Last line in readme, added from last commit on master. We integrate to another branch from here on.

commit 2086dc9f6d23931f3b8176de640bd6e15f5afc08
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:41 2015 +0200

Second commit on on master branch - updated README

commit 8ae8f122666d7340ef2923c3a49150f86f3dcfd0
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:40 2015 +0200

Initial commit on master branch - added README




###############################################################################
# Starting on development branch which will be pushed to ready and
# integrated by the Jenkins job later
# Doing two commits, based on the custom integration branch as starting point
###############################################################################
After push of the two commits on the development branch:
--------------------------------------------------------------------------------
--- git log graph: ---
* c1449e0 - Wed, 3 Jun 2015 14:03:46 +0200 (1 second ago) (HEAD, origin/ready/myDevelopmentBranch, myDevelopmentBranch)
| Added a second line from myDevelopmentBranch in test commit log file. - Praqma Support
* 70353ce - Wed, 3 Jun 2015 14:03:45 +0200 (2 seconds ago)
| Added line from myDevelopmentBranch in test commit log file. - Praqma Support
* c1b3225 - Wed, 3 Jun 2015 14:03:42 +0200 (5 seconds ago) (origin/customIntegrationBranch, customIntegrationBranch)
| Updated readme file on new custom integration branch - Praqma Support
| * 31bc93a - Wed, 3 Jun 2015 14:03:44 +0200 (3 seconds ago) (origin/master, master)
|/ Last line in readme, added from last commit on master. We integrate to another branch from here on. - Praqma Support
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (6 seconds ago)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (7 seconds ago)
Initial commit on master branch - added README - Praqma Support

--- git log: ---
commit c1449e075f528974c63eef81109d0632eaada0c7
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:46 2015 +0200

Added a second line from myDevelopmentBranch in test commit log file.

commit 70353ce6771866f29c38b4460b3f74f9024f8ce2
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:45 2015 +0200

Added line from myDevelopmentBranch in test commit log file.

commit c1b32254ce62190bd7650b7d44e9aec4983d5c0a
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:42 2015 +0200

Updated readme file on new custom integration branch

commit 2086dc9f6d23931f3b8176de640bd6e15f5afc08
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:41 2015 +0200

Second commit on on master branch - updated README

commit 8ae8f122666d7340ef2923c3a49150f86f3dcfd0
Author: Praqma Support <support@praqma.net>
Date: Wed Jun 3 14:03:40 2015 +0200

Initial commit on master branch - added README




###############################################################################
# Printing some final views of the repository
###############################################################################


View of repository from integration branch:
--------------------------------------------------------------------------------
--- git log graph: ---
* c1449e0 - Wed, 3 Jun 2015 14:03:46 +0200 (1 second ago) (origin/ready/myDevelopmentBranch, myDevelopmentBranch)
| Added a second line from myDevelopmentBranch in test commit log file. - Praqma Support
* 70353ce - Wed, 3 Jun 2015 14:03:45 +0200 (2 seconds ago)
| Added line from myDevelopmentBranch in test commit log file. - Praqma Support
* c1b3225 - Wed, 3 Jun 2015 14:03:42 +0200 (5 seconds ago) (HEAD, origin/customIntegrationBranch, customIntegrationBranch)
| Updated readme file on new custom integration branch - Praqma Support
| * 31bc93a - Wed, 3 Jun 2015 14:03:44 +0200 (3 seconds ago) (origin/master, master)
|/ Last line in readme, added from last commit on master. We integrate to another branch from here on. - Praqma Support
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (6 seconds ago)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (7 seconds ago)
Initial commit on master branch - added README - Praqma Support

View of repository from development branch:
--------------------------------------------------------------------------------
--- git log graph: ---
* c1449e0 - Wed, 3 Jun 2015 14:03:46 +0200 (1 second ago) (HEAD, origin/ready/myDevelopmentBranch, myDevelopmentBranch)
| Added a second line from myDevelopmentBranch in test commit log file. - Praqma Support
* 70353ce - Wed, 3 Jun 2015 14:03:45 +0200 (2 seconds ago)
| Added line from myDevelopmentBranch in test commit log file. - Praqma Support
* c1b3225 - Wed, 3 Jun 2015 14:03:42 +0200 (5 seconds ago) (origin/customIntegrationBranch, customIntegrationBranch)
| Updated readme file on new custom integration branch - Praqma Support
| * 31bc93a - Wed, 3 Jun 2015 14:03:44 +0200 (3 seconds ago) (origin/master, master)
|/ Last line in readme, added from last commit on master. We integrate to another branch from here on. - Praqma Support
* 2086dc9 - Wed, 3 Jun 2015 14:03:41 +0200 (6 seconds ago)
| Second commit on on master branch - updated README - Praqma Support
* 8ae8f12 - Wed, 3 Jun 2015 14:03:40 +0200 (7 seconds ago)
Initial commit on master branch - added README - Praqma Support
7 changes: 7 additions & 0 deletions src/test/resources/customIntegrationBranch.md
@@ -0,0 +1,7 @@
# Test repository description for custom integration branch tests


Related to [JENKINS-28596 Hardcoded branch name when resolving commit messages](https://issues.jenkins-ci.org/browse/JENKINS-28596) we have created the _customIntegrationBranch_ test repository for testing the use case where the plugin is configured to integrate development branch changed on the custom integration branch - that is something else than master.


See the log-file `customIntegrationBranch-repo_description.log` for details on the repository.

0 comments on commit ea943f3

Please sign in to comment.