Skip to content

Commit e27bd7b

Browse files
authored
Merge pull request #303 from tayloraswift/repo-privacy
Repo privacy
2 parents dacf88b + 1261f54 commit e27bd7b

23 files changed

+216
-103
lines changed

Assets/css/Main.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/css/Main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/GitHubAPI/GitHub.Repo.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ extension GitHub
4949
public
5050
var fork:Bool
5151

52-
/// The repo’s visibility on GitHub. Some queries return only public repositories and so
53-
/// omit this field.
52+
/// The repo’s visibility on GitHub.
5453
public
55-
var visibility:RepoVisibility?
54+
var visibility:RepoVisibility
5655
/// The repo’s dominant language, if GitHub was able to detect one.
5756
public
5857
var language:String?
@@ -90,7 +89,7 @@ extension GitHub
9089
archived:Bool,
9190
disabled:Bool,
9291
fork:Bool,
93-
visibility:RepoVisibility? = nil,
92+
visibility:RepoVisibility,
9493
language:String? = nil,
9594
homepage:String? = nil,
9695
about:String? = nil,
@@ -172,7 +171,7 @@ extension GitHub.Repo:JSONObjectDecodable
172171
archived: try json[.archived].decode(),
173172
disabled: try json[.disabled].decode(),
174173
fork: try json[.fork].decode(),
175-
visibility: try json[.visibility]?.decode(),
174+
visibility: try json[.visibility].decode(),
176175
language: try json[.language]?.decode(),
177176
homepage: try json[.homepage]?.decode(),
178177
about: try json[.description]?.decode(),

Sources/GitHubAPI/GitHub.RepoVisibility.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,47 @@ import JSON
33
extension GitHub
44
{
55
@frozen public
6-
enum RepoVisibility:String, JSONEncodable, JSONDecodable, Equatable, Sendable
6+
enum RepoVisibility:Equatable, Comparable, Sendable
77
{
8-
case `public`
98
case `private`
9+
case `internal`
10+
case `public`
11+
}
12+
}
13+
extension GitHub.RepoVisibility:CustomStringConvertible
14+
{
15+
@inlinable public
16+
var description:String
17+
{
18+
switch self
19+
{
20+
case .private: "private"
21+
case .internal: "internal"
22+
case .public: "public"
23+
}
1024
}
1125
}
26+
extension GitHub.RepoVisibility:LosslessStringConvertible
27+
{
28+
@inlinable public
29+
init?(_ description:String)
30+
{
31+
// Note: the GraphQL API returns the visibility in all caps, for some reason.
32+
switch description
33+
{
34+
case "PRIVATE": self = .private
35+
case "private": self = .private
36+
37+
case "INTERNAL": self = .internal
38+
case "internal": self = .internal
39+
40+
case "PUBLIC": self = .public
41+
case "public": self = .public
42+
43+
default: return nil
44+
}
45+
}
46+
}
47+
extension GitHub.RepoVisibility:JSONStringDecodable
48+
{
49+
}

