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

Commit a760dd5

Browse files
author
Bernhard Grünewaldt
committed
initial poc
1 parent 7f9d26c commit a760dd5

File tree

10 files changed

+443
-1
lines changed

10 files changed

+443
-1
lines changed

.editorconfig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
charset = utf-8
9+
10+
[*.html]
11+
indent_style = space
12+
indent_size = 4
13+
14+
[*.js]
15+
indent_style = space
16+
indent_size = 2
17+
18+
19+
[*.scss]
20+
indent_style = space
21+
indent_size = 4
22+
23+
[*.css]
24+
indent_style = space
25+
indent_size = 4
26+
27+
28+
[*.properties]
29+
charset = latin1
30+
indent_style = space
31+
indent_size = 4
32+

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.idea
2+
target
3+
bin
4+
logs
5+
*.iml
6+
.settings
7+
.classpath
8+
.project
9+
generated
10+
node_modules
11+
*.log
12+
work

DEVELOPMENT.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### Tests
2+
3+
```bash
4+
mvn hpi:run
5+
6+
curl -X POST \
7+
-H "Content-Type: application/json" \
8+
-d @test-webhook-payload.json \
9+
http://localhost:8080/jenkins/plugin/github-webhook-notifier-plugin/receive
10+
```

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 codeclou.io
3+
Copyright (c) 2017 Bernhard Grünewaldt
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# jenkins-github-webhook-notifier-plugin
22
jenkins-github-webhook-notifier-plugin
3+
4+
5+
-----
6+
7+
 
