/**
   * Tests to trigger builds with two diffetent patch sets. Expecting two build to be scheduled with
   * one cause each.
   *
   * @throws Exception if so.
   */
  @LocalData
  public void testDoubleTriggeredBuildsOfDifferentChange() throws Exception {
    FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
    server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
    PluginImpl.getInstance().triggerEvent(Setup.createPatchsetCreated());
    PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
    patchsetCreated.getChange().setNumber("2000");
    PluginImpl.getInstance().triggerEvent(patchsetCreated);

    RunList<FreeStyleBuild> builds = waitForBuilds(project, 2, 5000);
    assertEquals(2, builds.size());
    assertSame(Result.SUCCESS, builds.get(0).getResult());
    assertSame(Result.SUCCESS, builds.get(1).getResult());

    int count = 0;
    for (Cause cause : builds.get(0).getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
      }
    }
    assertEquals(1, count);
    count = 0;
    for (Cause cause : builds.get(1).getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
        assertNotNull(((GerritCause) cause).getContext());
        assertNotNull(((GerritCause) cause).getEvent());
      }
    }
    assertEquals(1, count);
  }
  /**
   * Tests to trigger a build with the same patch set twice, one is a manual event and the other a
   * normal. Expecting one build to be scheduled with two causes of different type..
   *
   * @throws Exception if so.
   */
  @LocalData
  public void testDoubleTriggeredBuildOfDifferentType() throws Exception {
    FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
    server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
    PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
    ManualPatchsetCreated mpc = new ManualPatchsetCreated();
    mpc.setChange(patchsetCreated.getChange());
    mpc.setPatchset(patchsetCreated.getPatchSet());
    mpc.setUploader(patchsetCreated.getUploader());
    mpc.setUserName("bobby");
    PluginImpl.getInstance().triggerEvent(Setup.createPatchsetCreated());
    PluginImpl.getInstance().triggerEvent(mpc);

    RunList<FreeStyleBuild> builds = waitForBuilds(project, 1, 5000);
    FreeStyleBuild build = builds.get(0);
    assertSame(Result.SUCCESS, build.getResult());
    assertEquals(1, builds.size());

    int count = 0;
    for (Cause cause : build.getCauses()) {
      if (cause instanceof GerritCause) {
        count++;
        assertNotNull(((GerritCause) cause).getContext());
        assertNotNull(((GerritCause) cause).getContext().getThisBuild());
        assertNotNull(((GerritCause) cause).getEvent());
      }
    }
    assertEquals(2, count);
  }