Sources/GitHubClient/GitHub.Client.Connection.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ extension GitHub.Client
2626
}
2727
}
2828
}
29-
extension GitHub.Client<GitHub.PersonalAccessToken>.Connection
29+
extension GitHub.Client<Void>.Connection
3030
{
3131
/// Run a GraphQL API request.
3232
///
3333
/// The request will be charged to the user associated with the stored token. It is not
3434
/// possible to run a GraphQL API request without a token.
3535
@inlinable public
36-
func post<Response>(query:String,
37-
for _:Response.Type = Response.self) async throws -> GraphQL.Response<Response>
38-
where Response:JSONDecodable
36+
func post<T>(query:String,
37+
expecting _:T.Type = T.self,
38+
with authorization:GitHub.ClientAuthorization) async throws -> GraphQL.Response<T>
39+
where T:JSONDecodable
3940
{
4041
let request:HTTP.Client2.Request = .init(headers:
4142
[
@@ -44,7 +45,7 @@ extension GitHub.Client<GitHub.PersonalAccessToken>.Connection
4445
":authority": self.http2.remote,
4546
":path": "/graphql",
4647

47-
"authorization": "Bearer \(self.app)",
48+
"authorization": authorization.header,
4849

4950
// GitHub will reject the API request if the user-agent is not set.
5051
"user-agent": self.agent,
@@ -65,8 +66,7 @@ extension GitHub.Client<GitHub.PersonalAccessToken>.Connection
6566
if let second:String = response.headers?["x-ratelimit-reset"].first,
6667
let second:Int64 = .init(second)
6768
{
68-
throw GitHub.Client<GitHub.PersonalAccessToken>.RateLimitError.init(
69-
until: .second(second))
69+
throw GitHub.Client<Application>.RateLimitError.init(until: .second(second))
7070
}
7171
else
7272
{

Sources/GitHubClient/GitHub.Client.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ extension GitHub
2727
}
2828
}
2929
}
30-
extension GitHub.Client<GitHub.PersonalAccessToken>
30+
extension GitHub.Client<Void>
3131
{
3232
public static
33-
func graphql(pat:consuming GitHub.PersonalAccessToken,
33+
func graphql(
3434
threads:consuming MultiThreadedEventLoopGroup,
3535
niossl:consuming NIOSSLContext,
36-
as agent:String) -> GitHub.Client<GitHub.PersonalAccessToken>
36+
as agent:String) -> GitHub.Client<Void>
3737
{
3838
.init(http2: .init(threads: threads, niossl: niossl, remote: "api.github.com"),
3939
agent: agent,
40-
app: pat)
40+
app: ())
4141
}
4242
}
4343
extension GitHub.Client where Application:GitHubApplication

Sources/GitHubClient/GitHub.ClientAuthorization.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import GitHubAPI
44
extension GitHub
55
{
66
@frozen public
7-
enum ClientAuthorization
7+
enum ClientAuthorization:Sendable
88
{
99
case basic(GitHub.OAuth)
1010
case token(String)
1111
}
1212
}
1313
extension GitHub.ClientAuthorization
14+
{
15+
@inlinable public
16+
static func token(_ iat:GitHub.InstallationAccessToken) -> Self { .token(iat.rawValue) }
17+
18+
@inlinable public
19+
static func token(_ pat:GitHub.PersonalAccessToken) -> Self { .token(pat.rawValue) }
20+
}
21+
extension GitHub.ClientAuthorization
1422
{
1523
@inlinable
1624
var header:String

Sources/UnidocDB/Packages/Unidoc.PackageRepo (ext).swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ extension Unidoc.PackageRepo
1212
{
1313
@inlinable public static
1414
func github(_ repo:GitHub.Repo,
15-
crawled:UnixMillisecond,
16-
installation:Int32? = nil) throws -> Self
15+
crawled:UnixMillisecond) throws -> Self
1716
{
1817
/// We clip this to Midnights because we use this as a shard key, and also because
1918
/// Midnights are Swifty.
@@ -46,6 +45,7 @@ extension Unidoc.PackageRepo
4645
created: .init(created),
4746
updated: updated,
4847
license: repo.license.map { .init(spdx: $0.id, name: $0.name) },
48+
private: repo.visibility < .public,
4949
topics: repo.topics,
5050
master: repo.master,
5151
origin: .github(.init(
@@ -59,8 +59,7 @@ extension Unidoc.PackageRepo
5959
size: repo.size,
6060
archived: repo.archived,
6161
disabled: repo.disabled,
62-
fork: repo.fork,
63-
installation: installation)),
62+
fork: repo.fork)),
6463
forks: repo.forks,
6564
stars: repo.stars)
6665
}

Sources/UnidocRecords/Origins/Unidoc.GitHubOrigin.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ extension Unidoc
3838
public
3939
var fork:Bool
4040

