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: 3b5840533e4442179dcf8a094b6f5d882c2e17d4 (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
package com.getbootstrap.savage.server

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 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 = {
      val branchName = settings.BranchPrefix + prNum.number
      Branch(branchName).getOrElse {
        throw new SecurityException(s"Generated insecure branch name: ${branchName}")
      }
    }
    val branchSpec = s"${commitSha.sha}:${newBranch.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}")
        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
  }
}