Ejemplo n.º 3
0
 /**
  * Gets the URL for the change.
  *
  * @return the URL to the change.
  */
 public String getUrl() {
   if (tEvent.getChange() != null) {
     if (tEvent.getChange().getUrl() != null && tEvent.getChange().getUrl().length() > 0) {
       return tEvent.getChange().getUrl();
     } else {
       return PluginImpl.getInstance()
           .getConfig()
           .getGerritFrontEndUrlFor(
               tEvent.getChange().getNumber(), tEvent.getPatchSet().getNumber());
     }
   } else {
     return PluginImpl.getInstance().getConfig().getGerritFrontEndUrl();
   }
 }
 /**
  * Gets the internal list of listeners from {@link
  * com.sonymobile.tools.gerrit.gerritevents.GerritHandler#gerritEventListeners}.
  *
  * @return the list.
  */
 private Collection<GerritEventListener> getGerritEventListeners() {
   GerritHandler handler =
       Whitebox.getInternalState(
           PluginImpl.getInstance().getServer(PluginImpl.DEFAULT_SERVER_NAME),
           GerritHandler.class);
   return Whitebox.getInternalState(handler, "gerritEventListeners");
 }
 /**
  * Tests that the trigger is added as a listener during startup of the server.
  *
  * @throws Exception if so.
  */
 @LocalData
 public void testProject() throws Exception {
   GerritHandler handler =
       Whitebox.getInternalState(PluginImpl.getInstance(), GerritHandler.class);
   Map<Integer, GerritEventListener> gerritEventListeners =
       Whitebox.getInternalState(handler, "gerritEventListeners");
   assertEquals(1, gerritEventListeners.size());
 }
 /**
  * Tests that the re-loaded trigger is still there when a new triggered project is added.
  *
  * @throws Exception if so.
  */
 @LocalData
 public void testCreateNewProject() throws Exception {
   FreeStyleProject p = createGerritTriggeredJob(this, "testing1");
   GerritHandler handler =
       Whitebox.getInternalState(PluginImpl.getInstance(), GerritHandler.class);
   Map<Integer, GerritEventListener> gerritEventListeners =
       Whitebox.getInternalState(handler, "gerritEventListeners");
   assertEquals(2, gerritEventListeners.size());
 }
 /**
  * Same test logic as {@link #testBuildLatestPatchsetOnly()} except the trigger is configured to
  * not cancel the previous build.
  *
  * @throws Exception if so.
  */
 @LocalData
 public void testNotBuildLatestPatchsetOnly() throws Exception {
   ((Config) PluginImpl.getInstance().getConfig()).setGerritBuildCurrentPatchesOnly(false);
   FreeStyleProject project = DuplicatesUtil.createGerritTriggeredJob(this, "projectX");
   project.getBuildersList().add(new SleepBuilder(2000));
   server.waitForCommand(GERRIT_STREAM_EVENTS, 2000);
   PatchsetCreated firstEvent = Setup.createPatchsetCreated();
   PluginImpl.getInstance().triggerEvent(firstEvent);
   AbstractBuild firstBuild = waitForBuildToStart(firstEvent, 2000);
   PatchsetCreated secondEvent = Setup.createPatchsetCreated();
   secondEvent.getPatchSet().setNumber("2");
   PluginImpl.getInstance().triggerEvent(secondEvent);
   RunList<FreeStyleBuild> builds = waitForBuilds(project, 2, 5000);
   assertEquals(2, builds.size());
   assertSame(Result.SUCCESS, firstBuild.getResult());
   assertSame(Result.SUCCESS, builds.getFirstBuild().getResult());
   assertSame(Result.SUCCESS, builds.getLastBuild().getResult());
 }
  /**
   * Get the front end url from a ChangeBasedEvent.
   *
   * @param event the event
   * @param project the project for which the parameters are being set
   * @return the front end url
   */
  private static String getURL(ChangeBasedEvent event, AbstractProject project) {
    String url = "";
    String serverName = null;
    // Figure out what serverName to use
    if (event.getProvider() != null) {
      serverName = event.getProvider().getName();
    } else if (project != null) {
      String name = GerritTrigger.getTrigger(project).getServerName();
      if (!GerritServer.ANY_SERVER.equals(name)) {
        serverName = name;
      }
    }
    if (serverName == null && PluginImpl.getInstance().getFirstServer() != null) {
      logger.warn(
          "No server could be determined from event or project config, "
              + "defaulting to the first configured server. Event: [{}] Project: [{}]",
          event,
          project);
      serverName = PluginImpl.getInstance().getFirstServer().getName();
    } else if (serverName == null) {
      // We have exhausted all possibilities, time to fail horribly
      throw new IllegalStateException(
          "Cannot determine a Gerrit server to link to. Have you configured one?");
    }

    GerritServer server = PluginImpl.getInstance().getServer(serverName);
    if (server != null) {
      IGerritHudsonTriggerConfig config = server.getConfig();
      if (config != null) {
        url = config.getGerritFrontEndUrlFor(event);
      } else {
        logger.error("Could not find config for Gerrit server {}", serverName);
      }
    } else {
      logger.error("Could not find Gerrit server {}", serverName);
    }
    return url;
  }
 /**
  * Checks the size of the listeners collection retrieved by {@link #getGerritEventListeners()}.
  *
  * @param extra number of added listeners, other than the default.
  */
 void assertNrOfEventListeners(int extra) {
   Collection<GerritEventListener> gerritEventListeners = getGerritEventListeners();
   int nbrOfListeners = extra;
   nbrOfListeners++; // EventListener adds 1 listener
   nbrOfListeners++; // DependencyQueueTaskDispatcher adds 1 listener
   nbrOfListeners++; // ReplicationQueueTaskDispatcher adds 1 listener
   GerritServer server = PluginImpl.getServer_(PluginImpl.DEFAULT_SERVER_NAME);
   if (server.isConnected()
       && server.getConfig().isEnableProjectAutoCompletion()
       && server.isProjectCreatedEventsSupported()) {
     nbrOfListeners++; // GerritProjectListUpdater adds 1 listener//
   }
   assertEquals(nbrOfListeners, gerritEventListeners.size());
 }