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

PullRequestPusher.scala « server « savage « getbootstrap « com « scala « main « src - github.com/twbs/savage.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 030736ea9c7b886d016b6f10005a74d565af3738 (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
package com.getbootstrap.savage.server

import akka.actor.ActorRef
import org.eclipse.egit.github.core.RepositoryId
import com.getbootstrap.savage.github._
import com.getbootstrap.savage.github.util.RichRepositoryId
import com.getbootstrap.savage.util.{ErrorExit, SuccessfulExit, SimpleSubprocess}
import com.getbootstrap.savage.util.{RichPath,UnixFileSystemString}

class PullRequestPusher(
  protected val branchDeleter: ActorRef
) extends GitHubActorWithLogging {
  private val gitRemoteRefsDirectory = ".git/refs/remotes".asUnixPath

  override def receive = {
    case PullRequestPushRequest(originRepo, prNum, commitSha) => {
      if (pull(originRepo)) {
        push(originRepo = originRepo, prNum = prNum, commitSha = commitSha)
      }
    }
  }

  def pull(originRepo: RepositoryId): Boolean = {
    // clobberingly fetch all branch heads into a dummy remote
    SimpleSubprocess(Seq("git", "fetch", "--no-tags", "--recurse-submodules=no", originRepo.asPullRemote, "+refs/heads/*:refs/remotes/scratch/*")).run() match {
      case SuccessfulExit(_) => {
        log.info(s"Successfully fetched from ${originRepo}")
        true
      }
      case ErrorExit(exitValue, output) => {
        log.error(s"Error fetching from ${originRepo}:\nExit code: ${exitValue}\n${output}")
        false
      }
    }
  }

  def push(originRepo: RepositoryId, prNum: PullRequestNumber, commitSha: CommitSha): Boolean = {
    val newBranch = SavageBranch(prNum, commitSha)
    val branchSpec = s"${commitSha.sha}:${newBranch.branch.asRef}"
    val destRemote = settings.TestRepoId.asPushRemote
    val success = SimpleSubprocess(Seq("git", "push", "-f", destRemote, branchSpec)).run() match {
      case SuccessfulExit(_) => {
        log.info(s"Successfully pushed ${commitSha} from ${originRepo} to ${destRemote} as ${newBranch}")
        scheduleFailsafeBranchDeletion(newBranch)
        true
      }
      case ErrorExit(exitValue, output) => {
        log.error(s"Error pushing ${commitSha} from ${originRepo} to ${destRemote} as ${newBranch}:\nExit code: ${exitValue}\n${output}")
        false
      }
    }
    // delete all remote refs
    implicit val logger = log
    gitRemoteRefsDirectory.deleteRecursively()
    success
  }

  private def scheduleFailsafeBranchDeletion(branch: SavageBranch) {
    implicit val execContext = context.system.dispatcher
    context.system.scheduler.scheduleOnce(settings.TravisTimeout, branchDeleter, branch)
  }
}