Commit 30241f8a authored by Nish Tahir's avatar Nish Tahir

Merge branch 'feature/github-user' into 'master'

Add Github profile feature and reduce repository info

- Add new Github profile feature
- Change Github repo pattern to only accept direct link
- Reduce repository info

See merge request !26
parents 109647e3 4884d024
Pipeline #4657337 failed with stages
in 7 minutes and 26 seconds
......@@ -3,19 +3,22 @@ package com.nishtahir.linkbait.github
import com.nishtahir.linkbait.plugin.{Attachment, MessageEvent, MessageEventListener, PluginContext}
import com.github.verbalexpressions.VerbalExpression
import VerbalExpression._
import org.kohsuke.github.{GHRepository, GitHub}
import scala.collection.JavaConverters._
import org.kohsuke.github.{GHRepository, GHUser, GitHub}
import scala.collection.JavaConverters._
import scala.collection.Map
import scala.io.Source
/**
* Created by nish on 10/4/16.
* Modified by niko-yuwono on 10/18/16.
*/
class GithubHandler(context: PluginContext) extends MessageEventListener {
val GITHUB_PATTERN =
$.andThen("http")
val GITHUB_REPO_PATTERN =
$.startOfLine()
.andThen("http")
.maybe("s")
.andThen("://")
.andThen("github.com/")
......@@ -23,41 +26,76 @@ class GithubHandler(context: PluginContext) extends MessageEventListener {
.andThen("/")
.find($.words(true).or($.digits(true)))
.maybe("/")
.endOfLine()
val GITHUB_PROFILE_PATTERN =
$.startOfLine()
.andThen("http")
.maybe("s")
.andThen("://")
.andThen("github.com/")
.find($.words(true).or($.digits(true)))
.maybe("/")
.endOfLine()
override def handleMessageEvent(event: MessageEvent): Unit = {
val matcher = GITHUB_PATTERN.compile.matcher(event.getMessage)
if (matcher.find()) {
val user = matcher.group(5)
val repo = matcher.group(9)
val repoMatcher = GITHUB_REPO_PATTERN.compile.matcher(event.getMessage)
if (repoMatcher.find()) {
val user = repoMatcher.group(5)
val repo = repoMatcher.group(9)
val repository = GitHub.connectAnonymously().getRepository(s"$user/$repo")
if(repository != null){
val name = repository.getFullName
val contributors = repository.listContributors().asList().size()
val titleUrl = repository.getHtmlUrl
val languages = repository.listLanguages().asScala.keySet.mkString(", ")
val openIssues = repository.getOpenIssueCount
val releases = repository.listReleases().asList().size()
val branches = repository.getBranches.size()
val commits = repository.listCommits().asList().size()
val license = repository.getLicense().getName
val readMe = Source.fromInputStream(repository.getReadme.read()).mkString
val att = new Attachment(name, readMe, "FFFFFF", titleUrl.toString, "", "")
att.setAdditionalFields(Map(
"Languages" -> languages.toString,
"Open issues" -> openIssues.toString,
"Contributors" -> contributors.toString,
"Branches" -> branches.toString,
"Commits" -> commits.toString,
"Releases" -> releases.toString,
"License" -> license.toString
).asJava)
context.getMessenger.sendAttachment(event.getChannel, att)
}
sendRepositoryAttachment(repository, event)
return
}
val profileMatcher = GITHUB_PROFILE_PATTERN.compile.matcher(event.getMessage)
if (repoMatcher.find()) {
val userID = profileMatcher.group(5)
val user = GitHub.connectAnonymously().getUser(userID)
}
}
private def sendRepositoryAttachment(repository: GHRepository, event: MessageEvent): Unit = {
if (repository == null) { return }
val name = repository.getFullName
val titleUrl = repository.getHtmlUrl
val languages = repository.listLanguages().asScala.keySet.mkString(", ")
val openIssues = repository.getOpenIssueCount
val latestReleases = repository.listReleases().asList().get(0)
val commits = repository.listCommits().asList().size()
val readMe = Source.fromInputStream(repository.getReadme.read()).mkString
val att = new Attachment(name, readMe, "FFFFFF", titleUrl.toString, "", "")
att.setAdditionalFields(Map(
"Languages" -> languages.toString,
"Open issues" -> openIssues.toString,
"Commits" -> commits.toString,
"Releases" -> latestReleases.toString
).asJava)
context.getMessenger.sendAttachment(event.getChannel, att)
}
private def sendUserAttachment(user: GHUser, event: MessageEvent): Unit = {
if (user == null) { return }
val name = user.getName
val blog = user.getBlog
val avatarUrl = user.getAvatarUrl
val repositories = user.getRepositories.asScala
val att = new Attachment(name, blog, "FFFFFF", "", avatarUrl, "")
att.setAdditionalFields(convertGHRepositories(repositories).asJava)
context.getMessenger.sendAttachment(event.getChannel, att)
}
private def convertGHRepositories(repositories: Map[String, GHRepository]) =
repositories.map(r => (r._1, createGHRepositoryStringDescription(r._2)))
private def createGHRepositoryStringDescription(repository: GHRepository) =
" Lang : " + repository.getLanguage + " open issue count : " + repository.getOpenIssueCount
}
package com.nishtahir.linkbait.github;
import com.nishtahir.linkbait.plugin.MessageEvent;
import org.junit.Test;
import java.util.regex.Matcher;
......@@ -9,13 +8,15 @@ import static org.junit.Assert.*;
/**
* Created by nish on 10/4/16.
* Modified by niko-yuwono on 10/18/16.
*/
public class GithubHandlerTest {
@Test
public void GITHUB_PATTERN() throws Exception {
public void testGithubRepoPatternValidUrl() throws Exception {
GithubHandler handler = new GithubHandler(null);
System.out.println(handler.GITHUB_PATTERN().toString());
Matcher matcher = handler.GITHUB_PATTERN().compile().matcher("https://github.com/Kotlin/anko/");
System.out.println(handler.GITHUB_REPO_PATTERN().toString());
Matcher matcher = handler.GITHUB_REPO_PATTERN().compile().matcher("https://github.com/Kotlin/anko/");
if(matcher.find()){
String user = matcher.group(5);
String repo = matcher.group(9);
......@@ -25,20 +26,37 @@ public class GithubHandlerTest {
} else {
fail();
}
}
// assert(matcher.matches());
// for(int i = 0; i < matcher.groupCount(); i++){
// System.out.println("" + i + ": " + matcher.group(i));
// }
// System.out.println(groups);
@Test
public void testGithubRepoPatternInvalidUrl() throws Exception {
GithubHandler handler = new GithubHandler(null);
System.out.println(handler.GITHUB_REPO_PATTERN().toString());
Matcher matcher = handler.GITHUB_REPO_PATTERN().compile().matcher("https://github.com/Kotlin/anko/issues");
assertFalse(matcher.find());
}
@Test
public void testHandleMessageEvent(){
// MessageEvent mockEvent = new MessageEvent();
// mockEvent.setMessage("https://github.com/Kotlin/anko");
// GithubHandler handler = new GithubHandler(null);
// handler.handleMessageEvent(mockEvent);
public void testGithubProfilePatternValidUrl() throws Exception {
GithubHandler handler = new GithubHandler(null);
System.out.println(handler.GITHUB_PROFILE_PATTERN().toString());
Matcher matcher = handler.GITHUB_PROFILE_PATTERN().compile().matcher("https://github.com/Kotlin/");
if(matcher.find()){
String user = matcher.group(5);
assertEquals(user, "Kotlin");
} else {
fail();
}
}
@Test
public void testGithubProfilePatternInvalidUrl() throws Exception {
GithubHandler handler = new GithubHandler(null);
System.out.println(handler.GITHUB_PROFILE_PATTERN().toString());
Matcher matcher = handler.GITHUB_PROFILE_PATTERN().compile().matcher("https://github.com/Kotlin/anko");
assertFalse(matcher.find());
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment