Java Code Examples for hudson.model.FreeStyleProject#addTrigger()

The following examples show how to use hudson.model.FreeStyleProject#addTrigger() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PushBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void build() throws IOException {
    try {
        FreeStyleProject testProject = jenkins.createFreeStyleProject();
        when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
        testProject.addTrigger(trigger);

        exception.expect(HttpResponses.HttpResponseException.class);
        new PushBuildAction(testProject, getJson("PushEvent.json"), null).execute(response);
    } finally {
        ArgumentCaptor<PushHook> pushHookArgumentCaptor = ArgumentCaptor.forClass(PushHook.class);
        verify(trigger).onPost(pushHookArgumentCaptor.capture());
        assertThat(pushHookArgumentCaptor.getValue().getProject(), is(notNullValue()));
        assertThat(pushHookArgumentCaptor.getValue().getProject().getWebUrl(), is(notNullValue()));
        assertThat(pushHookArgumentCaptor.getValue().getUserUsername(), is(notNullValue()));
        assertThat(pushHookArgumentCaptor.getValue().getUserUsername(), containsString("jsmith"));
    }
}
 
Example 2
Source File: GitLabConnectionConfigTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void authenticationEnabled_anonymous_forbidden() throws IOException {
    Boolean defaultValue = jenkins.get(GitLabConnectionConfig.class).isUseAuthenticatedEndpoint();
    assertTrue(defaultValue);
    jenkins.getInstance().setAuthorizationStrategy(new GlobalMatrixAuthorizationStrategy());
    URL jenkinsURL = jenkins.getURL();
    FreeStyleProject project = jenkins.createFreeStyleProject("test");
    GitLabPushTrigger trigger = mock(GitLabPushTrigger.class);
    project.addTrigger(trigger);

    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(jenkinsURL.toExternalForm() + "project/test");
    request.addHeader("X-Gitlab-Event", "Push Hook");
    request.setEntity(new StringEntity("{}"));

    CloseableHttpResponse response = client.execute(request);

    assertThat(response.getStatusLine().getStatusCode(), is(403));
}
 
Example 3
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Test
public void dontFailOnBadJob() throws IOException, ANTLRException {
    String goodRepo = "https://github.com/KostyaSha-auto/test-repo";

    final FreeStyleProject job1 = jenkins.createProject(FreeStyleProject.class, "bad job");
    job1.addProperty(new GithubProjectProperty("http://bad.url/deep/bad/path/"));
    job1.addTrigger(new GitHubPRTrigger("", GitHubPRTriggerMode.HEAVY_HOOKS_CRON, emptyList()));

    Set<Job> jobs = getPRTriggerJobs(goodRepo);
    MatcherAssert.assertThat(jobs, hasSize(0));

    final FreeStyleProject job2 = jenkins.createProject(FreeStyleProject.class, "good job");
    job2.addProperty(new GithubProjectProperty(goodRepo));
    job2.addTrigger(new GitHubPRTrigger("", GitHubPRTriggerMode.HEAVY_HOOKS_CRON, emptyList()));

    jobs = getPRTriggerJobs("KostyaSha-auto/test-repo");
    MatcherAssert.assertThat(jobs, hasSize(1));
    MatcherAssert.assertThat(jobs, hasItems(job2));
}
 
Example 4
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Test
public void shouldTriggerJobOnPullRequestOpen() throws Exception {
    when(trigger.getRepoFullName(any(AbstractProject.class))).thenReturn(create(REPO_URL_FROM_PAYLOAD));
    when(trigger.getTriggerMode()).thenReturn(GitHubPRTriggerMode.HEAVY_HOOKS);

    FreeStyleProject job = jenkins.createFreeStyleProject();
    job.addProperty(new GithubProjectProperty(REPO_URL_FROM_PAYLOAD));
    job.addTrigger(trigger);

    new GHPullRequestSubscriber().onEvent(new GHSubscriberEvent(
            "",
            GHEvent.PULL_REQUEST,
            classpath("payload/pull_request.json"))
    );

    verify(trigger).queueRun(eq(job), eq(1));
}
 
