Skip to content

Commit eb73b54

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

File tree

4 files changed

+154
-10
lines changed

4 files changed

+154
-10
lines changed

.github/workflows/pull_request.yml

+9-5
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.event.base.repo.full_name }}" "${{ 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

+5-5
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

+33
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

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)