41-
/// A GitHub App installation, currently only set for private repositories.
42-
public
43-
var installation:Int32?
44-
4541
@inlinable public
4642
init(id:Int32,
4743
owner:String,
@@ -53,8 +49,7 @@ extension Unidoc
5349
size:Int,
5450
archived:Bool,
5551
disabled:Bool,
56-
fork:Bool,
57-
installation:Int32?)
52+
fork:Bool)
5853
{
5954
self.id = id
6055
self.pushed = pushed
@@ -67,7 +62,6 @@ extension Unidoc
6762
self.archived = archived
6863
self.disabled = disabled
6964
self.fork = fork
70-
self.installation = installation
7165
}
7266
}
7367
}
@@ -107,6 +101,8 @@ extension Unidoc.GitHubOrigin
107101
case archived = "X"
108102
case disabled = "D"
109103
case fork = "K"
104+
105+
@available(*, unavailable)
110106
case installation = "L"
111107
}
112108
}
@@ -129,7 +125,6 @@ extension Unidoc.GitHubOrigin:BSONDocumentEncodable
129125
bson[.archived] = self.archived
130126
bson[.disabled] = self.disabled
131127
bson[.fork] = self.fork
132-
bson[.installation] = self.installation
133128
}
134129
}
135130
extension Unidoc.GitHubOrigin:BSONDocumentDecodable
@@ -147,7 +142,6 @@ extension Unidoc.GitHubOrigin:BSONDocumentDecodable
147142
size: try bson[.size].decode(),
148143
archived: try bson[.archived].decode(),
149144
disabled: try bson[.disabled].decode(),
150-
fork: try bson[.fork].decode(),
151-
installation: try bson[.installation]?.decode())
145+
fork: try bson[.fork].decode())
152146
}
153147
}

Sources/UnidocRecords/Packages/Unidoc.PackageRepo.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ extension Unidoc
3333
/// The repo’s license. All software repos have the concept of a license.
3434
public
3535
var license:PackageLicense?
36+
37+
/// Indicates if the repo is private, and therefore cannot be cloned without
38+
/// authentication.
39+
public
40+
var `private`:Bool
41+
3642
/// The repo’s topic memberships. Both GitHub and GitLab have topics.
3743
public
3844
var topics:[String]
@@ -57,6 +63,7 @@ extension Unidoc
5763
created:UnixMillisecond,
5864
updated:UnixMillisecond,
5965
license:PackageLicense?,
66+
private:Bool,
6067
topics:[String],
6168
master:String?,
6269
origin:PackageOrigin,
@@ -69,6 +76,7 @@ extension Unidoc
6976
self.created = created
7077
self.updated = updated
7178
self.license = license
79+
self.private = `private`
7280
self.topics = topics
7381
self.master = master
7482
self.origin = origin
@@ -95,6 +103,7 @@ extension Unidoc.PackageRepo
95103
case updated = "U"
96104

97105
case license = "L"
106+
case `private` = "X"
98107
case topics = "T"
99108
case master = "M"
100109

@@ -115,6 +124,7 @@ extension Unidoc.PackageRepo:BSONDocumentEncodable
115124
bson[.updated] = self.updated
116125

117126
bson[.license] = self.license
127+
bson[.private] = self.private
118128
bson[.topics] = self.topics.isEmpty ? nil : self.topics
119129
bson[.master] = self.master
120130

@@ -140,6 +150,8 @@ extension Unidoc.PackageRepo:BSONDocumentDecodable
140150
created: try bson[.created].decode(),
141151
updated: try bson[.updated].decode(),
142152
license: try bson[.license]?.decode(),
153+
// TODO: deoptionalize
154+
private: try bson[.private]?.decode() ?? false,
143155
topics: try bson[.topics]?.decode() ?? [],
144156
master: try bson[.master]?.decode(),
145157
origin: origin,

0 commit comments

Comments
 (0)