| 
 | 1 | +import Foundation  | 
 | 2 | + | 
 | 3 | +#if canImport(FoundationNetworking)  | 
 | 4 | +import FoundationNetworking  | 
 | 5 | +#endif  | 
 | 6 | + | 
 | 7 | +struct GenericError: Error, CustomStringConvertible {  | 
 | 8 | +  var description: String  | 
 | 9 | + | 
 | 10 | +  init(_ description: String) {  | 
 | 11 | +    self.description = description  | 
 | 12 | +  }  | 
 | 13 | +}  | 
 | 14 | + | 
 | 15 | +func run(_ executable: URL, _ arguments: String..., workingDirectory: URL? = nil) throws {  | 
 | 16 | +  print("Running " + ([executable.path] + arguments).joined(separator: " "))  | 
 | 17 | +  let process = Process()  | 
 | 18 | +  process.executableURL = executable  | 
 | 19 | +  process.arguments = arguments  | 
 | 20 | +  if let workingDirectory {  | 
 | 21 | +    process.currentDirectoryURL = workingDirectory  | 
 | 22 | +  }  | 
 | 23 | + | 
 | 24 | +  try process.run()  | 
 | 25 | +  process.waitUntilExit()  | 
 | 26 | +}  | 
 | 27 | + | 
 | 28 | +public func lookup(executable: String) throws -> URL {  | 
 | 29 | +  // Compute search paths from PATH variable.  | 
 | 30 | +  #if os(Windows)  | 
 | 31 | +  let pathSeparator: Character = ";"  | 
 | 32 | +  let pathVariable = "Path"  | 
 | 33 | +  #else  | 
 | 34 | +  let pathSeparator: Character = ":"  | 
 | 35 | +  let pathVariable = "PATH"  | 
 | 36 | +  #endif  | 
 | 37 | +  guard let pathString = ProcessInfo.processInfo.environment[pathVariable] else {  | 
 | 38 | +    throw GenericError("Failed to read path environment variable")  | 
 | 39 | +  }  | 
 | 40 | +  for searchPath in pathString.split(separator: pathSeparator) {  | 
 | 41 | +    let candidateUrl = URL(fileURLWithPath: String(searchPath)).appendingPathComponent(executable)  | 
 | 42 | +    if FileManager.default.isExecutableFile(atPath: candidateUrl.path) {  | 
 | 43 | +      return candidateUrl  | 
 | 44 | +    }  | 
 | 45 | +  }  | 
 | 46 | +  throw GenericError("Did not find \(executable)")  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +struct CrossRepoPR {  | 
 | 50 | +  let org: String  | 
 | 51 | +  let repo: String  | 
 | 52 | +  let prNum: String  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +let crossRepoPrs = [  | 
 | 56 | +  CrossRepoPR(org: "swiftlang", repo: "swift-syntax", prNum: "2859")  | 
 | 57 | +]  | 
 | 58 | + | 
 | 59 | +func getBaseBranch(_ crossRepoPr: CrossRepoPR) throws -> String {  | 
 | 60 | +  struct PR: Codable {  | 
 | 61 | +    struct Base: Codable {  | 
 | 62 | +      let ref: String  | 
 | 63 | +    }  | 
 | 64 | +    let base: Base  | 
 | 65 | +  }  | 
 | 66 | + | 
 | 67 | +  let data = try Data(  | 
 | 68 | +    contentsOf: URL(  | 
 | 69 | +      string: "https://api.github.com/repos/\(crossRepoPr.org)/\(crossRepoPr.repo)/pulls/\(crossRepoPr.prNum)"  | 
 | 70 | +    )!  | 
 | 71 | +  )  | 
 | 72 | +  let decoded = try JSONDecoder().decode(PR.self, from: data)  | 
 | 73 | +  return decoded.base.ref  | 
 | 74 | +}  | 
 | 75 | + | 
 | 76 | +for (key, value) in ProcessInfo.processInfo.environment {  | 
 | 77 | +  print("\(key): \(value)")  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +for crossRepoPr in crossRepoPrs {  | 
 | 81 | +  let git = try lookup(executable: "git")  | 
 | 82 | +  let swift = try lookup(executable: "swift")  | 
 | 83 | +  let baseBranch = try getBaseBranch(crossRepoPr)  | 
 | 84 | + | 
 | 85 | +  let workspaceDir = URL(fileURLWithPath: "..")  | 
 | 86 | +  let repoDir = workspaceDir.appendingPathComponent(crossRepoPr.repo)  | 
 | 87 | +  try run(  | 
 | 88 | +    git,  | 
 | 89 | +    "clone",  | 
 | 90 | +    "https://github.com/\(crossRepoPr.org)/\(crossRepoPr.repo).git",  | 
 | 91 | +    "\(crossRepoPr.repo)",  | 
 | 92 | +    workingDirectory: workspaceDir  | 
 | 93 | +  )  | 
 | 94 | +  try run(git, "fetch", "origin", "pull/\(crossRepoPr.prNum)/merge:pr_merge", workingDirectory: repoDir)  | 
 | 95 | +  try run(git, "checkout", baseBranch, workingDirectory: repoDir)  | 
 | 96 | +  try run(git, "reset", "--hard", "pr_merge", workingDirectory: repoDir)  | 
 | 97 | +  try run(  | 
 | 98 | +    swift,  | 
 | 99 | +    "package",  | 
 | 100 | +    "config",  | 
 | 101 | +    "set-mirror",  | 
 | 102 | +    "--package-url",  | 
 | 103 | +    "https://github.com/\(crossRepoPr.org)/\(crossRepoPr.repo).git",  | 
 | 104 | +    "--mirror-url",  | 
 | 105 | +    repoDir.resolvingSymlinksInPath().path  | 
 | 106 | +  )  | 
 | 107 | +}  | 
0 commit comments