Skip to content

Commit

Permalink
Test for autofavoriting
Browse files Browse the repository at this point in the history
  • Loading branch information
i386 committed Nov 12, 2016
1 parent 4c13d3b commit a117585
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-branch-source</artifactId>
<version>1.9</version>
<optional>true</optional>
<artifactId>scm-api</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<version>2.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene

Job<?, ?> job = build.getParent();
User author = first.getAuthor();
if (!User.getUnknown().equals(author) && !Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) {
Favorites.addFavorite(author, job);
logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author);

// User does not exist or is unknown
if (User.getById(author.getId(), false) == null || User.getUnknown().equals(author)) {
return;
}

// This user has previously favorited this job but has removed the favorite
if (Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) {
return;
}

Favorites.addFavorite(author, job);
logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.jenkins.blueocean.autofavorite;

import hudson.model.Result;
import hudson.model.User;
import hudson.plugins.favorite.Favorites;
import jenkins.branch.BranchSource;
import jenkins.branch.MultiBranchProject.BranchIndexing;
import jenkins.plugins.git.GitSCMSource;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class FavoritingScmListenerTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testAutoFavoriteForRegisteredUser() throws Exception {
User.getById("jdumay", true);
WorkflowJob job = createAndRunPipeline();
User user = User.getById("jdumay", false);
assertNotNull(user);
assertTrue(Favorites.isFavorite(user, job));
}

// @Test
/** Disabled because of https://issues.jenkins-ci.org/browse/JENKINS-39694 **/
public void testAutoFavoriteForNonRegisteredUser() throws Exception {
assertNull(User.getById("jdumay", false));
WorkflowJob job = createAndRunPipeline();
User user = User.getById("jdumay", false);
assertNotNull(user);
assertFalse(Favorites.isFavorite(user, job));
}

private WorkflowJob createAndRunPipeline() throws java.io.IOException, InterruptedException {
WorkflowMultiBranchProject mbp = j.createProject(WorkflowMultiBranchProject.class, "WorkflowMultiBranchProject");
mbp.getSourcesList().add(new BranchSource(new GitSCMSource(null, "https://github.com/i386/feedle.git", "", "*", "", true)));

BranchIndexing<WorkflowJob, WorkflowRun> indexing = mbp.getIndexing();
indexing.run();

while (indexing.getResult() == Result.NOT_BUILT) {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}

assertEquals(Result.SUCCESS, indexing.getResult());

WorkflowJob job = mbp.getItem("master");
while (job.getBuilds().isEmpty()) {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}

WorkflowRun run = job.getBuildByNumber(1);
assertNotNull(run);


while (run.getResult() == null) {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
return job;
}
}

0 comments on commit a117585

Please sign in to comment.