Example 5
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Test
public void shouldTriggerFreestyleProjectOnIssueComment() throws Exception {
    when(trigger.getRepoFullName(any(Job.class))).thenReturn(create(REPO_URL_FROM_PAYLOAD));
    when(trigger.getTriggerMode()).thenReturn(GitHubPRTriggerMode.HEAVY_HOOKS);

    FreeStyleProject job = jenkins.createFreeStyleProject();
    job.addProperty(new GithubProjectProperty(REPO_URL_FROM_PAYLOAD));
    job.addTrigger(trigger);

    new GHPullRequestSubscriber().onEvent(new GHSubscriberEvent(
            "",
            GHEvent.ISSUE_COMMENT,
            classpath("payload/issue_comment.json"))
    );

    verify(trigger).queueRun(eq(job), eq(1));
}
 
Example 6
Source File: GHBranchSubscriberTest.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Test
public void dontFailOnBadJob() throws IOException, ANTLRException {
    String goodRepo = "https://github.com/KostyaSha-auto/test-repo";

    final FreeStyleProject job1 = jRule.createProject(FreeStyleProject.class, "bad job");
    job1.addProperty(new GithubProjectProperty("http://bad.url/deep/bad/path/"));
    job1.addTrigger(new GitHubBranchTrigger("", GitHubPRTriggerMode.HEAVY_HOOKS_CRON, emptyList()));

    Set<Job> jobs = getBranchTriggerJobs(goodRepo);
    assertThat(jobs, hasSize(0));

    final FreeStyleProject job2 = jRule.createProject(FreeStyleProject.class, "good job");
    job2.addProperty(new GithubProjectProperty(goodRepo));
    job2.addTrigger(new GitHubBranchTrigger("", GitHubPRTriggerMode.HEAVY_HOOKS_CRON, emptyList()));

    jobs = getBranchTriggerJobs("KostyaSha-auto/test-repo");
    assertThat(jobs, hasSize(1));
    assertThat(jobs, hasItems(job2));
}
 
Example 7
Source File: PushBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void skip_missingRepositoryUrl() throws IOException {
    FreeStyleProject testProject = jenkins.createFreeStyleProject();
    testProject.addTrigger(trigger);

    new PushBuildAction(testProject, getJson("PushEvent_missingRepositoryUrl.json"), null).execute(response);

    verify(trigger, never()).onPost(any(PushHook.class));
}
 
Example 8
Source File: NoteBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void build_alreadyBuiltMR_differentTargetBranch() throws IOException, ExecutionException, InterruptedException {
    FreeStyleProject testProject = jenkins.createFreeStyleProject();
    testProject.addTrigger(trigger);
    testProject.setScm(new GitSCM(gitRepoUrl));
    QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new GitLabWebHookCause(causeData()
            .withActionType(CauseData.ActionType.NOTE)
            .withSourceProjectId(1)
            .withTargetProjectId(1)
            .withBranch("feature")
            .withSourceBranch("feature")
            .withUserName("")
            .withSourceRepoHomepage("https://gitlab.org/test")
            .withSourceRepoName("test")
            .withSourceNamespace("test-namespace")
            .withSourceRepoUrl("[email protected]:test.git")
            .withSourceRepoSshUrl("[email protected]:test.git")
            .withSourceRepoHttpUrl("https://gitlab.org/test.git")
            .withMergeRequestTitle("Test")
            .withMergeRequestId(1)
            .withMergeRequestIid(1)
            .withTargetBranch("master")
            .withTargetRepoName("test")
            .withTargetNamespace("test-namespace")
            .withTargetRepoSshUrl("[email protected]:test.git")
            .withTargetRepoHttpUrl("https://gitlab.org/test.git")
            .withTriggeredByUser("test")
            .withLastCommit("123")
            .withTargetProjectUrl("https://gitlab.org/test")
            .build()));
    future.get();

    exception.expect(HttpResponses.HttpResponseException.class);
    new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);

    verify(trigger).onPost(any(NoteHook.class));
}
 
