diff --git a/README.md b/README.md index b5b1278..297efc2 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ sbt clean assembly export gw_tasks=followerWatchTask,repositoryWatchTask,reactionWatchTask # tasks export gw_user_id=${YOUR_GITHUB_USER_ID} export gw_es_endpoint=${YOUR_ES_ENDPOINT} # default: localhost:9200 +export gw_es_username=${YOUR_ES_USERNAME) # Optional +export gw_es_password=${YOUR_ES_PASSWORD} # Optional export gw_line_bot_id=${YOUR_LINE_BOT_ID} export gw_line_channel_token=${YOUR_LINE_CHANNEL_TOKEN} export gw_github_api_token=${YOUR_GITHUB_API_TOKEN} diff --git a/src/main/scala/org/occidere/githubwatcher/GithubWatcher.scala b/src/main/scala/org/occidere/githubwatcher/GithubWatcher.scala index d170811..ea13830 100644 --- a/src/main/scala/org/occidere/githubwatcher/GithubWatcher.scala +++ b/src/main/scala/org/occidere/githubwatcher/GithubWatcher.scala @@ -14,10 +14,11 @@ import scala.util.{Failure, Success, Try} */ object GithubWatcher extends App with GithubWatcherTask with GithubWatcherLogger { private val userId = sys.env.getOrElse("gw_user_id", "") - sys.env.getOrElse("gw_tasks", "").split(",").map(_.trim) + private val skipAlert = sys.env.getOrElse("gw_skip_alert", "false").toBoolean + sys.env.getOrElse("gw_tasks", "").split(",").map(_.trim).filterNot(taskName => taskName.isBlank) .foreach(task => { if (!AVAILABLE_TASKS.contains(task)) logger.warn(s"Unsupported task: $task") - else Try(AVAILABLE_TASKS(task).run(userId)) match { + else Try(AVAILABLE_TASKS(task).run(userId, skipAlert)) match { case Success(_) => logger.info(s"$task Success!") case Failure(e) => logger.error(s"$task Failed!", e) } diff --git a/src/main/scala/org/occidere/githubwatcher/service/ElasticService.scala b/src/main/scala/org/occidere/githubwatcher/service/ElasticService.scala index 4c917bb..c34191b 100644 --- a/src/main/scala/org/occidere/githubwatcher/service/ElasticService.scala +++ b/src/main/scala/org/occidere/githubwatcher/service/ElasticService.scala @@ -4,7 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.sksamuel.elastic4s.requests.searches.SearchIterator import com.sksamuel.elastic4s.{ElasticClient, ElasticProperties} +import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials} import org.apache.http.client.config.RequestConfig +import org.apache.http.impl.client.BasicCredentialsProvider +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder import org.occidere.githubwatcher.logger.GithubWatcherLogger import org.occidere.githubwatcher.vo._ @@ -19,13 +22,23 @@ import scala.util.Try */ object ElasticService extends GithubWatcherLogger { private lazy val MAPPER = new ObjectMapper().registerModule(DefaultScalaModule) + + private val esUsername = sys.env.get("gw_es_username") + private val esPassword = sys.env.get("gw_es_password") + private lazy val client = ElasticClient( com.sksamuel.elastic4s.http.JavaClient( ElasticProperties(s"http://${sys.env.getOrElse("gw_es_endpoint", "localhost:9200")}"), (requestConfigBuilder: RequestConfig.Builder) => requestConfigBuilder .setSocketTimeout(60000) .setConnectTimeout(10000) - .setConnectionRequestTimeout(10000) + .setConnectionRequestTimeout(10000), + // Set credentials if exist + (httpClientBuilder: HttpAsyncClientBuilder) => httpClientBuilder.setDefaultCredentialsProvider( + new BasicCredentialsProvider() { + if (hasCredentials) setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUsername.get, esPassword.get)) + } + ) ) ) @@ -77,6 +90,8 @@ object ElasticService extends GithubWatcherLogger { def close(): Unit = client.close() + private def hasCredentials = esUsername.isDefined && esPassword.isDefined; + private def scrollFilterSearch[T](index: String, queryString: String, dataType: Class[T]): List[T] = SearchIterator.hits(client, search(index) .bool(boolQuery().filter(query(queryString))) diff --git a/src/main/scala/org/occidere/githubwatcher/task/FollowerWatchTask.scala b/src/main/scala/org/occidere/githubwatcher/task/FollowerWatchTask.scala index 9495600..5ce66dd 100644 --- a/src/main/scala/org/occidere/githubwatcher/task/FollowerWatchTask.scala +++ b/src/main/scala/org/occidere/githubwatcher/task/FollowerWatchTask.scala @@ -12,7 +12,7 @@ import org.occidere.githubwatcher.vo.FollowerDiff */ object FollowerWatchTask extends Task with GithubWatcherLogger { - override def run(userId: String): Unit = { + override def run(userId: String, skipAlert: Boolean = false): Unit = { logger.info(s"User ID: $userId") // Data from GitHub API @@ -31,7 +31,7 @@ object FollowerWatchTask extends Task with GithubWatcherLogger { logger.info(s"Not changed followers: ${diff.notChangedFollowerLogins.size}") // Send line message if follower changed - if (diff.hasChanged) LineMessengerService.sendFollowerMessage(diff) + if (diff.hasChanged && !skipAlert) LineMessengerService.sendFollowerMessage(diff) // Update DB to latest data including repos latestUser.followerLogins = diff.newFollowerLogins ++ diff.notChangedFollowerLogins diff --git a/src/main/scala/org/occidere/githubwatcher/task/ReactionWatchTask.scala b/src/main/scala/org/occidere/githubwatcher/task/ReactionWatchTask.scala index 40a85ed..4db68da 100644 --- a/src/main/scala/org/occidere/githubwatcher/task/ReactionWatchTask.scala +++ b/src/main/scala/org/occidere/githubwatcher/task/ReactionWatchTask.scala @@ -14,7 +14,7 @@ import scala.util.{Failure, Success, Try} * @since 2020-09-23 */ object ReactionWatchTask extends Task with GithubWatcherLogger { - override def run(userId: String): Unit = { + override def run(userId: String, skipAlert: Boolean = false): Unit = { logger.info(s"User ID: $userId") // Fetch reactions from Issues @@ -30,7 +30,7 @@ object ReactionWatchTask extends Task with GithubWatcherLogger { // Diff & Send line message val changedReactions = latestReactions.filter(latest => { val diff = ReactionDiff(prevReactions.getOrElse(latest.uniqueKey, copyReactionWithoutCounts(latest)), latest) - Try(if (diff.hasChanged) LineMessengerService.sendReactionMessage(diff)) match { + Try(if (diff.hasChanged && !skipAlert) LineMessengerService.sendReactionMessage(diff)) match { case Failure(e) => logger.error(s"${latest.uniqueKey} process failed (htmlUrl: ${latest.htmlUrl})", e) false diff --git a/src/main/scala/org/occidere/githubwatcher/task/RepositoryWatchTask.scala b/src/main/scala/org/occidere/githubwatcher/task/RepositoryWatchTask.scala index db64171..dd51253 100644 --- a/src/main/scala/org/occidere/githubwatcher/task/RepositoryWatchTask.scala +++ b/src/main/scala/org/occidere/githubwatcher/task/RepositoryWatchTask.scala @@ -14,7 +14,7 @@ import scala.util.{Failure, Success, Try} */ object RepositoryWatchTask extends Task with GithubWatcherLogger { - override def run(userId: String): Unit = { + override def run(userId: String, skipAlert: Boolean = false): Unit = { logger.info(s"User ID: $userId") // Latest repos from API @@ -38,7 +38,7 @@ object RepositoryWatchTask extends Task with GithubWatcherLogger { logger.info(s"Repo: ${diff.repoName} (changed: ${diff.hasChanged})") // Send message - Try(if (diff.hasChanged) LineMessengerService.sendRepositoryMessage(diff)) match { + Try(if (diff.hasChanged && !skipAlert) LineMessengerService.sendRepositoryMessage(diff)) match { case Failure(exception) => logger.error(s"${latest.name} process failed", exception) false diff --git a/src/main/scala/org/occidere/githubwatcher/task/Task.scala b/src/main/scala/org/occidere/githubwatcher/task/Task.scala index 6e43f1b..4ee5702 100644 --- a/src/main/scala/org/occidere/githubwatcher/task/Task.scala +++ b/src/main/scala/org/occidere/githubwatcher/task/Task.scala @@ -7,5 +7,5 @@ package org.occidere.githubwatcher.task * @since 2020-09-22 */ trait Task { - def run(userId: String): Unit + def run(userId: String, skipAlert: Boolean = false): Unit } \ No newline at end of file