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

BranchDeleter.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: 396130fa011377cfe224a1da813ea92a43469783 (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
package com.getbootstrap.savage.server

import scala.collection.JavaConverters._
import org.eclipse.egit.github.core.service.RepositoryService
import com.getbootstrap.savage.github.{GitHubActorWithLogging, Branch, BranchDeletionRequest}
import com.getbootstrap.savage.github.util.RichRepositoryId
import com.getbootstrap.savage.util.{SuccessfulExit, ErrorExit, SimpleSubprocess}

class BranchDeleter extends GitHubActorWithLogging {
  override def receive = {
    case BranchDeletionRequest(branch, commitSha) => {
      if (isSavageBranch(branch)) {
        val repoService = new RepositoryService(gitHubClient)
        val maybeRepoBranch = repoService.getBranches(settings.TestRepoId).asScala.find{ _.getName == branch.name }
        maybeRepoBranch match {
          case None => log.info(s"Nothing to delete; ${branch} does not exist in ${settings.TestRepoId}")
          case Some(repoBranch) => {
            val repoSha = repoBranch.getCommit.getSha
            if (repoSha == commitSha.sha) {
              val remote = settings.TestRepoId.asPushRemote
              val process = SimpleSubprocess(Seq("git", "push", remote, ":" + branch.name))
              log.info(s"Deleting ${branch} from remote ${remote}")
              process.run() match {
                case SuccessfulExit(_) => log.info(s"Successfully deleted ${branch} in ${remote}")
                case ErrorExit(exitValue, output) => log.error(s"Error deleting ${branch} in ${remote} :\nExit code: ${exitValue}\n${output}")
              }
            }
            else {
              log.info(s"Not deleting ${branch} from ${settings.TestRepoId} because commits differ; request was for ${commitSha} but current is ${repoSha}")
            }
          }
        }
      }
      else {
        log.error(s"Not deleting non-Savage branch : ${branch}")
      }
    }
  }

  private def isSavageBranch(branch: Branch): Boolean = branch.name startsWith settings.BranchPrefix
}