Example 9
Source File: NoteBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void build_alreadyBuiltMR_alreadyBuiltMR() throws IOException, ExecutionException, InterruptedException {
    FreeStyleProject testProject = jenkins.createFreeStyleProject();
    testProject.addTrigger(trigger);
    testProject.setScm(new GitSCM(gitRepoUrl));
    QueueTaskFuture<?> future = testProject.scheduleBuild2(0, new ParametersAction(new StringParameterValue("gitlabTargetBranch", "master")));
    future.get();

    exception.expect(HttpResponses.HttpResponseException.class);
    new NoteBuildAction(testProject, getJson("NoteEvent_alreadyBuiltMR.json"), null).execute(response);

    verify(trigger).onPost(any(NoteHook.class));
}
 
Example 10
Source File: NoteBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void build() throws IOException {
    FreeStyleProject testProject = jenkins.createFreeStyleProject();
    testProject.addTrigger(trigger);

    exception.expect(HttpResponses.HttpResponseException.class);
    new NoteBuildAction(testProject, getJson("NoteEvent.json"), null).execute(response);

    verify(trigger).onPost(any(NoteHook.class));
}
 
Example 11
Source File: PushBuildActionTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void invalidToken() throws IOException {
    FreeStyleProject testProject = jenkins.createFreeStyleProject();
    when(trigger.getTriggerOpenMergeRequestOnPush()).thenReturn(TriggerOpenMergeRequest.never);
    when(trigger.getSecretToken()).thenReturn("secret");
    testProject.addTrigger(trigger);

    exception.expect(HttpResponses.HttpResponseException.class);
    new PushBuildAction(testProject, getJson("PushEvent.json"), "wrong-secret").execute(response);

    verify(trigger, never()).onPost(any(PushHook.class));
}
 
Example 12
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldBeApplicableWithCronHooksTrigger() throws Exception {
    when(trigger.getTriggerMode()).thenReturn(GitHubPRTriggerMode.HEAVY_HOOKS_CRON);

    FreeStyleProject job = jenkins.createFreeStyleProject();
    job.addTrigger(trigger);

    assertThat("only for jobs with trigger with hook", new GHPullRequestSubscriber().isApplicable(job), is(true));
}
 
Example 13
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldBeApplicableWithHeavyHooksTrigger() throws Exception {
    when(trigger.getTriggerMode()).thenReturn(GitHubPRTriggerMode.HEAVY_HOOKS);

    FreeStyleProject job = jenkins.createFreeStyleProject();
    job.addTrigger(trigger);

    assertThat("only for jobs with trigger with hook", new GHPullRequestSubscriber().isApplicable(job), is(true));
}
 
Example 14
Source File: GHPullRequestSubscriberTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldNotBeApplicableWithCronTrigger() throws Exception {
    when(trigger.getTriggerMode()).thenReturn(GitHubPRTriggerMode.CRON);

    FreeStyleProject job = jenkins.createFreeStyleProject();
    job.addTrigger(trigger);

    assertThat("should ignore cron trigger", new GHPullRequestSubscriber().isApplicable(job), is(false));
}
 
Example 15
Source File: GitHubPRTriggerTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void checkBuildDataAbsenceAfterBuild() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("test-job");
    p.addProperty(new GithubProjectProperty("https://github.com/KostyaSha/test-repo"));
    p.addTrigger(defaultGitHubPRTrigger());
    p.getBuildersList().add(new BuildDataBuilder());

    GitHubPRCause cause = new GitHubPRCause("headSha", 1, true, "targetBranch", "srcBranch", "[email protected]",
            "title", new URL("http://www.example.com"), "repoOwner", new HashSet<String>(),
            null, false, "nice reason", "author name", "[email protected]", "open");
    FreeStyleBuild build = p.scheduleBuild2(0, cause).get();
    j.waitUntilNoActivity();

    assertTrue(build.getActions(BuildData.class).size() == 0);
}
 
Example 16
Source File: GitHubPRTriggerMockTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
/**
 * loading old local state data, running trigger and checking that old disappeared and new appeared
 */
