Skip to content

Commit 8995242

Browse files
committed
Enable cross-PR testing
1 parent d82d736 commit 8995242

File tree

4 files changed

+258
-10
lines changed

4 files changed

+258
-10
lines changed

.github/workflows/pull_request.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ jobs:
88
tests:
99
name: Test
1010
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
11-
soundness:
12-
name: Soundness
13-
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
1411
with:
15-
license_header_check_enabled: false
16-
license_header_check_project_name: "Swift.org"
12+
linux_pre_build_command: |
13+
swiftc cross-pr-checkout.swift -o ../cross-pr-checkout
14+
../cross-pr-checkout "${{ github.repository }}" "${{ github.event.number }}"
15+
windows_pre_build_command: |
16+
swiftc cross-pr-checkout.swift -o ..\cross-pr-checkout
17+
..\cross-pr-checkout "${{ github.repository }}" "${{ github.event.number }}"
18+
# soundness:
19+
# name: Soundness
20+
# uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
21+
# with:
22+
# license_header_check_enabled: false
23+
# license_header_check_project_name: "Swift.org"

Sources/SwiftFormat/Rules/UseShorthandTypeNames.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
4848
switch node.name.text {
4949
case "Array":
5050
guard let argument = genericArgumentList.firstAndOnly,
51-
case .type(let typeArgument) = argument else {
51+
case .type(let typeArgument) = argument.argument else {
5252
newNode = nil
5353
break
5454
}
@@ -62,7 +62,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
6262
case "Dictionary":
6363
guard let arguments = exactlyTwoChildren(of: genericArgumentList),
6464
case .type(let type0Argument) = arguments.0.argument,
65-
caes .type(let type1Argument) = arguments.1.argument else {
65+
case .type(let type1Argument) = arguments.1.argument else {
6666
newNode = nil
6767
break
6868
}
@@ -79,7 +79,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
7979
break
8080
}
8181
guard let argument = genericArgumentList.firstAndOnly,
82-
case .type(let typeArgument) = argument else {
82+
case .type(let typeArgument) = argument.argument else {
8383
newNode = nil
8484
break
8585
}
@@ -143,7 +143,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
143143
switch expression.baseName.text {
144144
case "Array":
145145
guard let argument = genericArgumentList.firstAndOnly,
146-
case .type(let typeArgument) = argument else {
146+
case .type(let typeArgument) = argument.argument else {
147147
newNode = nil
148148
break
149149
}
@@ -172,7 +172,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
172172

173173
case "Optional":
174174
guard let argument = genericArgumentList.firstAndOnly,
175-
case .type(let typeArgument) = argument else {
175+
case .type(let typeArgument) = argument.argument else {
176176
newNode = nil
177177
break
178178
}

cross-pr-checkout.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import subprocess
2+
import pathlib
3+
import requests
4+
5+
class CrossRepoPR:
6+
org: str
7+
repo: str
8+
pr_num: str
9+
10+
def __init__(self, org: str, repo: str, pr_num: str) -> None:
11+
self.org = org
12+
self.repo = repo
13+
self.pr_num = pr_num
14+
15+
def cross_repo_prs() -> list[CrossRepoPR]:
16+
return [
17+
CrossRepoPR("swiftlang", "swift-syntax", "2859")
18+
]
19+
20+
def run(cmd: list[str], cwd: str|None = None):
21+
print(" ".join(cmd))
22+
subprocess.check_call(cmd, cwd=cwd)
23+
24+
def main():
25+
for cross_repo_pr in cross_repo_prs():
26+
run(["git", "clone", f"https://github.com/{cross_repo_pr.org}/{cross_repo_pr.repo}.git", f"{cross_repo_pr.repo}"], cwd="..")
27+
run(["git", "fetch", "origin", f"pull/{cross_repo_pr.pr_num}/merge:pr_merge"], cwd="../swift-syntax")
28+
run(["git", "checkout", "main"], cwd="../swift-syntax")
29+
run(["git", "reset", "--hard", "pr_merge"], cwd="../swift-syntax")
30+
run(["swift", "package", "config", "set-mirror", "--package-url", "https://github.com/swiftlang/swift-syntax.git", "--mirror-url", str(pathlib.Path("../swift-syntax").resolve())])
31+
32+
if __name__ == "__main__":
33+
main()

cross-pr-checkout.swift

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import Foundation
2+
import RegexBuilder
3+
4+
#if canImport(FoundationNetworking)
5+
import FoundationNetworking
6+
#endif
7+
8+
#if canImport(WinSDK)
9+
import WinSDK
10+
#endif
11+
12+
struct GenericError: Error, CustomStringConvertible {
13+
var description: String
14+
15+
init(_ description: String) {
16+
self.description = description
17+
}
18+
}
19+
20+
/// Escape the given command to be printed for log output.
21+
func escapeCommand(_ executable: URL, _ arguments: [String]) -> String {
22+
return ([executable.path] + arguments).map {
23+
if $0.contains(" ") {
24+
return "'\($0)'"
25+
}
26+
return $0
27+
}.joined(separator: " ")
28+
}
29+
30+
/// Launch a subprocess with the given command and wait for it to finish
31+
func run(_ executable: URL, _ arguments: String..., workingDirectory: URL? = nil) throws {
32+
print("Running \(escapeCommand(executable, arguments))")
33+
let process = Process()
34+
process.executableURL = executable
35+
process.arguments = arguments
36+
if let workingDirectory {
37+
process.currentDirectoryURL = workingDirectory
38+
}
39+
40+
try process.run()
41+
process.waitUntilExit()
42+
guard process.terminationStatus == 0 else {
43+
throw GenericError(
44+
"\(escapeCommand(executable, arguments)) failed with non-zero exit code: \(process.terminationStatus)"
45+
)
46+
}
47+
}
48+
49+
/// Find the executable with the given name
50+
public func lookup(executable: String) throws -> URL {
51+
// Compute search paths from PATH variable.
52+
#if os(Windows)
53+
let pathVariable = "Path"
54+
let pathSeparator: Character = ";"
55+
#else
56+
let pathVariable = "PATH"
57+
let pathSeparator: Character = ":"
58+
#endif
59+
guard let pathString = ProcessInfo.processInfo.environment[pathVariable] else {
60+
throw GenericError("Failed to read path environment variable")
61+
}
62+
for searchPath in pathString.split(separator: pathSeparator) {
63+
let candidateUrl = URL(fileURLWithPath: String(searchPath)).appendingPathComponent(executable)
64+
if FileManager.default.isExecutableFile(atPath: candidateUrl.path) {
65+
return candidateUrl
66+
}
67+
}
68+
throw GenericError("Did not find \(executable)")
69+
}
70+
71+
/// The JSON fields of the `https://api.github.com/repos/\(repository)/pulls/\(prNumber)` endpoint that we care about.
72+
struct PRInfo: Codable {
73+
struct Base: Codable {
74+
/// The name of the PR's base branch.
75+
let ref: String
76+
}
77+
/// The base branch of the PR
78+
let base: Base
79+
80+
/// The PR's description.
81+
let body: String?
82+
}
83+
84+
/// - Parameters:
85+
/// - repository: The repository's name, eg. `swiftlang/swift-syntax`
86+
func getPRInfo(repository: String, prNumber: String) throws -> PRInfo {
87+
guard let prInfoUrl = URL(string: "https://api.github.com/repos/\(repository)/pulls/\(prNumber)") else {
88+
throw GenericError("Failed to form URL for GitHub API")
89+
}
90+
91+
do {
92+
let data = try Data(contentsOf: prInfoUrl)
93+
return try JSONDecoder().decode(PRInfo.self, from: data)
94+
} catch {
95+
throw GenericError("Failed to load PR info from \(prInfoUrl): \(error)")
96+
}
97+
}
98+
99+
/// Information about a PR that should be tested with this PR.
100+
struct CrossRepoPR {
101+
/// The owner of the repository, eg. `swiftlang`
102+
let repositoryOwner: String
103+
104+
/// The name of the repository, eg. `swift-syntax`
105+
let repositoryName: String
106+
107+
/// The PR number that's referenced.
108+
let prNumber: String
109+
}
110+
111+
/// Retrieve all PRs that are referenced from the PR with the given number in `repository`.
112+
/// `repository` is the owner and repo name joined by `/`, eg. `swiftlang/swift-syntax`.
113+
func getCrossRepoPrs(repository: String, prNumber: String) throws -> [CrossRepoPR] {
114+
var result: [CrossRepoPR] = []
115+
let prInfo = try getPRInfo(repository: repository, prNumber: prNumber)
116+
for line in prInfo.body?.split(separator: "\n") ?? [] {
117+
guard line.lowercased().starts(with: "linked pr:") else {
118+
continue
119+
}
120+
let repoRegex = Regex {
121+
Capture {
122+
#/swiftlang|apple/#
123+
}
124+
"/"
125+
Capture {
126+
#/[-a-zA-Z0-9_]+/#
127+
}
128+
ChoiceOf {
129+
"/pull/"
130+
"#"
131+
}
132+
Capture {
133+
OneOrMore(.digit)
134+
}
135+
}
136+
for match in line.matches(of: repoRegex) {
137+
result.append(
138+
CrossRepoPR(repositoryOwner: String(match.1), repositoryName: String(match.2), prNumber: String(match.3))
139+
)
140+
}
141+
}
142+
return result
143+
}
144+
145+
func main() throws {
146+
guard ProcessInfo.processInfo.arguments.count >= 3 else {
147+
throw GenericError(
148+
"""
149+
Expected two arguments:
150+
- Repository name, eg. `swiftlang/swift-syntax
151+
- PR number
152+
"""
153+
)
154+
}
155+
let repository = ProcessInfo.processInfo.arguments[1]
156+
let prNumber = ProcessInfo.processInfo.arguments[2]
157+
158+
let crossRepoPrs = try getCrossRepoPrs(repository: repository, prNumber: prNumber)
159+
if !crossRepoPrs.isEmpty {
160+
print("Detected cross-repo PRs")
161+
for crossRepoPr in crossRepoPrs {
162+
print("\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName)#\(crossRepoPr.prNumber)")
163+
}
164+
}
165+
166+
for crossRepoPr in crossRepoPrs {
167+
let git = try lookup(executable: "git")
168+
let swift = try lookup(executable: "swift")
169+
let baseBranch = try getPRInfo(
170+
repository: "\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName)",
171+
prNumber: crossRepoPr.prNumber
172+
).base.ref
173+
174+
let workspaceDir = URL(fileURLWithPath: "..")
175+
let repoDir = workspaceDir.appendingPathComponent(crossRepoPr.repositoryName)
176+
try run(
177+
git,
178+
"clone",
179+
"https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git",
180+
"\(crossRepoPr.repositoryName)",
181+
workingDirectory: workspaceDir
182+
)
183+
try run(git, "fetch", "origin", "pull/\(crossRepoPr.prNumber)/merge:pr_merge", workingDirectory: repoDir)
184+
try run(git, "checkout", baseBranch, workingDirectory: repoDir)
185+
try run(git, "reset", "--hard", "pr_merge", workingDirectory: repoDir)
186+
try run(
187+
swift,
188+
"package",
189+
"config",
190+
"set-mirror",
191+
"--package-url",
192+
"https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git",
193+
"--mirror-url",
194+
repoDir.resolvingSymlinksInPath().path
195+
)
196+
}
197+
}
198+
199+
do {
200+
try main()
201+
} catch {
202+
print(error)
203+
#if os(Windows)
204+
_Exit(1)
205+
#else
206+
exit(1)
207+
#endif
208+
}

0 commit comments

Comments
 (0)