Skip to content

Commit 56d29be

Browse files
committed
Lithium 2.1.1
Cryptonite 1.1-snapshot
1 parent 68cc4ee commit 56d29be

File tree

9 files changed

+73
-40
lines changed

9 files changed

+73
-40
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ cache:
77
services:
88
- docker
99
after_success:
10-
- if [[ "$TRAVIS_BRANCH" == "master" ]]; then docker login -u $DOCKER_USERNAME -p
11-
$DOCKER_PASSWORD ; docker build -t dejankovacevic/github-bot:latest
12-
. ; docker push dejankovacevic/github-bot ; fi
10+
- if [[ "$TRAVIS_BRANCH" == "master" ]]; then
11+
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD ;
12+
docker build -t dejankovacevic/github-bot:latest . ;
13+
docker push dejankovacevic/github-bot ;
14+
fi
15+
1316
before_install:
1417
- openssl aes-256-cbc -K $encrypted_941c9d24308c_key -iv $encrypted_941c9d24308c_iv
1518
-in client-secret.json.enc -out client-secret.json -d

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM dejankovacevic/bots.runtime:latest
22

33
COPY target/github.jar /opt/github/github.jar
4-
COPY conf/github.yaml /etc/github/github.yaml
4+
COPY github.yaml /etc/github/github.yaml
55

66
WORKDIR /opt/github
77

conf/github.yaml renamed to github.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ server:
77
appenders: []
88

99
auth: "your_token"
10-
cryptoDir: /var/lib/github/crypto
10+
data: ./data

pom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<artifactId>github-bot</artifactId>
88
<groupId>com.wire.bots</groupId>
9-
<version>0.2.0</version>
9+
<version>0.3.0</version>
1010

1111
<repositories>
1212
<repository>
@@ -16,6 +16,13 @@
1616
<enabled>true</enabled>
1717
</releases>
1818
</repository>
19+
<repository>
20+
<id>cryptonite</id>
21+
<url>https://packagecloud.io/dkovacevic/cryptonite/maven2</url>
22+
<releases>
23+
<enabled>true</enabled>
24+
</releases>
25+
</repository>
1926
</repositories>
2027

2128
<profiles>
@@ -62,7 +69,12 @@
6269
<dependency>
6370
<groupId>com.wire.bots</groupId>
6471
<artifactId>lithium</artifactId>
65-
<version>1.1.3</version>
72+
<version>2.1.1</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.wire.bots</groupId>
76+
<artifactId>cryptonite</artifactId>
77+
<version>1.1-SNAPSHOT</version>
6678
</dependency>
6779
<dependency>
6880
<groupId>com.github.spullara.mustache.java</groupId>

src/main/java/com/wire/bots/github/BotService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818

1919
package com.wire.bots.github;
2020

21+
import com.wire.bots.cryptonite.CryptoService;
22+
import com.wire.bots.cryptonite.StorageService;
23+
import com.wire.bots.cryptonite.client.CryptoClient;
24+
import com.wire.bots.cryptonite.client.StorageClient;
2125
import com.wire.bots.github.resource.GitHubResource;
2226
import com.wire.bots.sdk.MessageHandlerBase;
2327
import com.wire.bots.sdk.Server;
28+
import com.wire.bots.sdk.factories.CryptoFactory;
29+
import com.wire.bots.sdk.factories.StorageFactory;
2430
import io.dropwizard.setup.Environment;
2531