8+
9+
### License
10+
11+
[MIT](./LICENSE) © [Bernhard Grünewaldt](https://github.com/clouless)

pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.jenkins-ci.plugins</groupId>
8+
<artifactId>plugin</artifactId>
9+
<version>1.580.1</version>
10+
<relativePath/>
11+
</parent>
12+
<groupId>io.codeclou.jenkins.github.webhook.notifier.plugin</groupId>
13+
<artifactId>github-webhook-notifier-plugin</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
<packaging>hpi</packaging>
16+
17+
<name>github-webhook-notifier-plugin</name>
18+
<description>github-webhook-notifier-plugin</description>
19+
<url>https://github.com/codeclou/jenkins-github-webhook-notifier-plugin</url>
20+
<licenses>
21+
<license>
22+
<name>MIT License</name>
23+
<url>https://github.com/codeclou/jenkins-github-webhook-notifier-plugin/blob/master/LICENSE</url>
24+
</license>
25+
</licenses>
26+
<repositories>
27+
<repository>
28+
<id>repo.jenkins-ci.org</id>
29+
<url>http://repo.jenkins-ci.org/public/</url>
30+
</repository>
31+
</repositories>
32+
<pluginRepositories>
33+
<pluginRepository>
34+
<id>repo.jenkins-ci.org</id>
35+
<url>http://repo.jenkins-ci.org/public/</url>
36+
</pluginRepository>
37+
</pluginRepositories>
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.code.gson</groupId>
41+
<artifactId>gson</artifactId>
42+
<version>2.8.0</version>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed under MIT License
3+
* Copyright (c) 2017 Bernhard Grünewaldt
4+
*/
5+
package io.codeclou.jenkins.github.webhook.notifier.plugin;
6+
7+
import com.google.gson.Gson;
8+
import com.google.gson.JsonSyntaxException;
9+
import hudson.Plugin;
10+
import hudson.util.HttpResponses;
11+
import jenkins.model.Jenkins;
12+
import org.apache.commons.httpclient.HttpClient;
13+
import org.apache.commons.httpclient.HttpMethod;
14+
import org.apache.commons.httpclient.methods.GetMethod;
15+
import org.kohsuke.stapler.HttpResponse;
16+
import org.kohsuke.stapler.StaplerRequest;
17+
import org.kohsuke.stapler.interceptor.RequirePOST;
18+
19+
import javax.servlet.ServletException;
20+
import javax.servlet.http.HttpServletRequest;
21+
import java.io.BufferedReader;
22+
import java.io.IOException;
23+
24+
25+
public class GithubWebhookNotifierPlugin extends Plugin {
26+
27+
/*
28+
* http://jenkins.foo/plugin/github-webhook-notifier-plugin/receive
29+
*/
30+
@RequirePOST
31+
public HttpResponse doReceive(HttpServletRequest request, StaplerRequest staplerRequest) throws IOException, ServletException {
32+
String jeninsRootUrl = Jenkins.getInstance().getRootUrl(); // will return something like: http://localhost:8080/jenkins/
33+
BufferedReader reader = request.getReader();
34+
Gson gson = new Gson();
35+
try {
36+
GithubWebhookPayload githubWebhookPayload = gson.fromJson(reader, GithubWebhookPayload.class);
37+
String gitPluginNotifyUrl = jeninsRootUrl +
38+
"git/notifyCommit?" +
39+
githubWebhookPayload.getRepository().getClone_url() +
40+
"&branches=" +
41+
githubWebhookPayload.getRef() +
42+
"&sha1=" +
43+
githubWebhookPayload.getAfter();
44+
HttpClient client = new HttpClient();
45+
HttpMethod method = new GetMethod(gitPluginNotifyUrl);
46+
int statusCode = client.executeMethod(method);
47+
String responseText = "triggered: " + gitPluginNotifyUrl + "\nstatus: " + statusCode;
48+
if (statusCode != 200) {
49+
return HttpResponses.error(400, responseText);
50+
}
51+
return HttpResponses.plainText(responseText);
52+
} catch (JsonSyntaxException ex) {
53+
return HttpResponses.error(500, "json invalid");
54+
}
55+
}
56+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed under MIT License
3+
* Copyright (c) 2017 Bernhard Grünewaldt
4+
*/
5+
package io.codeclou.jenkins.github.webhook.notifier.plugin;
6+
7+
public class GithubWebhookPayload {
8+
9+
private String ref;
10+
private String before;
11+
private String after;
12+
private GithubWebhookPayloadRepository repository;
13+
14+
public GithubWebhookPayload() {
15+
16+
}
17+
18+
public String getRef() {
19+
return ref;
20+
}
21+
22+
public void setRef(String ref) {
23+
this.ref = ref;
24+
}
25+
26+
public String getBefore() {
27+
return before;
28+
}
29+
30+
public void setBefore(String before) {
31+
this.before = before;
32+
}
33+
34+
public String getAfter() {
35+
return after;
36+
}
37+
38+
public void setAfter(String after) {
39+
this.after = after;
40+
}
41+
42+
public GithubWebhookPayloadRepository getRepository() {
43+
return repository;
44+
}
45+
46+
public void setRepository(GithubWebhookPayloadRepository repository) {
47+
this.repository = repository;
48+
}
49+
50+
public class GithubWebhookPayloadRepository {
51+
private String clone_url;
52+
private String html_url;
53+
private String name;
54+
private String full_name;
55+
56+
public GithubWebhookPayloadRepository() {
57+
58+
}
59+
60+
public String getClone_url() {
61+
return clone_url;
62+
}
63+
64+
public void setClone_url(String clone_url) {
65+
this.clone_url = clone_url;
66+
}
67+
68+
public String getHtml_url() {
69+
return html_url;
70+
}
71+
72+
public void setHtml_url(String html_url) {
73+
this.html_url = html_url;
74+
}
75+
76+
public String getName() {
77+
return name;
78+
}
79+
80+
public void setName(String name) {
81+
this.name = name;
82+
}
83+
84+
public String getFull_name() {
85+
return full_name;
86+
}
87+
88+
public void setFull_name(String full_name) {
89+
this.full_name = full_name;
90+
}
91+
}
92+
}

src/main/resources/index.jelly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?jelly escape-by-default='true'?>
2+
<div>
3+
dummy
4+
</div>

0 commit comments

Comments
 (0)