Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GitHubPullRequestCommenter.scala « github « rorschach « getbootstrap « com « scala « main « src - github.com/twbs/rorschach.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2084a95d9f647d32f23407a35b96283712482a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.getbootstrap.rorschach.github

import scala.util.{Try,Failure,Success}
import org.eclipse.egit.github.core.service.{PullRequestService, IssueService}
import org.eclipse.egit.github.core.RepositoryId
import com.getbootstrap.rorschach.github.util.RichPullRequest
import com.getbootstrap.rorschach.server.Settings


class GitHubPullRequestCommenter extends GitHubActorWithLogging {
  // val settings = Settings(context.system)

  private def tryToCommentOn(repo: RepositoryId, prNum: PullRequestNumber, commentMarkdown: String) = {
    val issueService = new IssueService(gitHubClient)
    Try { issueService.createComment(repo, prNum.number, commentMarkdown) }
  }

  private def tryToClose(repo: RepositoryId, prNum: PullRequestNumber): Try[None.type] = {
    val prService = new PullRequestService(gitHubClient)
    val prTry = Try { prService.getPullRequest(repo, prNum.number) } match {
      case fetchFail@Failure(exc) => {
        log.error(exc, s"Error fetching pull request ${prNum} in order to close it")
        fetchFail
      }
      case Success(pr) => {
        pr.status = Closed
        Try { prService.editPullRequest(repo, pr) }
      }
    }
    prTry.map{ x => None }
  }

  override def receive = {
    case PullRequestFeedback(repo, prNum, requester, messages) => {
      val username = requester.getLogin
      val messagesMarkdown = messages.map{ "* " + _ }.mkString("\n")
      val commentMarkdown = s"""
        |Hi @${username}!
        |
        |Thanks for wanting to contribute to Bootstrap by sending this pull request!
        |Unfortunately, your pull request seems to have some problems:
        |${messagesMarkdown}
        |
        |You'll need to **fix these mistakes** and revise your pull request before we can proceed further.
        |Once you've fixed these problems, you can either ask the maintainers to re-open this pull request, or you can create a new pull request.
        |Thanks!
        |
        |(*Please note that this is a [fully automated](https://github.com/twbs/rorschach) comment.*)
      """.stripMargin

      tryToCommentOn(repo, prNum, commentMarkdown) match {
        case Success(comment) => log.info(s"Successfully posted comment ${comment.getUrl} for ${prNum}")
        case Failure(exc) => log.error(exc, s"Error posting comment for ${prNum}")
      }

      if (settings.CloseBadPullRequests) {
        tryToClose(repo, prNum) match {
          case Success(_) => log.info(s"Successfully closed ${prNum} due to failed audit(s)")
          case Failure(exc) => log.error(exc, s"Error closing ${prNum}")
        }
      }
    }
  }
}