@LocalData
@Test
public void actualiseRepo() throws Exception {
    Thread.sleep(1000);

    FreeStyleProject project = (FreeStyleProject) jRule.getInstance().getItem("project");
    assertThat(project, notNullValue());

    GitHubPRTrigger trigger = project.getTrigger(GitHubPRTrigger.class);
    assertThat(trigger, notNullValue());

    GitHubRepositoryName repoFullName = trigger.getRepoFullName();
    assertThat(repoFullName.getHost(), is("localhost"));
    assertThat(repoFullName.getUserName(), is("org"));
    assertThat(repoFullName.getRepositoryName(), is("old-repo"));

    GitHubPRRepository prRepository = project.getAction(GitHubPRRepository.class);
    assertThat(project, notNullValue());

    assertThat(prRepository.getFullName(), is("org/old-repo"));
    assertThat(prRepository.getGithubUrl(), notNullValue());
    assertThat(prRepository.getGithubUrl().toString(), is("https://localhost/org/old-repo/"));
    assertThat(prRepository.getGitUrl(), is("git://localhost/org/old-repo.git"));
    assertThat(prRepository.getSshUrl(), is("git@localhost:org/old-repo.git"));

    final Map<Integer, GitHubPRPullRequest> pulls = prRepository.getPulls();
    assertThat(pulls.size(), is(1));

    final GitHubPRPullRequest pullRequest = pulls.get(1);
    assertThat(pullRequest, notNullValue());
    assertThat(pullRequest.getNumber(), is(1));
    assertThat(pullRequest.getHeadSha(), is("65d0f7818009811e5d5eb703ebad38bbcc816b49"));
    assertThat(pullRequest.isMergeable(), is(true));
    assertThat(pullRequest.getBaseRef(), is("master"));
    assertThat(pullRequest.getHtmlUrl().toString(), is("https://localhost/org/old-repo/pull/1"));

    // now new
    project.addProperty(new GithubProjectProperty("http://localhost/org/repo"));

    GitHubPluginRepoProvider repoProvider = new GitHubPluginRepoProvider();
    repoProvider.setManageHooks(false);
    repoProvider.setRepoPermission(GHPermission.PULL);

    trigger.setRepoProvider(repoProvider);
    project.addTrigger(trigger);
    project.save();

    // activate trigger
    jRule.configRoundtrip(project);

    trigger = ghPRTriggerFromJob(project);

    trigger.doRun();

    jRule.waitUntilNoActivity();

    assertThat(project.getBuilds(), hasSize(1));

    repoFullName = trigger.getRepoFullName();
    assertThat(repoFullName.getHost(), Matchers.is("localhost"));
    assertThat(repoFullName.getUserName(), Matchers.is("org"));
    assertThat(repoFullName.getRepositoryName(), Matchers.is("repo"));


    GitHubPRPollingLogAction logAction = project.getAction(GitHubPRPollingLogAction.class);
    assertThat(logAction, notNullValue());
    assertThat(logAction.getLog(),
            containsString("Repository full name changed from 'org/old-repo' to 'org/repo'.\n"));

    assertThat(logAction.getLog(),
            containsString("Changing GitHub url from 'https://localhost/org/old-repo/' " +
                    "to 'http://localhost/org/repo'.\n"));
    assertThat(logAction.getLog(),
            containsString("Changing Git url from 'git://localhost/org/old-repo.git' " +
                    "to 'git://localhost/org/repo.git'.\n"));
    assertThat(logAction.getLog(),
            containsString("Changing SSH url from 'git@localhost:org/old-repo.git' " +
                    "to 'git@localhost:org/repo.git'.\n"));
    assertThat(logAction.getLog(),
            containsString("Local settings changed, removing PRs in repository!"));

}
 
Example 17
Source File: GitHubBranchTriggerJRuleTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
/**
 * Ensure that GitHubPRRepository can be created without remote connections.
 */
@Test
public void repositoryInitialisationWhenProviderFails() throws Exception {
    final FreeStyleProject project = jRule.createProject(FreeStyleProject.class, "project");

    project.addProperty(new GithubProjectProperty("https://github.com/KostyaSha/test-repo/"));

    final GitHubBranchTrigger trigger = new GitHubBranchTrigger("", HEAVY_HOOKS, emptyList());
    trigger.setRepoProvider(new FailingGitHubRepoProvider());
    project.addTrigger(trigger);

    project.save();

    final FreeStyleProject freeStyleProject = (FreeStyleProject) jRule.getInstance().getItem("project");

    final GitHubBranchRepository repository = freeStyleProject.getAction(GitHubBranchRepository.class);

    assertThat(repository, notNullValue());
}
 
