Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit 4437eda

Browse files
author
Bernhard Grünewaldt
committed
minor changes and doc
1 parent 0195696 commit 4437eda

File tree

6 files changed

+142
-24
lines changed

6 files changed

+142
-24
lines changed

DEVELOPMENT.md

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,97 @@
11
### Tests
22

33
```bash
4-
mvn hpi:run
4+
mvn compile && rm -rf work/ && mvn hpi:run
55

66
curl -X POST \
77
-H "Content-Type: application/json" \
88
-d @test-webhook-payload.json \
9-
http://localhost:8080/jenkins/plugin/github-webhook-notifier-plugin/receive
9+
http://localhost:8080/jenkins/github-webhook-notifier/receive
1010
```
1111

1212
### Build hpi
1313

1414
```
15-
mvn clean
16-
mvn compile
17-
mvn hpi:hpi
15+
mvn clean && mvn compile && mvn hpi:hpi
16+
```
17+
18+
19+
### Notes
20+
21+
Howto manually checkout certain branch at specific revision
22+
23+
```
24+
# Clone develop branch
25+
git clone --single-branch --branch develop https://github.com/codeclou/test-webhook.git .
26+
# Switch to revision
27+
git reset --hard 495140cf2b763b8bc3821e25b4c26dd1273d6206
28+
```
29+
30+
31+
### Webhook Payload Examples
32+
33+
34+
##### Tag Push Payload
35+
36+
```bash
37+
# on head of master
38+
git tag -a 0.0.1 -m "0.0.1"
39+
git push origin 0.0.1
40+
```
41+
42+
Webhook payload:
43+
44+
```json
45+
{
46+
"ref": "refs/tags/0.0.1",
47+
"before": "0000000000000000000000000000000000000000",
48+
"after": "80aa5698b8688df969d56678d5304f4450adda84",
49+
"created": true,
50+
"deleted": false,
51+
"forced": false
52+
}
53+
```
54+
55+
##### Branch Push Payload
56+
57+
```bash
58+
# on head of master
59+
git branch develop
60+
git push origin develop
61+
```
62+
63+
Webhook payload:
64+
65+
```json
66+
{
67+
"ref": "refs/heads/develop",
68+
"before": "0000000000000000000000000000000000000000",
69+
"after": "495140cf2b763b8bc3821e25b4c26dd1273d6206",
70+
"created": true,
71+
"deleted": false,
72+
"forced": false
73+
}
74+
```
75+
76+
##### Normal commit Push Payload
77+
78+
```bash
79+
# on head of master
80+
echo "foo" > newfile.txt
81+
git add . -A
82+
git commit -m "foo"
83+
git push
84+
```
85+
86+
Webhook payload:
87+
88+
```json
89+
{
90+
"ref": "refs/heads/master",
91+
"before": "495140cf2b763b8bc3821e25b4c26dd1273d6206",
92+
"after": "3be1cb4b6b86533b5dab2b0083fa9fb8b401b430",
93+
"created": false,
94+
"deleted": false,
95+
"forced": false
96+
}
1897
```

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ and push that tag, that the jenkins job will not be triggered, since the revisio
2727
### GitHub Webhook Configuration
2828

2929
* **Payload URL**
30-
* `https://user:pass@jenkins.foo/jenkins/github-webhook-notifier-plugin/receive`
30+
* `https://jenkins.foo/jenkins/github-webhook-notifier/receive`
31+
* Note: The endpoint can be called without authentication.
3132
* **Content type**
3233
* `application/json`
3334
* **Secret**
34-
* empty.
35+
* Choose a good secret, at best a random sha512 hash
3536
* **Which events ...**
3637
* Just the `push` event
3738

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.jenkins-ci.plugins</groupId>
88
<artifactId>plugin</artifactId>
9-
<version>1.580.1</version>
9+
<version>2.25</version>
1010
<relativePath/>
1111
</parent>
1212
<groupId>io.codeclou.jenkins.github.webhook.notifier.plugin</groupId>
@@ -52,5 +52,10 @@
5252
<artifactId>commons-io</artifactId>
5353
<version>2.5</version>
5454
</dependency>
55+
<dependency>
56+
<groupId>org.jenkins-ci.plugins</groupId>
57+
<artifactId>git</artifactId>
58+
<version>3.2.0</version>
59+
</dependency>
5560
</dependencies>
5661
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Licensed under MIT License
3+
* Copyright (c) 2017 Bernhard Grünewaldt
4+
*/
5+
package io.codeclou.jenkins.githubwebhooknotifierplugin;
6+
7+
import hudson.Plugin;
8+
9+
public class GithubWebhookNotifierPlugin extends Plugin {
10+
11+
}
Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Licensed under MIT License
33
* Copyright (c) 2017 Bernhard Grünewaldt
44
*/
5-
package io.codeclou.jenkins.github.webhook.notifier.plugin;
5+
package io.codeclou.jenkins.githubwebhooknotifierplugin;
66

