|
| 1 | +import Foundation |
| 2 | +import RegexBuilder |
| 3 | + |
| 4 | +#if canImport(FoundationNetworking) |
| 5 | +import FoundationNetworking |
| 6 | +#endif |
| 7 | + |
| 8 | +struct GenericError: Error, CustomStringConvertible { |
| 9 | + var description: String |
| 10 | + |
| 11 | + init(_ description: String) { |
| 12 | + self.description = description |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/// Run the given command |
| 17 | +func run(_ executable: URL, _ arguments: String..., workingDirectory: URL? = nil) throws { |
| 18 | + print("Running " + ([executable.path] + arguments).joined(separator: " ")) |
| 19 | + let process = Process() |
| 20 | + process.executableURL = executable |
| 21 | + process.arguments = arguments |
| 22 | + if let workingDirectory { |
| 23 | + process.currentDirectoryURL = workingDirectory |
| 24 | + } |
| 25 | + |
| 26 | + try process.run() |
| 27 | + process.waitUntilExit() |
| 28 | +} |
| 29 | + |
| 30 | +/// Find the executable with the given name |
| 31 | +public func lookup(executable: String) throws -> URL { |
| 32 | + // Compute search paths from PATH variable. |
| 33 | + #if os(Windows) |
| 34 | + let pathSeparator: Character = ";" |
| 35 | + let pathVariable = "Path" |
| 36 | + #else |
| 37 | + let pathSeparator: Character = ":" |
| 38 | + let pathVariable = "PATH" |
| 39 | + #endif |
| 40 | + guard let pathString = ProcessInfo.processInfo.environment[pathVariable] else { |
| 41 | + throw GenericError("Failed to read path environment variable") |
| 42 | + } |
| 43 | + for searchPath in pathString.split(separator: pathSeparator) { |
| 44 | + let candidateUrl = URL(fileURLWithPath: String(searchPath)).appendingPathComponent(executable) |
| 45 | + if FileManager.default.isExecutableFile(atPath: candidateUrl.path) { |
| 46 | + return candidateUrl |
| 47 | + } |
| 48 | + } |
| 49 | + throw GenericError("Did not find \(executable)") |
| 50 | +} |
| 51 | + |
| 52 | +struct CrossRepoPR { |
| 53 | + let repositoryOwner: String |
| 54 | + let repositoryName: String |
| 55 | + let prNumber: String |
| 56 | +} |
| 57 | + |
| 58 | +/// The JSON fields of the `https://api.github.com/repos/\(repository)/pulls/\(prNumber)` endpoint that we care about. |
| 59 | +struct PRInfo: Codable { |
| 60 | + struct Base: Codable { |
| 61 | + let ref: String |
| 62 | + } |
| 63 | + let base: Base |
| 64 | + let body: String |
| 65 | +} |
| 66 | + |
| 67 | +/// - Parameters: |
| 68 | +/// - repository: The repository's name, eg. `swiftlang/swift-syntax` |
| 69 | +func getPRInfo(repository: String, prNumber: String) throws -> PRInfo { |
| 70 | + guard let prInfoUrl = URL(string: "https://api.github.com/repos/\(repository)/pulls/\(prNumber)") else { |
| 71 | + throw GenericError("Failed to form URL for GitHub API") |
| 72 | + } |
| 73 | + |
| 74 | + let data = try Data(contentsOf: prInfoUrl) |
| 75 | + return try JSONDecoder().decode(PRInfo.self, from: data) |
| 76 | +} |
| 77 | + |
| 78 | +func getCrossRepoPrs(repository: String, prNumber: String) throws -> [CrossRepoPR] { |
| 79 | + var result: [CrossRepoPR] = [] |
| 80 | + let prInfo = try getPRInfo(repository: repository, prNumber: prNumber) |
| 81 | + for line in prInfo.body.split(separator: "\n") { |
| 82 | + guard line.lowercased().starts(with: "linked pr:") else { |
| 83 | + continue |
| 84 | + } |
| 85 | + let repoRegex = Regex { |
| 86 | + Capture { |
| 87 | + #/swiftlang|apple/# |
| 88 | + } |
| 89 | + "/" |
| 90 | + Capture { |
| 91 | + #/[-a-zA-Z0-9_]+/# |
| 92 | + } |
| 93 | + ChoiceOf { |
| 94 | + "/pull/" |
| 95 | + "#" |
| 96 | + } |
| 97 | + Capture { |
| 98 | + OneOrMore(.digit) |
| 99 | + } |
| 100 | + } |
| 101 | + for match in line.matches(of: repoRegex) { |
| 102 | + result.append( |
| 103 | + CrossRepoPR(repositoryOwner: String(match.0), repositoryName: String(match.1), prNumber: String(match.2)) |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + return result |
| 108 | +} |
| 109 | + |
| 110 | +print(ProcessInfo.processInfo.arguments) |
| 111 | + |
| 112 | +guard ProcessInfo.processInfo.arguments.count > 2 else { |
| 113 | + throw GenericError( |
| 114 | + """ |
| 115 | + Expected two arguments: |
| 116 | + - Repository name, eg. `swiftlang/swift-syntax |
| 117 | + - PR number |
| 118 | + """ |
| 119 | + ) |
| 120 | +} |
| 121 | +let repository = ProcessInfo.processInfo.arguments[1] |
| 122 | +let prNumber = ProcessInfo.processInfo.arguments[2] |
| 123 | + |
| 124 | +let crossRepoPrs = try getCrossRepoPrs(repository: repository, prNumber: prNumber) |
| 125 | + |
| 126 | +for crossRepoPr in crossRepoPrs { |
| 127 | + let git = try lookup(executable: "git") |
| 128 | + let swift = try lookup(executable: "swift") |
| 129 | + let baseBranch = try getPRInfo( |
| 130 | + repository: "\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryOwner)", |
| 131 | + prNumber: crossRepoPr.prNumber |
| 132 | + ).base.ref |
| 133 | + |
| 134 | + let workspaceDir = URL(fileURLWithPath: "..") |
| 135 | + let repoDir = workspaceDir.appendingPathComponent(crossRepoPr.repositoryName) |
| 136 | + try run( |
| 137 | + git, |
| 138 | + "clone", |
| 139 | + "https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git", |
| 140 | + "\(crossRepoPr.repositoryName)", |
| 141 | + workingDirectory: workspaceDir |
| 142 | + ) |
| 143 | + try run(git, "fetch", "origin", "pull/\(crossRepoPr.prNumber)/merge:pr_merge", workingDirectory: repoDir) |
| 144 | + try run(git, "checkout", baseBranch, workingDirectory: repoDir) |
| 145 | + try run(git, "reset", "--hard", "pr_merge", workingDirectory: repoDir) |
| 146 | + try run( |
| 147 | + swift, |
| 148 | + "package", |
| 149 | + "config", |
| 150 | + "set-mirror", |
| 151 | + "--package-url", |
| 152 | + "https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git", |
| 153 | + "--mirror-url", |
| 154 | + repoDir.resolvingSymlinksInPath().path |
| 155 | + ) |
| 156 | +} |
0 commit comments