2632
public class BotService extends Server<BotConfig> {
@@ -30,12 +36,22 @@ public static void main(String[] args) throws Exception {
3036

3137
@Override
3238
protected MessageHandlerBase createHandler(BotConfig config, Environment env) {
33-
return new MessageHandler(config);
39+
return new MessageHandler(getStorageFactory(config));
3440
}
3541

3642
@Override
3743
protected void onRun(BotConfig botConfig, Environment env) {
38-
Validator validator = new Validator(config.getCryptoDir());
44+
Validator validator = new Validator(config.data);
3945
addResource(new GitHubResource(repo, validator), env);
4046
}
47+
48+
@Override
49+
protected StorageFactory getStorageFactory(BotConfig config) {
50+
return botId -> new StorageService("github", botId, new StorageClient(config.data));
51+
}
52+
53+
@Override
54+
protected CryptoFactory getCryptoFactory(BotConfig config) {
55+
return (botId) -> new CryptoService(botId, new CryptoClient(config.data));
56+
}
4157
}

src/main/java/com/wire/bots/github/MessageHandler.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919
package com.wire.bots.github;
2020

2121
import com.wire.bots.github.utils.SessionIdentifierGenerator;
22-
import com.wire.bots.sdk.Logger;
2322
import com.wire.bots.sdk.MessageHandlerBase;
24-
import com.wire.bots.sdk.Util;
2523
import com.wire.bots.sdk.WireClient;
24+
import com.wire.bots.sdk.factories.StorageFactory;
2625
import com.wire.bots.sdk.models.TextMessage;
2726
import com.wire.bots.sdk.server.model.Member;
2827
import com.wire.bots.sdk.server.model.NewBot;
2928
import com.wire.bots.sdk.server.model.User;
29+
import com.wire.bots.sdk.tools.Logger;
30+
import com.wire.bots.sdk.tools.Util;
3031

3132
import javax.annotation.Nullable;
32-
import java.io.File;
33-
import java.io.IOException;
3433
import java.net.URLEncoder;
3534
import java.util.Collection;
3635
import java.util.Collections;
3736
import java.util.concurrent.TimeUnit;
3837

3938
public class MessageHandler extends MessageHandlerBase {
40-
private final BotConfig config;
39+
private static final String SECRET_FILE = "secret";
4140
private final SessionIdentifierGenerator sesGen = new SessionIdentifierGenerator();
41+
private final StorageFactory storageFactory;
4242

43-
MessageHandler(BotConfig config) {
44-
this.config = config;
43+
MessageHandler(StorageFactory storageFactory) {
44+
this.storageFactory = storageFactory;
4545
}
4646

4747
@Override
@@ -65,7 +65,7 @@ public boolean onNewBot(NewBot newBot) {
6565
public void onNewConversation(WireClient client) {
6666
try {
6767
String secret = sesGen.next(6);
68-
Util.writeLine(secret, new File(String.format("%s/%s/secret", config.getCryptoDir(), client.getId())));
68+
storageFactory.create(client.getId()).saveFile(SECRET_FILE, secret);
6969

7070
String help = formatHelp(client);
7171
client.sendText(help, TimeUnit.MINUTES.toMillis(15));
@@ -89,21 +89,21 @@ public void onText(WireClient client, TextMessage msg) {
8989
}
9090
}
9191

92-
private String formatHelp(WireClient client) throws IOException {
92+
private String formatHelp(WireClient client) throws Exception {
9393
String botId = client.getId();
9494
String host = getHost();
95-
String secret = Util.readLine(new File(String.format("%s/%s/secret", config.getCryptoDir(), botId)));
95+
String secret = storageFactory.create(botId).readFile(SECRET_FILE);
9696
String name = client.getConversation().name;
9797
String convName = name != null ? URLEncoder.encode(name, "UTF-8") : "";
98-
String owner = getOwner(client, botId);
98+
String owner = getOwner(client);
9999

100100
String url = String.format("https://%s/%s#conv=%s,owner=@%s", host, botId, convName, owner);
101101
return formatHelp(url, secret);
102102
}
103103

104104
private String formatHelp(String url, String secret) {
105105
return String.format("Hi, I'm GitHub bot. Here is how to set me up:\n\n"
106-
+ "1. Go to the repository that you want to connect to\n"
106+
+ "1. Go to the repository that you would like to connect to\n"
107107
+ "2. Go to **Settings / Webhooks / Add webhook**\n"
108108
+ "3. Add **Payload URL**: %s\n"
109109
+ "4. Set **Content-Type**: application/json\n"
@@ -117,12 +117,10 @@ private String getHost() {
117117
}
118118

119119
@Nullable
120-
private String getOwner(WireClient client, String botId) throws IOException {
121-
File originFile = new File(String.format("%s/%s/origin.id", config.getCryptoDir(), botId));
122-
if (!originFile.exists())
123-
return null;
124-
String origin = Util.readLine(originFile);
125-
Collection<User> users = client.getUsers(Collections.singletonList(origin));
120+
private String getOwner(WireClient client) throws Exception {
121+
String botId = client.getId();
122+
NewBot state = storageFactory.create(botId).getState();
123+
Collection<User> users = client.getUsers(Collections.singletonList(state.origin.id));
126124
for (User user : users)
127125
return user.handle;
128126
return null;

src/main/java/com/wire/bots/github/Validator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.wire.bots.github;
22

3-
import com.wire.bots.sdk.Util;
3+
import com.wire.bots.sdk.tools.Util;
44

55
import java.io.File;
66

src/main/java/com/wire/bots/github/resource/GitHubResource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.wire.bots.github.WebHookHandler;
66
import com.wire.bots.github.model.GitResponse;
77
import com.wire.bots.sdk.ClientRepo;
8-
import com.wire.bots.sdk.Logger;
98
import com.wire.bots.sdk.WireClient;
9+
import com.wire.bots.sdk.tools.Logger;
1010

1111
import javax.ws.rs.*;
1212
import javax.ws.rs.core.MediaType;
@@ -18,12 +18,12 @@ public class GitHubResource {
1818

1919
private final ClientRepo repo;
2020
private final Validator validator;
21-
private final ObjectMapper mapper = new ObjectMapper();
22-
private final WebHookHandler webHookHandler = new WebHookHandler();
21+
private final WebHookHandler webHookHandler;
2322

2423
public GitHubResource(ClientRepo repo, Validator validator) {
2524
this.repo = repo;
2625
this.validator = validator;
26+
webHookHandler = new WebHookHandler();
2727
}
2828

2929
@POST
@@ -50,6 +50,7 @@ public Response webHook(
5050
build();
5151
}
5252

53+
ObjectMapper mapper = new ObjectMapper();
5354
GitResponse response = mapper.readValue(payload, GitResponse.class);
5455

5556
Logger.info("%s.%s\tBot: %s", event, response.action, botId);

src/tests/com/wire/bots/github/test/GitHubResourceTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//package com.wire.bots.github.test;
22
//
3-
//import com.wire.bots.github.Validator;
3+
//import com.wire.bots.github.BotConfig;
4+
//import com.wire.bots.github.BotService;
45
//import com.wire.bots.github.resource.GitHubResource;
56
//import com.wire.bots.github.test.helpers.DummyRepo;
67
//import com.wire.bots.github.test.helpers.DummyValidator;
7-
//import com.wire.bots.sdk.ClientRepo;
88
//import io.dropwizard.testing.FixtureHelpers;
9+
//import io.dropwizard.testing.junit.DropwizardAppRule;
910
//import io.dropwizard.testing.junit.ResourceTestRule;
1011
//import org.junit.ClassRule;
1112
//import org.junit.Test;
@@ -17,30 +18,32 @@
1718
//import static junit.framework.TestCase.assertEquals;
1819
//
1920
//public class GitHubResourceTest {
20-
// private static Validator validator = new DummyValidator();
21-
// private static ClientRepo repo = new DummyRepo();
21+
// @ClassRule
22+
// public static final DropwizardAppRule<BotConfig> app
23+
// = new DropwizardAppRule<>(BotService.class, "github.yaml");
2224
//
2325
// @ClassRule
2426
// public static final ResourceTestRule resources = ResourceTestRule.builder()
25-
// .addResource(new GitHubResource(repo, validator))
27+
// .addResource(new GitHubResource(new DummyRepo(), new DummyValidator()))
2628
// .build();
2729
//
2830
// @Test
2931
// public void testOnCommitCommentCreated() {
3032
// String event = "commit_comment";
3133
// String payload = FixtureHelpers.fixture("fixtures/events/" + event + ".created.json");
32-
// gitHubWebhookPost(event, payload);
34+
// gitHubWebhookPost("bot", event, payload);
3335
// }
3436
//
3537
// @Test
3638
// public void testOnPRCreated() {
3739
// String event = "pull_request";
3840
// String payload = FixtureHelpers.fixture("fixtures/events/" + event + ".created.json");
39-
// gitHubWebhookPost(event, payload);
41+
// gitHubWebhookPost("bot", event, payload);
4042
// }
4143
//
42-
// private void gitHubWebhookPost(String event, String payload) {
43-
// Response response = resources.target("/github/botId")
44+
// private void gitHubWebhookPost(String botId, String event, String payload) {
45+
// Response response = resources.target("/")
46+
// .path(botId)
4447
// .request()
4548
// .header("X-GitHub-Event", event)
4649
// .header("X-Hub-Signature", "signature")

0 commit comments

Comments
 (0)