Skip to content

Commit

Permalink
Tweet GitHub's commit comment. fixes #339 @3h
Browse files Browse the repository at this point in the history
  • Loading branch information
みぞ@CrazyBeatCoder committed Feb 25, 2018
1 parent f73a40c commit 73ab8aa
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/mizo0203/komeiji/GitHubHookHandlerServlet.java
@@ -1,6 +1,10 @@
package com.mizo0203.komeiji;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mizo0203.komeiji.domain.UseCase;
import com.mizo0203.komeiji.domain.difine.GitHubUser;
import com.mizo0203.komeiji.domain.difine.TwitterUser;
import com.mizo0203.komeiji.repo.github.data.Comment;
import com.mizo0203.komeiji.repo.github.data.CommitCommentEvent;
import org.apache.commons.io.IOUtils;

Expand All @@ -22,6 +26,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
CommitCommentEvent commitCommentEvent =
new ObjectMapper().readValue(body, CommitCommentEvent.class);
LOG.info("commitCommentEvent: " + commitCommentEvent);
GitHubUser gitHubUser = GitHubUser.getGitHubUser(commitCommentEvent.getSender().getId());
TwitterUser twitterUser =
gitHubUser == GitHubUser.MIZO0203 ? TwitterUser.MIZO0203 : TwitterUser.REDMIZO;
Comment comment = commitCommentEvent.getComment();
String commentBody = comment.getBody();
String commentBodySubString = commentBody.substring(0, Math.min(commentBody.length(), 123));
try (UseCase useCase = new UseCase()) {
useCase.updateStatus(
twitterUser, "@mizo0203 " + commentBodySubString + " " + comment.getHtml_url());
}
}

@Override
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/mizo0203/komeiji/domain/UseCase.java
@@ -1,5 +1,6 @@
package com.mizo0203.komeiji.domain;

import com.mizo0203.komeiji.domain.difine.TwitterUser;
import com.mizo0203.komeiji.repo.TwitterClient;

import java.util.logging.Logger;
Expand Down Expand Up @@ -83,4 +84,20 @@ public void listCurrentAllActivityTypeSubscriptions() {
public void deactivatesSubscriptions() {
mTwitterClient.deactivatesSubscriptions();
}

/**
* Updates the authenticating user’s current status, also known as Tweeting. For each update
* attempt, the update text is compared with the authenticating user’s recent Tweets. Any attempt
* that would result in duplication will be blocked, resulting in a 403 error. A user cannot
* submit the same status twice in a row. While not rate limited by the API, a user is limited in
* the number of Tweets they can create at a time. If the number of updates posted by the user
* reaches the current allowed limit this method will return an HTTP 403 error.
*
* @see <a
* href="https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update">POST
* statuses/update — Twitter Developers</a>
*/
public void updateStatus(TwitterUser twitterUser, String statusString) {
mTwitterClient.updateStatus(twitterUser, statusString);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/mizo0203/komeiji/domain/difine/GitHubUser.java
@@ -0,0 +1,15 @@
package com.mizo0203.komeiji.domain.difine;

public enum GitHubUser {
MIZO0203,
;

public static GitHubUser getGitHubUser(int id) {
switch (id) {
case 3683707:
return MIZO0203;
default:
return null;
}
}
}
Expand Up @@ -6,4 +6,6 @@ public class KeysAndAccessTokens {
public static final String CONSUMER_SECRET = "";
public static final String TOKEN = "";
public static final String TOKEN_SECRET = "";
public static final String REDMIZO_TOKEN = "";
public static final String REDMIZO_TOKEN_SECRET = "";
}
@@ -0,0 +1,7 @@
package com.mizo0203.komeiji.domain.difine;

public enum TwitterUser {
MIZO0203,
REDMIZO,
;
}
42 changes: 38 additions & 4 deletions src/main/java/com/mizo0203/komeiji/repo/TwitterClient.java
@@ -1,10 +1,9 @@
package com.mizo0203.komeiji.repo;

import twitter4j.HttpResponse;
import twitter4j.Twitter;
import twitter4j.Twitter4JUtil;
import twitter4j.TwitterException;
import com.mizo0203.komeiji.domain.difine.TwitterUser;
import twitter4j.*;

import javax.annotation.Nonnull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
Expand Down Expand Up @@ -162,4 +161,39 @@ public void deactivatesSubscriptions() {
e.printStackTrace();
}
}

/**
* Updates the authenticating user’s current status, also known as Tweeting. For each update
* attempt, the update text is compared with the authenticating user’s recent Tweets. Any attempt
* that would result in duplication will be blocked, resulting in a 403 error. A user cannot
* submit the same status twice in a row. While not rate limited by the API, a user is limited in
* the number of Tweets they can create at a time. If the number of updates posted by the user
* reaches the current allowed limit this method will return an HTTP 403 error.
*
* @see <a
* href="https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update">POST
* statuses/update — Twitter Developers</a>
*/
public void updateStatus(TwitterUser twitterUser, String statusString) {
Twitter twitter = getTwitter(twitterUser);
try {
Status status = twitter.updateStatus(statusString);
// LOG.log(Level.INFO, "updateStatus ret.toString(): " + ret.toString());
// LOG.log(Level.INFO, "updateStatus ret.asString(): " + ret.asString());
} catch (TwitterException e) {
e.printStackTrace();
}
}

@Nonnull
private Twitter getTwitter(TwitterUser twitterUser) {
switch (twitterUser) {
case MIZO0203:
return mTwitter4JUtil.getUserAuthentication().getTwitter();
case REDMIZO:
return mTwitter4JUtil.getRedmizoUserAuthentication().getTwitter();
default:
throw new IllegalArgumentException("twitterUser: " + twitterUser);
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/mizo0203/komeiji/repo/github/data/Comment.java
Expand Up @@ -126,4 +126,52 @@ public int hashCode() {
author_association,
body);
}

public String getUrl() {
return url;
}

public String getHtml_url() {
return html_url;
}

public int getId() {
return id;
}

public User getUser() {
return user;
}

public String getPosition() {
return position;
}

public String getLine() {
return line;
}

public String getPath() {
return path;
}

public String getCommit_id() {
return commit_id;
}

public String getCreated_at() {
return created_at;
}

public String getUpdated_at() {
return updated_at;
}

public String getAuthor_association() {
return author_association;
}

public String getBody() {
return body;
}
}
Expand Up @@ -23,6 +23,22 @@ public CommitCommentEvent(
this.sender = sender;
}

public String getAction() {
return action;
}

public Comment getComment() {
return comment;
}

public Repository getRepository() {
return repository;
}

public User getSender() {
return sender;
}

@Override
public String toString() {
return "CommitCommentEvent{"
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/mizo0203/komeiji/repo/github/data/User.java
Expand Up @@ -167,4 +167,72 @@ public int hashCode() {
type,
site_admin);
}

public String getLogin() {
return login;
}

public int getId() {
return id;
}

public String getAvatar_url() {
return avatar_url;
}

public String getGravatar_id() {
return gravatar_id;
}

public String getUrl() {
return url;
}

public String getHtml_url() {
return html_url;
}

public String getFollowers_url() {
return followers_url;
}

public String getFollowing_url() {
return following_url;
}

public String getGists_url() {
return gists_url;
}

public String getStarred_url() {
return starred_url;
}

public String getSubscriptions_url() {
return subscriptions_url;
}

public String getOrganizations_url() {
return organizations_url;
}

public String getRepos_url() {
return repos_url;
}

public String getEvents_url() {
return events_url;
}

public String getReceived_events_url() {
return received_events_url;
}

public String getType() {
return type;
}

public boolean isSite_admin() {
return site_admin;
}
}
31 changes: 27 additions & 4 deletions src/main/java/twitter4j/Twitter4JUtil.java
Expand Up @@ -7,19 +7,25 @@

