Skip to content

Commit 642e911

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

File tree

4 files changed

+203
-10
lines changed

4 files changed

+203
-10
lines changed

.github/workflows/pull_request.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ 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+
swift cross-pr-checkout.swift "${{ github.repository }}" "${{ github.event.number }}"
15+
# soundness:
16+
# name: Soundness
17+
# uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
18+
# with:
19+
# license_header_check_enabled: false
20+
# 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: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)