Skip to content

Commit 203e5cf

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

File tree

4 files changed

+217
-10
lines changed

4 files changed

+217
-10
lines changed

.github/workflows/pull_request.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ 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+
enable_windows_checks: false
13+
linux_pre_build_command: |
14+
swiftc cross-pr-checkout.swift -o /tmp/cross-pr-checkout
15+
/tmp/cross-pr-checkout "${{ github.repository }}" "${{ github.event.number }}"
16+
# soundness:
17+
# name: Soundness
18+
# uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
19+
# with:
20+
# license_header_check_enabled: false
21+
# 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: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
/// Launch a subprocess with the given command and wait for it to finish
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+
print("Loading PR info from \(prInfoUrl)")
74+
75+
let data = try Data(contentsOf: prInfoUrl)
76+
print("Done loading PR info from \(prInfoUrl)")
77+
return try JSONDecoder().decode(PRInfo.self, from: data)
78+
}
79+
80+
func getCrossRepoPrs(repository: String, prNumber: String) throws -> [CrossRepoPR] {
81+
var result: [CrossRepoPR] = []
82+
let prInfo = try getPRInfo(repository: repository, prNumber: prNumber)
83+
for line in prInfo.body.split(separator: "\n") {
84+
guard line.lowercased().starts(with: "linked pr:") else {
85+
continue
86+
}
87+
let repoRegex = Regex {
88+
Capture {
89+
#/swiftlang|apple/#
90+
}
91+
"/"
92+
Capture {
93+
#/[-a-zA-Z0-9_]+/#
94+
}
95+
ChoiceOf {
96+
"/pull/"
97+
"#"
98+
}
99+
Capture {
100+
OneOrMore(.digit)
101+
}
102+
}
103+
for match in line.matches(of: repoRegex) {
104+
result.append(
105+
CrossRepoPR(repositoryOwner: String(match.0), repositoryName: String(match.1), prNumber: String(match.2))
106+
)
107+
}
108+
}
109+
return result
110+
}
111+
112+
func main() throws {
113+
print("Start")
114+
print(ProcessInfo.processInfo.arguments)
115+
116+
guard ProcessInfo.processInfo.arguments.count >= 3 else {
117+
throw GenericError(
118+
"""
119+
Expected two arguments:
120+
- Repository name, eg. `swiftlang/swift-syntax
121+
- PR number
122+
"""
123+
)
124+
}
125+
let repository = ProcessInfo.processInfo.arguments[1]
126+
let prNumber = ProcessInfo.processInfo.arguments[2]
127+
128+
let crossRepoPrs = try getCrossRepoPrs(repository: repository, prNumber: prNumber)
129+
print("Detected cross-repo PRs: \(crossRepoPrs)")
130+
131+
for crossRepoPr in crossRepoPrs {
132+
let git = try lookup(executable: "git")
133+
let swift = try lookup(executable: "swift")
134+
let baseBranch = try getPRInfo(
135+
repository: "\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryOwner)",
136+
prNumber: crossRepoPr.prNumber
137+
).base.ref
138+
139+
let workspaceDir = URL(fileURLWithPath: "..")
140+
let repoDir = workspaceDir.appendingPathComponent(crossRepoPr.repositoryName)
141+
try run(
142+
git,
143+
"clone",
144+
"https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git",
145+
"\(crossRepoPr.repositoryName)",
146+
workingDirectory: workspaceDir
147+
)
148+
try run(git, "fetch", "origin", "pull/\(crossRepoPr.prNumber)/merge:pr_merge", workingDirectory: repoDir)
149+
try run(git, "checkout", baseBranch, workingDirectory: repoDir)
150+
try run(git, "reset", "--hard", "pr_merge", workingDirectory: repoDir)
151+
try run(
152+
swift,
153+
"package",
154+
"config",
155+
"set-mirror",
156+
"--package-url",
157+
"https://github.com/\(crossRepoPr.repositoryOwner)/\(crossRepoPr.repositoryName).git",
158+
"--mirror-url",
159+
repoDir.resolvingSymlinksInPath().path
160+
)
161+
}
162+
}
163+
164+
do {
165+
main()
166+
} catch {
167+
print(error)
168+
exit(1)
169+
}

0 commit comments

Comments
 (0)