public class Twitter4JUtil {
private final AuthenticationModel mUserAuthentication;
private final AuthenticationModel mRedmizoUserAuthentication;
private final AuthenticationModel mApplicationOnlyAuthentication;

public Twitter4JUtil() {
mUserAuthentication = new AuthenticationModel(createTwitterInstance());
mUserAuthentication =
new AuthenticationModel(
createTwitterInstance(KeysAndAccessTokens.TOKEN, KeysAndAccessTokens.TOKEN_SECRET));
mRedmizoUserAuthentication =
new AuthenticationModel(
createTwitterInstance(
KeysAndAccessTokens.REDMIZO_TOKEN, KeysAndAccessTokens.REDMIZO_TOKEN_SECRET));
mApplicationOnlyAuthentication =
new AuthenticationModel(createTwitterApplicationOnlyAuthInstance());
}

private static TwitterImpl createTwitterInstance() {
private static TwitterImpl createTwitterInstance(String token, String tokenSecret) {
TwitterImpl twitter = (TwitterImpl) new TwitterFactory().getInstance();
twitter.setOAuthConsumer(KeysAndAccessTokens.CONSUMER_KEY, KeysAndAccessTokens.CONSUMER_SECRET);
twitter.setOAuthAccessToken(
new AccessToken(KeysAndAccessTokens.TOKEN, KeysAndAccessTokens.TOKEN_SECRET));
twitter.setOAuthAccessToken(new AccessToken(token, tokenSecret));
return twitter;
}

Expand Down Expand Up @@ -57,6 +63,23 @@ public AuthenticationModel getUserAuthentication() {
return mUserAuthentication;
}

/**
* User authentication ユーザー認証
*
* <p>This is the most common form of resource authentication in Twitter’s OAuth 1.0a
* implementation. A signed request identifies an application’s identity in addition to the
* identity accompanying granted permissions of the end-user the application is making API calls
* on behalf of, represented by the user’s access token.
*
* <p>これは、TwitterのOAuth 1.0a実装で最も一般的なリソース認証の形式です。
* 署名付きリクエストは、アプリケーションがユーザーのアクセストークンで表されるAPI呼び出しを行うエンドユーザーの付与されたアクセス許可に伴うIDに加えて、アプリケーションのIDを識別します。
*
* @return User authentication ユーザー認証
*/
public AuthenticationModel getRedmizoUserAuthentication() {
return mRedmizoUserAuthentication;
}

/**
* Application-only authentication アプリケーションのみの認証
*
Expand Down

2 comments on commit 73ab8aa

@mizo0203
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub への commit comment をツイートする機能を追加

@mizo0203
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can not deserialize instance of java.lang.String out of START_OBJECT token
http://redmine.mizo0203.com/issues/340

Please sign in to comment.