Skip to content

Commit 6d3783c

Browse files
author
Takafumi Ikeda
authored
Merge pull request #119 from github/java-deployment-sample
Implemented a deployment sample by Java
2 parents cdbe135 + 7a24586 commit 6d3783c

File tree

6 files changed

+378
-0
lines changed

6 files changed

+378
-0
lines changed

api/java/deployment/.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.class
2+
.settings
3+
.project
4+
.classpath
5+
target/
6+
.idea
7+
*.iml
8+
9+
# Mobile Tools for Java (J2ME)
10+
.mtj.tmp/
11+
12+
# Package Files #
13+
*.jar
14+
*.war
15+
*.ear
16+
17+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
18+
hs_err_pid*

api/java/deployment/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# DeployServer
2+
3+
A sample implementation for using GitHub Deployment API.
4+
5+
Ported [this](https://developer.github.com/guides/delivering-deployments/) by Java. Powered by [Spark](http://sparkjava.com/).
6+
7+
## Prerequisite
8+
- JDK8
9+
- Maven3
10+
- GitHub OAuth Token
11+
12+
## Getting Started
13+
First, you should set your OAuth token into an environment variable somewhere, like:
14+
```
15+
export GITHUB_OAUTH=xxxxxxx
16+
```
17+
18+
After that, you can:
19+
20+
- For development
21+
22+
```
23+
$ mvn compile exec:java
24+
```
25+
26+
If you aren't familiar with CLI, you can just run the main class via an execution button in your IDE as well.
27+
28+
- For deployment
29+
30+
```
31+
$ mvn clean package
32+
$ java -jar target/DeployServer-{version}.jar
33+
```
34+
35+
Then you can see it works on `http://localhost:4567`.
36+
37+
After you make sure this sever deployed a place where GitHub can reach out to, you can test how it interacts with GitHub via its Deployment API.
38+
39+
You can also place it on your local pc, then expose it by using ngrok. Please refer the direction described [here](https://developer.github.com/guides/delivering-deployments/).
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.github</groupId>
5+
<artifactId>DeployServer</artifactId>
6+
<name>DeployServer</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<url>https://github.com/github/platform-samples</url>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.1</version>
14+
<configuration>
15+
<source>${java.version}</source>
16+
<target>${java.version}</target>
17+
</configuration>
18+
</plugin>
19+
<plugin>
20+
<artifactId>maven-shade-plugin</artifactId>
21+
<version>2.3</version>
22+
<executions>
23+
<execution>
24+
<phase>package</phase>
25+
<goals>
26+
<goal>shade</goal>
27+
</goals>
28+
<configuration>
29+
<transformers>
30+
<transformer>
31+
<mainClass>${main.class}</mainClass>
32+
</transformer>
33+
</transformers>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.2.1</version>
42+
<executions>
43+
<execution>
44+
<goals>
45+
<goal>java</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
<configuration>
50+
<mainClass>${main.class}</mainClass>
51+
<arguments />
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
<dependencies>
57+
<dependency>
58+
<groupId>junit</groupId>
59+
<artifactId>junit</artifactId>
60+
<version>4.11</version>
61+
<scope>test</scope>
62+
<exclusions>
63+
<exclusion>
64+
<artifactId>hamcrest-core</artifactId>
65+
<groupId>org.hamcrest</groupId>
66+
</exclusion>
67+
</exclusions>
68+
</dependency>
69+
</dependencies>
70+
<properties>
71+
<main.class>com.github.DeployServer</main.class>
72+
<java.version>1.8</java.version>
73+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
74+
</properties>
75+
</project>
76+

api/java/deployment/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.github</groupId>
6+
<artifactId>DeployServer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>DeployServer</name>
11+
<url>https://github.com/github/platform-samples</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
<main.class>com.github.DeployServer</main.class>
17+
</properties>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<source>${java.version}</source>
27+
<target>${java.version}</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-shade-plugin</artifactId>
33+
<version>2.3</version>
34+
<executions>
35+
<!-- Run shade goal on package phase -->
36+
<execution>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
<configuration>
42+
<transformers>
43+
<!-- add Main-Class to manifest file -->
44+
<transformer
45+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
46+
<mainClass>${main.class}</mainClass>
47+
</transformer>
48+
</transformers>
49+
</configuration>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
<plugin>
54+
<groupId>org.codehaus.mojo</groupId>
55+
<artifactId>exec-maven-plugin</artifactId>
56+
<version>1.2.1</version>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>java</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
<configuration>
65+
<mainClass>${main.class}</mainClass>
66+
<arguments></arguments>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
<dependencies>
73+
<dependency>
74+
<groupId>junit</groupId>
75+
<artifactId>junit</artifactId>
76+
<version>4.11</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.sparkjava</groupId>
81+
<artifactId>spark-core</artifactId>
82+
<version>2.3</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.google.code.gson</groupId>
86+
<artifactId>gson</artifactId>
87+
<version>2.3.1</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.kohsuke</groupId>
91+
<artifactId>github-api</artifactId>
92+
<version>1.75</version>
93+
</dependency>
94+
</dependencies>
95+
</project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.github;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import org.kohsuke.github.*;
7+
8+
import java.io.IOException;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.kohsuke.github.GHDeploymentState.PENDING;
13+
import static org.kohsuke.github.GHDeploymentState.SUCCESS;
14+
import static spark.Spark.*;
15+
16+
/**
17+
* Hello world!
18+
*/
19+
public class DeployServer {
20+
public static void main(String[] args) {
21+
22+
get("/", (req, res) -> "Deploy Server");
23+
24+
get("/hello", (req, res) -> "Hello World");
25+
26+
post("/event_handler", (req, res) -> {
27+
String payload = req.body();
28+
String x_github_event = req.headers("X-GITHUB-EVENT");
29+
30+
Gson gson = new Gson();
31+
JsonObject jsonObject = gson.fromJson(payload, JsonElement.class).getAsJsonObject();
32+
33+
switch (x_github_event) {
34+
case "pull_request":
35+
if ("closed".equalsIgnoreCase(jsonObject.get("action").getAsString()) &&
36+
jsonObject.get("pull_request").getAsJsonObject().get("merged").getAsBoolean()) {
37+
38+
System.out.println("A pull request was merged! A deployment should start now...");
39+
40+
start_deployment(jsonObject.get("pull_request").getAsJsonObject());
41+
}
42+
break;
43+
case "deployment":
44+
process_deployment(jsonObject);
45+
break;
46+
case "deployment_status":
47+
update_deployment_status(jsonObject);
48+
break;
49+
}
50+
51+
return "Well Done!!!!!";
52+
});
53+
}
54+
55+
56+
private static void start_deployment(JsonObject jsonObject) {
57+
String user = jsonObject.get("user").getAsJsonObject().get("login").getAsString();
58+
Map<String, String> map = new HashMap<>();
59+
map.put("environment", "QA");
60+
map.put("deploy_user", user);
61+
Gson gson = new Gson();
62+
String payload = gson.toJson(map);
63+
64+
try {
65+
GitHub gitHub = GitHubBuilder.fromEnvironment().build();
66+
GHRepository repository = gitHub.getRepository(
67+
jsonObject.get("head").getAsJsonObject()
68+
.get("repo").getAsJsonObject()
69+
.get("full_name").getAsString());
70+
GHDeployment deployment =
71+
new GHDeploymentBuilder(
72+
repository,
73+
jsonObject.get("head").getAsJsonObject().get("sha").getAsString()
74+
).description("Auto Deploy after merge").payload(payload).autoMerge(false).create();
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
private static void process_deployment(JsonObject jsonObject) {
81+
String payload_str = jsonObject.get("deployment").getAsJsonObject().get("payload").getAsString();
82+
Map payload = new Gson().fromJson(payload_str, Map.class);
83+
84+
System.out.println("Processing " + jsonObject.get("deployment").getAsJsonObject().get("description").getAsString() +
85+
" for " + payload.<String>get("deploy_user") + " to " + payload.<String>get("environment"));
86+
87+
try {
88+
Thread.sleep(2000L);
89+
GitHub gitHub = GitHubBuilder.fromEnvironment().build();
90+
GHRepository repository = gitHub.getRepository(
91+
jsonObject.get("repository").getAsJsonObject()
92+
.get("full_name").getAsString());
93+
GHDeploymentStatus deploymentStatus = new GHDeploymentStatusBuilder(repository,
94+
jsonObject.get("deployment").getAsJsonObject().get("id").getAsInt(), PENDING).create();
95+
Thread.sleep(5000L);
96+
97+
GHDeploymentStatus deploymentStatus2 = new GHDeploymentStatusBuilder(repository,
98+
jsonObject.get("deployment").getAsJsonObject().get("id").getAsInt(), SUCCESS).create();
99+
} catch (IOException e) {
100+
e.printStackTrace();
101+
} catch (InterruptedException e) {
102+
e.printStackTrace();
103+
}
104+
105+
}
106+
107+
108+
private static void update_deployment_status(JsonObject jsonObject) {
109+
System.out.println("Deployment status for " + jsonObject.get("deployment").getAsJsonObject().get("id").getAsString() +
110+
" is " + jsonObject.get("deployment_status").getAsJsonObject().get("state").getAsString());
111+
}
112+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple DeployServer.
9+
*/
10+
public class DeployServerTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public DeployServerTest(String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( DeployServerTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)