summaryrefslogtreecommitdiff
path: root/Hutch/Extensions/SRHTWebURL.swift
blob: 9bc155270af0321a643d81fc77ede18bd1c99999 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Foundation

enum SRHTWebURL {
    static let chat = URL(string: "https://chat.sr.ht")!
    static let status = URL(string: "https://status.sr.ht")!

    static func repository(_ repository: RepositorySummary) -> URL? {
        userScopedURL(
            host: "\(repository.service.rawValue).sr.ht",
            ownerCanonicalName: repository.owner.canonicalName,
            pathComponents: [repository.name]
        )
    }

    static func commit(repository: RepositorySummary, commitId: String) -> URL? {
        userScopedURL(
            host: "\(repository.service.rawValue).sr.ht",
            ownerCanonicalName: repository.owner.canonicalName,
            pathComponents: [repository.name, commitPathComponent(for: repository.service), commitId]
        )
    }

    static func file(repository: RepositorySummary, revspec: String, path: String) -> URL? {
        switch repository.service {
        case .git:
            return userScopedURL(
                host: "git.sr.ht",
                ownerCanonicalName: repository.owner.canonicalName,
                pathComponents: [repository.name, "tree", revspec, "item"] + pathComponents(from: path)
            )
        case .hg:
            var components = URLComponents()
            components.scheme = "https"
            components.host = "hg.sr.ht"

            let ownerUsername = username(from: repository.owner.canonicalName)
            let encodedRepository = repository.name.addingPercentEncoding(withAllowedCharacters: pathComponentCharacterSet) ?? repository.name
            let encodedPath = path.split(separator: "/").map {
                String($0).addingPercentEncoding(withAllowedCharacters: pathComponentCharacterSet) ?? String($0)
            }.joined(separator: "/")

            components.percentEncodedPath = "/~\(ownerUsername)/\(encodedRepository)/browse/\(encodedPath)"
            components.queryItems = [URLQueryItem(name: "rev", value: revspec)]
            return components.url
        default:
            return nil
        }
    }

    static func build(jobId: Int, ownerCanonicalName: String) -> URL? {
        userScopedURL(
            host: "builds.sr.ht",
            ownerCanonicalName: ownerCanonicalName,
            pathComponents: ["job", String(jobId)]
        )
    }

    static func tracker(ownerUsername: String, trackerName: String) -> URL? {
        userScopedURL(
            host: "todo.sr.ht",
            ownerUsername: ownerUsername,
            pathComponents: [trackerName]
        )
    }

    static func ticket(ownerUsername: String, trackerName: String, ticketId: Int) -> URL? {
        userScopedURL(
            host: "todo.sr.ht",
            ownerUsername: ownerUsername,
            pathComponents: [trackerName, String(ticketId)]
        )
    }

    static func profile(canonicalName: String) -> URL? {
        userScopedURL(
            host: "sr.ht",
            ownerCanonicalName: canonicalName,
            pathComponents: []
        )
    }

    static func paste(ownerCanonicalName: String, pasteId: String) -> URL? {
        userScopedURL(
            host: "paste.sr.ht",
            ownerCanonicalName: ownerCanonicalName,
            pathComponents: [pasteId]
        )
    }

    private static func userScopedURL(
        host: String,
        ownerCanonicalName: String,
        pathComponents: [String]
    ) -> URL? {
        userScopedURL(
            host: host,
            ownerUsername: username(from: ownerCanonicalName),
            pathComponents: pathComponents
        )
    }

    private static func userScopedURL(
        host: String,
        ownerUsername: String,
        pathComponents: [String]
    ) -> URL? {
        var components = URLComponents()
        components.scheme = "https"
        components.host = host

        let encodedComponents = (["~\(ownerUsername)"] + pathComponents).map { pathComponent in
            pathComponent.addingPercentEncoding(withAllowedCharacters: pathComponentCharacterSet) ?? pathComponent
        }
        components.percentEncodedPath = "/" + encodedComponents.joined(separator: "/")
        return components.url
    }

    private static func username(from canonicalName: String) -> String {
        if canonicalName.hasPrefix("~") {
            return String(canonicalName.dropFirst())
        }
        return canonicalName
    }

    private static func commitPathComponent(for service: SRHTService) -> String {
        switch service {
        case .hg:
            return "rev"
        default:
            return "commit"
        }
    }

    private static let pathComponentCharacterSet: CharacterSet = {
        var characterSet = CharacterSet.urlPathAllowed
        characterSet.remove(charactersIn: "/")
        return characterSet
    }()

    private static func pathComponents(from path: String) -> [String] {
        path.split(separator: "/").map(String.init)
    }
}