Hi All,
I want to apply some validation on commit message.
How can I achieve this.
I got one sample in github i.e., https://github.com/sford/yet-another-commit-checker
there they used for jira services so I thought it not suited for my requirement because some of the classes I'm not able to import.
I'm using stash server and atlas-sdk. created pre repository hook. and I modified for getting commit message like below in my class.
public class MyPreReceiveRepositoryHook implements PreReceiveRepositoryHook
{
private CommitService commitService;
public MyPreReceiveRepositoryHook(CommitService commitService) {
this.commitService = commitService;
}
public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)
{
final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(context.getRepository()) .exclude(((RefChange)refChanges).getFromHash()).include(((RefChange)refChanges).getToHash()) .build();
final Page<Changeset> cs = commitService.getChangesetsBetween(request, new PageRequestImpl(0, PageRequest.MAX_PAGE_LIMIT));
String commitMessage;
for(Changeset changeset: cs.getValues())
{
commitMessage = changeset.getMessage();
hookResponse.out().println(commitMessage);
}
return true;
}
}
With this I want to see the commit messages but I got this error on Git(client console).
remote: Hook com.konylabs.stash.plugin.hook.MyPreReceiveRepositoryHook failed. Error:
remote: com.google.common.collect.SingletonImmutableList cannot be cast to com.atlassian.stash.repository.RefChange
Regards
Anil
The reason for getting the exception is that here:
final ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(context.getRepository()) .exclude(((RefChange)refChanges).getFromHash()).include(((RefChange)refChanges).getToHash()) .build();
You try to cast a Collection<RefChange> to a single RefChange.
Instead, you need to iterate over the collection like this:
for(RefChange refChange: refChanges) { ChangesetsBetweenRequest request = new ChangesetsBetweenRequest .Builder(context.getRepository()) .exclude(refChange.getFromHash()) .include(refChange.getToHash()).build(); [do the rest of the stuff here] }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anil,
I am the author of Yet Another Commit Checker. I just saw your question... what classes are you not able to import that would prevent you from using YACC? I am just wondering if there is something I can do to improve my plugin. Thanks!
Sean
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sean,
Glad to got the reply from you.
Currently I'm not able to import this one
org.eclipse.jgit.lib.Repository.
And one doubt is.
A)For suppose If I create a repo and pushed some changes.
B)Later I enabled this hook so here it will validate for current messge(Whether the user provides ticket number or not). Will it check the pushed changes of A or not.
Earlier when I used,
ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder
( context.getRepository()).exclude(refChange.getFromHash()) .include
(refChange.getToHash()).build();
Some times I found that, "refChange.getFromHash() given SHA-ID
0000000000000000000000000...00000". then it looked for all the changesets.
When I checkd the latest yacc plugin..I saw ChangesetsBetweenRequest request =
new ChangesetsBetweenRequest.Builder(repository).exclude(getBranches
(repository)).include(refChange.getToHash()).build();
So with latest plugin will my problemsolve..I dont want to check the earlier pushed changes.
Thanks
Anil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Is there any way to get only latest commit message.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is there an answer for this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am supprised that your code actually compiled with a cast exception.
You should also take a look at this example (https://bitbucket.org/cofarrell/stash-enforce-message-hook-plugin/src)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I used similar code, but couldn't get the latest commit message (the one in the current push) out to compare with my regx. Below is my code, and from the print out I only see the previous pushed in commit messages. Can anyone give some suggestion?
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.stash.hook.*;
import com.atlassian.stash.hook.repository.*;
import com.atlassian.stash.repository.*;
import com.atlassian.stash.commit.*;
import com.atlassian.stash.util.*;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
@Scanned
public class CommitVerifyHook implements PreReceiveRepositoryHook
{
private final CommitService _commitService;
@Autowired
public CommitVerifyHook(@ComponentImport CommitService commitService_) {
_commitService = commitService_;
}
@Override
public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)
{
for (RefChange refChange : refChanges)
{
CommitsBetweenRequest request = new CommitsBetweenRequest.Builder(context.getRepository())
.exclude(refChange.getToHash())
.include(refChange.getFromHash()).build();
Page<Commit> page = _commitService.getCommitsBetween(request, PageUtils.newRequest(0, 25));
Iterable<Commit> it = page.getValues();
String msgs = "";
//page.getValues().forEach();
for(Commit cm : it)
{
System.out.println("the commit = " + cm.getMessage());
msgs += cm.getMessage();
}
if( msgs == null) {
hookResponse.err().println("The ref cannot be committed'" + msgs + "'");
return false;
}
}
return true;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.