forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHub.swift
196 lines (163 loc) · 5.81 KB
/
GitHub.swift
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// GitHub.swift
// Carthage
//
// Created by Justin Spahr-Summers on 2014-10-10.
// Copyright (c) 2014 Carthage. All rights reserved.
//
import Foundation
import Result
import ReactiveCocoa
import Tentacle
/// The User-Agent to use for GitHub requests.
private func gitHubUserAgent() -> String {
let bundle = NSBundle.mainBundle() ?? NSBundle(identifier: CarthageKitBundleIdentifier)
let version = bundle.flatMap {
($0.objectForInfoDictionaryKey("CFBundleShortVersionString") ??
$0.objectForInfoDictionaryKey(kCFBundleVersionKey as String)) as? String
} ?? "unknown"
let identifier = bundle?.bundleIdentifier ?? "CarthageKit-unknown"
return "\(identifier)/\(version)"
}
extension Repository {
/// The URL that should be used for cloning this repository over HTTPS.
public var HTTPSURL: GitURL {
let auth: String
if let token = tokenFromEnvironment(forServer: server) {
auth = "\(token)@"
} else {
auth = ""
}
let scheme: String
#if swift(>=2.3)
scheme = server.URL.scheme!
#else
scheme = server.URL.scheme
#endif
return GitURL("\(scheme)://\(auth)\(server.URL.host!)/\(owner)/\(name).git")
}
/// The URL that should be used for cloning this repository over SSH.
public var SSHURL: GitURL {
return GitURL("ssh://git@\(server.URL.host!)/\(owner)/\(name).git")
}
/// The URL for filing a new GitHub issue for this repository.
public var newIssueURL: NSURL {
return NSURL(string: "\(server)/\(owner)/\(name)/issues/new")!
}
/// Matches an identifier of the form "owner/name".
private static let NWORegex = try! NSRegularExpression(pattern: "^([\\-\\.\\w]+)/([\\-\\.\\w]+)$", options: [])
/// Parses repository information out of a string of the form "owner/name"
/// for the github.com, or the form "http(s)://hostname/owner/name" for
/// Enterprise instances.
public static func fromIdentifier(identifier: String) -> Result<Repository, CarthageError> {
// GitHub.com
let range = NSRange(location: 0, length: (identifier as NSString).length)
if let match = NWORegex.firstMatchInString(identifier, options: [], range: range) {
let owner = (identifier as NSString).substringWithRange(match.rangeAtIndex(1))
let name = (identifier as NSString).substringWithRange(match.rangeAtIndex(2))
return .Success(self.init(owner: owner, name: stripGitSuffix(name)))
}
// GitHub Enterprise
if let
URL = NSURL(string: identifier),
host = URL.host,
// The trailing slash of the host is included in the components.
var pathComponents = URL.pathComponents?.filter({ $0 != "/" })
where pathComponents.count >= 2
{
// Consider that the instance might be in subdirectories.
let name = pathComponents.removeLast()
let owner = pathComponents.removeLast()
// If the host name starts with “github.com”, that is not an enterprise
// one.
if host == "github.com" || host == "www.github.com" {
return .Success(self.init(owner: owner, name: stripGitSuffix(name)))
} else {
let baseURL = URL.URLByDeletingLastPathComponent!.URLByDeletingLastPathComponent!
return .Success(self.init(server: .Enterprise(url: baseURL), owner: owner, name: stripGitSuffix(name)))
}
}
return .Failure(CarthageError.ParseError(description: "invalid GitHub repository identifier \"\(identifier)\""))
}
}
extension Release {
/// The name of this release, with fallback to its tag when the name is an empty string or nil.
public var nameWithFallback: String {
if let name = name where !name.isEmpty {
return name
}
return tag
}
}
private func credentialsFromGit(forServer server: Server) -> (String, String)? {
let data = "url=\(server)".dataUsingEncoding(NSUTF8StringEncoding)!
return launchGitTask([ "credential", "fill" ], standardInput: SignalProducer(value: data))
.flatMap(.Concat) { string in
return string.linesProducer
}
.reduce([:]) { (values: [String: String], line: String) -> [String: String] in
var values = values
let parts = line.characters
.split(1, allowEmptySlices: false) { $0 == "=" }
.map(String.init)
if parts.count >= 2 {
let key = parts[0]
let value = parts[1]
values[key] = value
}
return values
}
.map { (values: [String: String]) -> (String, String)? in
if let username = values["username"], password = values["password"] {
return (username, password)
}
return nil
}
.first()?
.value ?? nil
}
private func tokenFromEnvironment(forServer server: Server) -> String? {
let environment = NSProcessInfo.processInfo().environment
if let accessTokenInput = environment["GITHUB_ACCESS_TOKEN"] {
// Treat the input as comma-separated series of domains and tokens.
// (e.g., `GITHUB_ACCESS_TOKEN="github.com=XXXXXXXXXXXXX,enterprise.local/ghe=YYYYYYYYY"`)
let records = accessTokenInput
.characters
.split(allowEmptySlices: false) { $0 == "," }
.reduce([:]) { (values: [String: String], record) in
var values = values
let parts = record.split(1, allowEmptySlices: false) { $0 == "=" }.map(String.init)
switch parts.count {
case 1:
// If the input is provided as an access token itself, use the
// token for Github.com.
values["github.com"] = parts[0]
case 2:
let (server, token) = (parts[0], parts[1])
values[server] = token
default:
break
}
return values
}
return records[server.URL.host!]
}
return nil
}
extension Client {
convenience init(repository: Repository, authenticated: Bool = true) {
if Client.userAgent == nil {
Client.userAgent = gitHubUserAgent()
}
let server = repository.server
if !authenticated {
self.init(server)
} else if let token = tokenFromEnvironment(forServer: server) {
self.init(server, token: token)
} else if let (username, password) = credentialsFromGit(forServer: server) {
self.init(server, username: username, password: password)
} else {
self.init(server)
}
}
}