Search in sources :

Example 1 with RegistrationHandle

use of com.google.gerrit.extensions.registration.RegistrationHandle in project gerrit by GerritCodeReview.

the class ChangeIT method customCommitFooters.

@Test
public void customCommitFooters() throws Exception {
    PushOneCommit.Result change = createChange();
    RegistrationHandle handle = changeMessageModifiers.add(new ChangeMessageModifier() {

        @Override
        public String onSubmit(String newCommitMessage, RevCommit original, RevCommit mergeTip, Branch.NameKey destination) {
            assertThat(original.getName()).isNotEqualTo(mergeTip.getName());
            return newCommitMessage + "Custom: " + destination.get();
        }
    });
    ChangeInfo actual;
    try {
        EnumSet<ListChangesOption> options = EnumSet.of(ListChangesOption.ALL_REVISIONS, ListChangesOption.COMMIT_FOOTERS);
        actual = gApi.changes().id(change.getChangeId()).get(options);
    } finally {
        handle.remove();
    }
    List<String> footers = new ArrayList<>(Arrays.asList(actual.revisions.get(change.getCommit().getName()).commitWithFooters.split("\\n")));
    // remove subject + blank line
    footers.remove(0);
    footers.remove(0);
    List<String> expectedFooters = Arrays.asList("Change-Id: " + change.getChangeId(), "Reviewed-on: " + canonicalWebUrl.get() + change.getChange().getId(), "Custom: refs/heads/master");
    assertThat(footers).containsExactlyElementsIn(expectedFooters);
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ArrayList(java.util.ArrayList) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Branch(com.google.gerrit.reviewdb.client.Branch) ChangeMessageModifier(com.google.gerrit.server.git.ChangeMessageModifier) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with RegistrationHandle

use of com.google.gerrit.extensions.registration.RegistrationHandle in project gerrit by GerritCodeReview.

the class HttpPluginServlet method install.

private void install(Plugin plugin) {
    GuiceFilter filter = load(plugin);
    final String name = plugin.getName();
    final PluginHolder holder = new PluginHolder(plugin, filter);
    plugin.add(new RegistrationHandle() {

        @Override
        public void remove() {
            plugins.remove(name, holder);
        }
    });
    plugins.put(name, holder);
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) GuiceFilter(com.google.inject.servlet.GuiceFilter)

Example 3 with RegistrationHandle

use of com.google.gerrit.extensions.registration.RegistrationHandle in project gerrit by GerritCodeReview.

the class SubmitByCherryPickIT method changeMessageOnSubmit.

@Test
public void changeMessageOnSubmit() throws Exception {
    PushOneCommit.Result change = createChange();
    RegistrationHandle handle = changeMessageModifiers.add(new ChangeMessageModifier() {

        @Override
        public String onSubmit(String newCommitMessage, RevCommit original, RevCommit mergeTip, Branch.NameKey destination) {
            return newCommitMessage + "Custom: " + destination.get();
        }
    });
    try {
        submit(change.getChangeId());
    } finally {
        handle.remove();
    }
    testRepo.git().fetch().setRemote("origin").call();
    ChangeInfo info = get(change.getChangeId());
    RevCommit c = testRepo.getRevWalk().parseCommit(ObjectId.fromString(info.currentRevision));
    testRepo.getRevWalk().parseBody(c);
    assertThat(c.getFooterLines("Custom")).containsExactly("refs/heads/master");
    assertThat(c.getFooterLines(FooterConstants.REVIEWED_ON)).hasSize(1);
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Branch(com.google.gerrit.reviewdb.client.Branch) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ChangeMessageModifier(com.google.gerrit.server.git.ChangeMessageModifier) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 4 with RegistrationHandle

use of com.google.gerrit.extensions.registration.RegistrationHandle in project gerrit by GerritCodeReview.

the class SubmitByRebaseAlwaysIT method changeMessageOnSubmit.

@Test
@TestProjectInput(useContentMerge = InheritableBoolean.TRUE)
public void changeMessageOnSubmit() throws Exception {
    PushOneCommit.Result change1 = createChange();
    PushOneCommit.Result change2 = createChange();
    RegistrationHandle handle = changeMessageModifiers.add(new ChangeMessageModifier() {

        @Override
        public String onSubmit(String newCommitMessage, RevCommit original, RevCommit mergeTip, Branch.NameKey destination) {
            List<String> custom = mergeTip.getFooterLines("Custom");
            if (!custom.isEmpty()) {
                newCommitMessage += "Custom-Parent: " + custom.get(0) + "\n";
            }
            return newCommitMessage + "Custom: " + destination.get();
        }
    });
    try {
        // change1 is a fast-forward, but should be rebased in cherry pick style
        // anyway, making change2 not a fast-forward, requiring a rebase.
        approve(change1.getChangeId());
        submit(change2.getChangeId());
    } finally {
        handle.remove();
    }
    // ... but both changes should get custom footers.
    assertThat(getCurrentCommit(change1).getFooterLines("Custom")).containsExactly("refs/heads/master");
    assertThat(getCurrentCommit(change2).getFooterLines("Custom")).containsExactly("refs/heads/master");
    assertThat(getCurrentCommit(change2).getFooterLines("Custom-Parent")).containsExactly("refs/heads/master");
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) Branch(com.google.gerrit.reviewdb.client.Branch) List(java.util.List) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) ChangeMessageModifier(com.google.gerrit.server.git.ChangeMessageModifier) RevCommit(org.eclipse.jgit.revwalk.RevCommit) TestProjectInput(com.google.gerrit.acceptance.TestProjectInput) Test(org.junit.Test)

Example 5 with RegistrationHandle

use of com.google.gerrit.extensions.registration.RegistrationHandle in project gerrit by GerritCodeReview.

the class HttpPluginServlet method load.

private GuiceFilter load(Plugin plugin) {
    if (plugin.getHttpInjector() != null) {
        final String name = plugin.getName();
        final GuiceFilter filter;
        try {
            filter = plugin.getHttpInjector().getInstance(GuiceFilter.class);
        } catch (RuntimeException e) {
            log.warn(String.format("Plugin %s cannot load GuiceFilter", name), e);
            return null;
        }
        try {
            ServletContext ctx = PluginServletContext.create(plugin, wrapper.getFullPath(name));
            filter.init(new WrappedFilterConfig(ctx));
        } catch (ServletException e) {
            log.warn(String.format("Plugin %s failed to initialize HTTP", name), e);
            return null;
        }
        plugin.add(new RegistrationHandle() {

            @Override
            public void remove() {
                filter.destroy();
            }
        });
        return filter;
    }
    return null;
}
Also used : ServletException(javax.servlet.ServletException) RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) GuiceFilter(com.google.inject.servlet.GuiceFilter) ServletContext(javax.servlet.ServletContext)

Aggregations

RegistrationHandle (com.google.gerrit.extensions.registration.RegistrationHandle)8 GuiceFilter (com.google.inject.servlet.GuiceFilter)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)3 Branch (com.google.gerrit.reviewdb.client.Branch)3 ChangeMessageModifier (com.google.gerrit.server.git.ChangeMessageModifier)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 Test (org.junit.Test)3 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 TestProjectInput (com.google.gerrit.acceptance.TestProjectInput)1 ListChangesOption (com.google.gerrit.extensions.client.ListChangesOption)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1