Example 18
Source File: GitHubBranchTriggerTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
@LocalData
@Test
public void someTest() throws Exception {
    FreeStyleProject prj = jRule.createFreeStyleProject("project");
    prj.addProperty(new GithubProjectProperty("http://localhost/org/repo"));

    final List<GitHubBranchEvent> events = new ArrayList<>();
    events.add(new GitHubBranchCreatedEvent());
    events.add(new GitHubBranchHashChangedEvent());

    final GitHubBranchTrigger trigger = new GitHubBranchTrigger("", CRON, events);
    prj.addTrigger(trigger);
    prj.save();
    // activate trigger
    jRule.configRoundtrip(prj);

    final GitHubBranchTrigger branchTrigger = prj.getTrigger(GitHubBranchTrigger.class);

    assertThat(branchTrigger.getRemoteRepository(), notNullValue());

    GitHubBranchRepository localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo, notNullValue());
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), hasKey("for-removal"));
    assertThat(localRepo.getBranches(), hasKey("should-change"));
    GitHubBranch shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffbb"));

    // only single branch should change in local repo
    branchTrigger.doRun("should-change");
    jRule.waitUntilNoActivity();

    assertThat(prj.getBuilds(), hasSize(1));
    FreeStyleBuild lastBuild = prj.getLastBuild();

    GitHubBranchCause cause = lastBuild.getCause(GitHubBranchCause.class);
    assertThat(cause, notNullValue());
    assertThat(cause.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));
    assertThat(cause.getBranchName(), is("should-change"));

    localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo, notNullValue());
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), hasKey("for-removal"));
    assertThat(localRepo.getBranches(), hasKey("should-change"));
    shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));


    // and now full trigger run()
    branchTrigger.doRun();

    jRule.waitUntilNoActivity();

    assertThat(prj.getBuilds(), hasSize(2));
    lastBuild = prj.getLastBuild();
    assertThat(lastBuild, notNullValue());

    cause = lastBuild.getCause(GitHubBranchCause.class);
    assertThat(cause, notNullValue());
    assertThat(cause.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));
    assertThat(cause.getBranchName(), is("new-branch"));

    localRepo = prj.getAction(GitHubBranchRepository.class);
    assertThat(localRepo.getBranches().size(), is(2));
    assertThat(localRepo.getBranches(), not(hasKey("for-removal")));
    assertThat(localRepo.getBranches(), hasKey("should-change"));

    shouldChange = localRepo.getBranches().get("should-change");
    assertThat(shouldChange.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193ffgg"));

    assertThat(localRepo.getBranches(), hasKey("new-branch"));
    GitHubBranch branch = localRepo.getBranches().get("new-branch");
    assertThat(branch.getCommitSha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));

}
 
Example 19
Source File: GitHubPRTriggerJRuleTest.java    From github-integration-plugin with MIT License 4 votes vote down vote up
/**
 * Ensure that GitHubPRRepository can be created without remote connections.
 */
@Test
public void repositoryInitialisationWhenProviderFails() throws Exception {
    final FreeStyleProject project = jRule.createProject(FreeStyleProject.class, "project");

    project.addProperty(new GithubProjectProperty("https://github.com/KostyaSha/test-repo/"));

    final GitHubPRTrigger prTrigger = new GitHubPRTrigger("", HEAVY_HOOKS, emptyList());
    prTrigger.setRepoProvider(new GitHubBranchTriggerJRuleTest.FailingGitHubRepoProvider());
    project.addTrigger(prTrigger);

    project.save();

    final FreeStyleProject freeStyleProject = (FreeStyleProject) jRule.getInstance().getItem("project");

    final GitHubPRRepository repository = freeStyleProject.getAction(GitHubPRRepository.class);

    assertThat(repository, notNullValue());
}
 
Example 20
Source File: GitHubBranchTriggerITest.java    From github-integration-plugin with MIT License 3 votes vote down vote up
@Test
public void freestyleTest() throws Exception {
    // create job
    FreeStyleProject job = jRule.createFreeStyleProject("freestyle-job");

    job.addProperty(getPreconfiguredProperty(ghRule.getGhRepo()));

    job.addTrigger(getDefaultBranchTrigger());

    job.getBuildersList().add(new Shell("env && sleep 2"));

    job.save();

    super.smokeTest(job);
}