aboutsummaryrefslogtreecommitdiff
path: root/bot/test/PRServiceTests.scala
blob: 08155d59dfd948f3e0c1ea08485412021f17ea0a (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package dotty.tools.bot

import org.junit.Assert._
import org.junit.Test

import io.circe._
import io.circe.generic.auto._
import io.circe.syntax._
import io.circe.parser.decode

import model.Github._
import org.http4s.client.blaze._
import scalaz.concurrent.Task

class PRServiceTests extends PullRequestService {
  val user = sys.env("USER")
  val token = sys.env("TOKEN")

  def getResource(r: String): String =
    Option(getClass.getResourceAsStream(r)).map(scala.io.Source.fromInputStream)
      .map(_.mkString)
      .getOrElse(throw new Exception(s"resource not found: $r"))

  @Test def canUnmarshalIssueJson = {
    val json = getResource("/test-pr.json")
    val issue: Issue = decode[Issue](json) match {
      case Right(is: Issue) => is
      case Left(ex) => throw ex
    }

    assert(issue.pull_request.isDefined, "missing pull request")
  }

  @Test def canGetAllCommitsFromPR = {
    val httpClient = PooledHttp1Client()
    val issueNbr = 1941 // has 2 commits: https://github.com/lampepfl/dotty/pull/1941/commits

    val List(c1, c2) = getCommits(issueNbr, httpClient).run

    assertEquals(
      "Represent untyped operators as Ident instead of Name",
      c1.commit.message.takeWhile(_ != '\n')
    )

    assertEquals(
      "Better positions for infix term operations.",
      c2.commit.message.takeWhile(_ != '\n')
    )
  }

  @Test def canGetMoreThan100Commits = {
    val httpClient = PooledHttp1Client()
    val issueNbr = 1840 // has >100 commits: https://github.com/lampepfl/dotty/pull/1840/commits

    val numberOfCommits = getCommits(issueNbr, httpClient).run.length

    assert(
      numberOfCommits > 100,
      s"PR 1840, should have a number of commits greater than 100, but was: $numberOfCommits"
    )
  }

  @Test def canCheckCLA = {
    val httpClient = PooledHttp1Client()
    val validUserCommit = Commit("sha-here", Author(Some("felixmulder")), Author(Some("felixmulder")), CommitInfo(""))
    val statuses: List[CommitStatus] = checkCLA(validUserCommit :: Nil, httpClient).run

    assert(statuses.length == 1, s"wrong number of valid statuses: got ${statuses.length}, expected 1")
    httpClient.shutdownNow()
  }

  @Test def canSetStatus = {
    val httpClient = PooledHttp1Client()
    val sha = "fa64b4b613fe5e78a5b4185b4aeda89e2f1446ff"
    val status = Invalid("smarter", Commit(sha, Author(Some("smarter")), Author(Some("smarter")), CommitInfo("")))

    val statuses: List[StatusResponse] = sendStatuses(status :: Nil, httpClient).run

    assert(
      statuses.length == 1,
      s"assumed one status response would be returned, got: ${statuses.length}"
    )

    assert(
      statuses.head.state == "failure",
      s"status set had wrong state, expected 'failure', got: ${statuses.head.state}"
    )

    httpClient.shutdownNow()
  }
}