-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathGitHubPushTriggerTestCase.java
More file actions
79 lines (63 loc) · 2.9 KB
/
GitHubPushTriggerTestCase.java
File metadata and controls
79 lines (63 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.cloudbees.jenkins;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.jenkinsci.plugins.github.GitHubPlugin;
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import hudson.model.FreeStyleProject;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
/**
* @author <a href = "mailto:achim.derigs@graudata.com">Achim Derigs</a>
*/
public final class GitHubPushTriggerTestCase {
@Rule
public final JenkinsRule jenkins = new JenkinsRule();
private static void triggerWebHook(final String repositoryUrl, final String pusherName) throws IOException {
final GitHubPluginConfig configuration = GitHubPlugin.configuration();
final URL url = configuration.getHookUrl();
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("X-GitHub-Event", "push");
connection.setDoOutput(true);
connection.connect();
final String payload = "payload={\"repository\":{\"url\":\"" + repositoryUrl + "\"},\"pusher\":{\"name\":\"" + pusherName + "\",\"email\":\"\"}}";
try (final OutputStream stream = connection.getOutputStream()) {
stream.write(payload.getBytes(UTF_8));
}
assertEquals("fails to connect to " + url, HttpURLConnection.HTTP_OK, connection.getResponseCode());
}
@Test
public void trigger() throws IOException {
final String repositoryUrl = "https://github.com/kohsuke/foo";
final SCM scm = new GitSCM(repositoryUrl);
final FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(scm);
final String expected = System.getProperty("user.name");
final GitHubPushTrigger trigger = new GitHubPushTrigger();
project.addTrigger(trigger);
triggerWebHook(repositoryUrl, expected);
final String actual = trigger.pushBy();
assertEquals("fails to be triggered by pusher: ", expected, actual);
}
@Test
public void ignore() throws IOException {
final String repositoryUrl = "https://github.com/kohsuke/foo";
final SCM scm = new GitSCM(repositoryUrl);
final FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(scm);
final String pusherName = System.getProperty("user.name");
final GitHubPushTrigger trigger = new GitHubPushTrigger(pusherName);
project.addTrigger(trigger);
triggerWebHook(repositoryUrl, pusherName);
final Object object = trigger.pushBy();
assertNull("fails to ignore pusher: ", object);
}
}