Skip to content

Commit

Permalink
Merge pull request #23 from occidere/feature/issue-22
Browse files Browse the repository at this point in the history
[BE] Add skip alert param, es credentials param
  • Loading branch information
occidere committed Oct 8, 2021
2 parents 9d4ee8f + 4626b01 commit fe2f355
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/org/occidere/githubwatcher/GithubWatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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))
}
)
)
)

Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/occidere/githubwatcher/task/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fe2f355

Please sign in to comment.