Skip to content

Commit b53fdcb

Browse files
KristofferCjrevels
authored andcommitted
implement the gist API (#68)
1 parent 358d382 commit b53fdcb

File tree

5 files changed

+250
-4
lines changed

5 files changed

+250
-4
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Here's a table that matches up the provided `GitHubType`s with their correspondi
5151
| `Status` | id, e.g. `366961773` | [commit statuses](https://developer.github.com/v3/repos/statuses/) |
5252
| `PullRequest` | number, e.g. `44` | [pull requests](https://developer.github.com/v3/pulls/) |
5353
| `Issue` | number, e.g. `31` | [issues](https://developer.github.com/v3/issues/) |
54-
| `Team` | id, e.g. `1` | [teams](https://developer.github.com/v3/orgs/teams) |
55-
54+
| `Team` | id, e.g. `1` | [teams](https://developer.github.com/v3/orgs/teams) |
55+
| `Gist` | id, e.g. `0bace7cc774df4b3a4b0ee9aaa271ef6` | [gists](https://developer.github.com/v3/gists) |
5656

5757
You can inspect which fields are available for a type `G<:GitHubType` by calling `fieldnames(G)`.
5858

@@ -155,6 +155,23 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
155155
| `watch(repo)` | `HttpCommon.Response` | [watch `repo`](https://developer.github.com/v3/activity/watching/#set-a-repository-subscription) |
156156
| `unwatch(repo)` | `HttpCommon.Response` | [unwatch `repo`](https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription) |
157157

158+
#### Gists
159+
160+
| method | return type | documentation |
161+
|------------------------------------------|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
162+
| `gist(id)` | `Gist` | [get the gist specified by `id`](https://developer.github.com/v3/gists/#get-a-single-gist) |
163+
| `gist(id, revision)` | `Gist` | [get the gist specified by `id` and `revision`](https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist) |
164+
| `gists()` | `Tuple{Vector{Gist}, Dict}` | [get all public gists](https://developer.github.com/v3/gists/#list-all-public-gists) |
165+
| `gists(owner)` | `Tuple{Vector{Gist}, Dict}` | [get all gists for `owner`](https://developer.github.com/v3/gists/#list-a-users-gists) |
166+
| `create_gist()` | `Gist` | [create a gist](https://developer.github.com/v3/gists/#create-a-gist) |
167+
| `edit_gist(gist)` | `Gist` | [edit a gist](https://developer.github.com/v3/gists/#edit-a-gist) |
168+
| `delete_gist(gist)` | `HttpCommon.Response` | [delete a gist](https://developer.github.com/v3/gists/#delete-a-gist) |
169+
| `create_gist_fork(gist)` | `Gist` | [fork a gist](https://developer.github.com/v3/gists/#fork-a-gist) |
170+
| `gist_forks(gist)` | `Tuple{Vector{Gist}, Dict}` | [list the forks of a gist](https://developer.github.com/v3/gists/#list-gist-forks) |
171+
| `star_gist(gist)` | `HttpCommon.Response` | [star `gist`](https://developer.github.com/v3/gists/#star-a-gist) |
172+
| `starred_gists()` | `Tuple{Vector{Gist}, Dict}` | [get the starred `gist`s](https://developer.github.com/v3/gists/#list-starred-gists) |
173+
| `unstar_gist(gist)` | `HttpCommon.Response` | [unstar `gist`](https://developer.github.com/v3/gists/#unstar-a-gist) |
174+
158175
#### Miscellaneous
159176

160177
| method | return type | documentation |
@@ -247,7 +264,7 @@ julia> prs # 3 items per page * 2 page limit == 6 items, as expected
247264
GitHub.PullRequest(38)
248265

249266
julia> page_data
250-
Dict{UTF8String,UTF8String} with 4 entries:
267+
Dict{String,String} with 4 entries:
251268
"prev" => "https://api.github.com/repositories/16635105/pulls?page=2&per_page=3&state=all"
252269
"next" => "https://api.github.com/repositories/16635105/pulls?page=4&per_page=3&state=all"
253270
"first" => "https://api.github.com/repositories/16635105/pulls?page=1&per_page=3&state=all"
@@ -274,7 +291,7 @@ julia> prs2
274291
GitHub.PullRequest(22)
275292

276293
julia> page_data2
277-
Dict{UTF8String,UTF8String} with 4 entries:
294+
Dict{String,String} with 4 entries:
278295
"prev" => "https://api.github.com/repositories/16635105/pulls?page=4&per_page=3&state=all"
279296
"next" => "https://api.github.com/repositories/16635105/pulls?page=6&per_page=3&state=all"
280297
"first" => "https://api.github.com/repositories/16635105/pulls?page=1&per_page=3&state=all"

src/GitHub.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ export # comments.jl
148148
edit_comment,
149149
delete_comment
150150

151+
152+
#########
153+
# Gists #
154+
#########
155+
156+
# include -------
157+
158+
include("gists/gist.jl")
159+
160+
# export --------
161+
162+
export # gist.jl
163+
Gist,
164+
gist,
165+
gists,
166+
create_gist,
167+
edit_gist,
168+
delete_gist,
169+
star_gist,
170+
unstar_gist,
171+
starred_gists,
172+
create_gist_fork,
173+
gist_forks
174+
151175
############
152176
# Activity #
153177
############

src/gists/gist.jl

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
type Gist <: GitHubType
2+
url::Nullable{HttpCommon.URI}
3+
forks_url::Nullable{HttpCommon.URI}
4+
commits_url::Nullable{HttpCommon.URI}
5+
id::Nullable{String}
6+
description::Nullable{String}
7+
public::Nullable{Bool}
8+
owner::Nullable{Owner}
9+
user::Nullable{Owner}
10+
truncated::Nullable{Bool}
11+
comments::Nullable{Int}
12+
comments_url::Nullable{HttpCommon.URI}
13+
html_url::Nullable{HttpCommon.URI}
14+
git_pull_url::Nullable{HttpCommon.URI}
15+
git_push_url::Nullable{HttpCommon.URI}
16+
created_at::Nullable{Dates.DateTime}
17+
updated_at::Nullable{Dates.DateTime}
18+
forks::Nullable{Vector{Gist}}
19+
files::Nullable{Dict}
20+
history::Nullable{Vector{Dict}}
21+
end
22+
23+
Gist(data::Dict) = json2github(Gist, data)
24+
Gist(id::AbstractString) = Gist(Dict("id" => id))
25+
26+
namefield(gist::Gist) = gist.id
27+
28+
###############
29+
# API Methods #
30+
###############
31+
32+
# creating #
33+
#----------#
34+
35+
gist(gist_obj::Gist; options...) = gist(name(gist_obj); options...)
36+
37+
function gist(gist_obj, sha = ""; options...)
38+
!isempty(sha) && (sha = "/" * sha)
39+
result = gh_get_json("/gists/$(name(gist_obj))$sha"; options...)
40+
g = Gist(result)
41+
end
42+
43+
function gists(owner; options...)
44+
results, page_data = gh_get_paged_json("/users/$(name(owner))/gists"; options...)
45+
map(Gist, results), page_data
46+
end
47+
48+
function gists(; options...)
49+
results, page_data = gh_get_paged_json("/gists/public"; options...)
50+
return map(Gist, results), page_data
51+
end
52+
53+
# modifying #
54+
#-----------#
55+
56+
create_gist(; options...) = Gist(gh_post_json("/gists"; options...))
57+
edit_gist(gist; options...) = Gist(gh_patch_json("/gists/$(name(gist))"; options...))
58+
delete_gist(gist; options...) = gh_delete("/gists/$(name(gist))"; options...)
59+
60+
# stars #
61+
#------#
62+
63+
star_gist(gist; options...) = gh_put("/gists/$(name(gist))/star"; options...)
64+
unstar_gist(gist; options...) = gh_delete("/gists/$(name(gist))/star"; options...)
65+
66+
function starred_gists(; options...)
67+
results, page_data = gh_get_paged_json("/gists/starred"; options...)
68+
return map(Gist, results), page_data
69+
end
70+
71+
# forks #
72+
#-------#
73+
74+
create_gist_fork(gist::Gist; options...) = Gist(gh_post_json("/gists/$(name(gist))/forks"; options...))
75+
76+
function gist_forks(gist; options...)
77+
results, page_data = gh_get_paged_json("/gists/$(name(gist))/forks"; options...)
78+
return map(Gist, results), page_data
79+
end

test/ghtype_tests.jl

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,121 @@ end
511511
test_show(team_result)
512512
end
513513

514+
@testset "Gist" begin
515+
gist_json = JSON.parse("""
516+
{
517+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d",
518+
"forks_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/forks",
519+
"commits_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/commits",
520+
"id": "aa5a315d61ae9438b18d",
521+
"description": "description of gist",
522+
"public": true,
523+
"owner": {
524+
"login": "octocat",
525+
"id": 1,
526+
"gravatar_id": "",
527+
"url": "https://api.github.com/users/octocat",
528+
"type": "User",
529+
"site_admin": false
530+
},
531+
"user": null,
532+
"files": {
533+
"ring.erl": {
534+
"size": 932,
535+
"raw_url": "https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl",
536+
"type": "text/plain",
537+
"language": "Erlang",
538+
"truncated": false,
539+
"content": "contents of gist"
540+
}
541+
},
542+
"truncated": false,
543+
"comments": 0,
544+
"comments_url": "https://api.github.com/gists/aa5a315d61ae9438b18d/comments/",
545+
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d",
546+
"git_pull_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
547+
"git_push_url": "https://gist.github.com/aa5a315d61ae9438b18d.git",
548+
"created_at": "2010-04-14T02:15:15Z",
549+
"updated_at": "2011-06-20T11:34:15Z",
550+
"forks": [
551+
{
552+
"user": {
553+
"login": "octocat",
554+
"id": 1,
555+
"gravatar_id": "",
556+
"url": "https://api.github.com/users/octocat",
557+
"site_admin": false
558+
},
559+
"url": "https://api.github.com/gists/dee9c42e4998ce2ea439",
560+
"id": "dee9c42e4998ce2ea439",
561+
"created_at": "2011-04-14T16:00:49Z",
562+
"updated_at": "2011-04-14T16:00:49Z"
563+
}
564+
],
565+
"history": [
566+
{
567+
"url": "https://api.github.com/gists/aa5a315d61ae9438b18d/57a7f021a713b1c5a6a199b54cc514735d2d462f",
568+
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
569+
"user": {
570+
"login": "octocat",
571+
"id": 1,
572+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
573+
"gravatar_id": "",
574+
"url": "https://api.github.com/users/octocat",
575+
"html_url": "https://github.com/octocat",
576+
"followers_url": "https://api.github.com/users/octocat/followers",
577+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
578+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
579+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
580+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
581+
"organizations_url": "https://api.github.com/users/octocat/orgs",
582+
"repos_url": "https://api.github.com/users/octocat/repos",
583+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
584+
"received_events_url": "https://api.github.com/users/octocat/received_events",
585+
"type": "User",
586+
"site_admin": false
587+
},
588+
"change_status": {
589+
"deletions": 0,
590+
"additions": 180,
591+
"total": 180
592+
},
593+
"committed_at": "2010-04-14T02:15:15Z"
594+
}
595+
]
596+
}
597+
"""
598+
)
599+
600+
gist_result = Gist(
601+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["url"])),
602+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["forks_url"])),
603+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["commits_url"])),
604+
Nullable{String}(gist_json["id"]),
605+
Nullable{String}(gist_json["description"]),
606+
Nullable{Bool}(gist_json["public"]),
607+
Nullable{Owner}(Owner(gist_json["owner"])),
608+
Nullable{Owner}(),
609+
Nullable{Bool}(gist_json["truncated"]),
610+
Nullable{Int}(gist_json["comments"]),
611+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["comments_url"])),
612+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["html_url"])),
613+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["git_pull_url"])),
614+
Nullable{HttpCommon.URI}(HttpCommon.URI(gist_json["git_push_url"])),
615+
Nullable{Dates.DateTime}(Dates.DateTime(chop(gist_json["created_at"]))),
616+
Nullable{Dates.DateTime}(Dates.DateTime(chop(gist_json["updated_at"]))),
617+
Nullable{Vector{Gist}}(map(Gist, gist_json["forks"])),
618+
Nullable{Dict}(gist_json["files"]),
619+
Nullable{Vector{Dict}}(gist_json["history"]),
620+
)
621+
622+
@test Gist(gist_json) == gist_result
623+
@test name(Gist(gist_json["id"])) == name(gist_result)
624+
@test setindex!(GitHub.github2json(gist_result), nothing, "user") == gist_json
625+
626+
test_show(gist_result)
627+
end
628+
514629
@testset "Installation" begin
515630
# This is the format of an installation in the "installation event"
516631
installation_json = JSON.parse("""

test/read_only_api_tests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ end
110110
@test hasghobj(40, first(issues(ghjl; auth = auth, params = state_param)))
111111
end
112112

113+
@testset "Gists" begin
114+
kc_gists, page_data = gists("KristofferC"; page_limit=1, params=Dict("per_page" => 5), auth = auth)
115+
@test typeof(kc_gists) == Vector{Gist}
116+
@test length(kc_gists) != 0
117+
@test get(get(kc_gists[1].owner).login) == "KristofferC"
118+
119+
gist_obj = gist("0cb70f50a28d79905aae907e12cbe58e"; auth = auth)
120+
@test length(get(gist_obj.files)) == 2
121+
@test get(gist_obj.files)["file1.jl"]["content"] == "Hello World!"
122+
end
123+
113124
@testset "Activity" begin
114125
# test GitHub.stargazers, GitHub.starred
115126
@test length(first(stargazers(ghjl; auth = auth))) > 10 # every package should fail tests if it's not popular enough :p

0 commit comments

Comments
 (0)