77
import com.google.gson.Gson;
88
import com.google.gson.JsonSyntaxException;
9-
import hudson.Plugin;
9+
import hudson.Extension;
1010
import hudson.model.UnprotectedRootAction;
1111
import hudson.util.HttpResponses;
1212
import jenkins.model.Jenkins;
@@ -33,12 +33,12 @@
3333
import java.security.cert.CertificateException;
3434
import java.security.cert.X509Certificate;
3535

36-
37-
public class GithubWebhookNotifierPlugin extends Plugin implements UnprotectedRootAction {
36+
@Extension
37+
public class GithubWebhookNotifyAction implements UnprotectedRootAction {
3838

3939
@Override
4040
public String getUrlName() {
41-
return "github-webhook-notifier-plugin";
41+
return "github-webhook-notifier";
4242
}
4343

4444
@Override
@@ -48,11 +48,11 @@ public String getIconFileName() {
4848

4949
@Override
5050
public String getDisplayName() {
51-
return "github-webhook-notifier-plugin";
51+
return "github-webhook-notifier";
5252
}
5353

5454
/*
55-
* http://jenkins.foo/plugin/github-webhook-notifier-plugin/receive
55+
* http://jenkins.foo/github-webhook-notifier/receive
5656
*/
5757
@RequirePOST
5858
public HttpResponse doReceive(HttpServletRequest request, StaplerRequest staplerRequest) throws IOException, ServletException {
@@ -66,7 +66,7 @@ public HttpResponse doReceive(HttpServletRequest request, StaplerRequest stapler
6666
"git/notifyCommit?url=" +
6767
githubWebhookPayload.getRepository().getClone_url() +
6868
"&branches=" +
69-
githubWebhookPayload.getRef() +
69+
this.normalizeBranchNameForJenkins(githubWebhookPayload.getRef()) +
7070
"&sha1=" +
7171
githubWebhookPayload.getAfter();
7272
SSLContext sslContext = new SSLContextBuilder()
@@ -84,19 +84,37 @@ public boolean isTrusted(X509Certificate[] certificate, String authType) throws
8484
CloseableHttpResponse response = client.execute(httpGet);
8585
int statusCode = response.getStatusLine().getStatusCode();
8686
String gitNotificationResponse = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
87-
String responseText = "ok triggered: " + gitPluginNotifyUrl + "\nstatus: " + statusCode + "\n\n" + gitNotificationResponse;
87+
StringBuilder responseText = new StringBuilder();
88+
responseText.append("----------------------------------------------------------------------------------\n");
89+
responseText.append("github-webhook-notifier-plugin - parsed webhook payload:\n");
90+
responseText.append(" ref: ").append(githubWebhookPayload.getRef()).append("\n");
91+
responseText.append(" before: ").append(githubWebhookPayload.getBefore()).append("\n");
92+
responseText.append(" after: ").append(githubWebhookPayload.getAfter()).append("\n");
93+
responseText.append(" clone_url: ").append(githubWebhookPayload.getRepository().getClone_url()).append("\n");
94+
responseText.append("----------------------------------------------------------------------------------\n");
95+
responseText.append(">> REQUEST\n").append(gitPluginNotifyUrl).append("\n\n");
96+
responseText.append("<< RESPONSE HTTP ").append(statusCode).append("\n");
97+
responseText.append(gitNotificationResponse);
8898
if (statusCode != 200) {
89-
return HttpResponses.error(400, responseText);
99+
return HttpResponses.error(400, responseText.toString());
90100
}
91-
return HttpResponses.plainText(responseText);
101+
return HttpResponses.plainText(responseText.toString());
92102
} catch (JsonSyntaxException ex) {
93-
return HttpResponses.error(500, "json invalid");
103+
return HttpResponses.error(500, "github-webhook-notifier-plugin: github webhook json invalid");
94104
} catch (NoSuchAlgorithmException ex) {
95-
return HttpResponses.error(500, "NoSuchAlgorithmException");
105+
return HttpResponses.error(500, "github-webhook-notifier-plugin: internal error NoSuchAlgorithmException");
96106
} catch (KeyStoreException ex) {
97-
return HttpResponses.error(500, "KeyStoreException");
107+
return HttpResponses.error(500, "github-webhook-notifier-plugin: internal error KeyStoreException");
98108
} catch (KeyManagementException ex) {
99-
return HttpResponses.error(500, "KeyManagementException");
109+
return HttpResponses.error(500, "github-webhook-notifier-plugin: internal error KeyManagementException");
100110
}
101111
}
112+
113+
/*
114+
* converts "refs/heads/develop" to "origin/develop"
115+
*/
116+
private String normalizeBranchNameForJenkins(String branchname) {
117+
return branchname.replace("refs/heads/", "origin/");
118+
}
119+
102120
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
* Licensed under MIT License
33
* Copyright (c) 2017 Bernhard Grünewaldt
44
*/
5-
package io.codeclou.jenkins.github.webhook.notifier.plugin;
5+
package io.codeclou.jenkins.githubwebhooknotifierplugin;
66

7+
/**
8+
* GitHub Webhook JSON Pojo with only the parts that are interesting for us.
9+
* See: https://developer.github.com/webhooks/#payloads
10+
*/
711
public class GithubWebhookPayload {
812

913
private String ref;

0 commit comments

Comments
 (0)