Skip to content

Commit 3cf3501

Browse files
committed
Git Data: Fix bugs, add tests/docs
1 parent 066da8f commit 3cf3501

File tree

5 files changed

+197
-5
lines changed

5 files changed

+197
-5
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Here's a table that matches up the provided `GitHubType`s with their correspondi
4444
|---------------|--------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
4545
| `Owner` | login, e.g. `"octocat"` | [organizations](https://developer.github.com/v3/orgs/), [users](https://developer.github.com/v3/users/) |
4646
| `Repo` | full_name, e.g. `"JuliaWeb/GitHub.jl"` | [repositories](https://developer.github.com/v3/repos/) |
47-
| `Commit` | sha, e.g. `"d069993b320c57b2ba27336406f6ec3a9ae39375"` | [repository commits](https://developer.github.com/v3/repos/commits/) |
47+
| `Commit` | sha, e.g. `"d069993b320c57b2ba27336406f6ec3a9ae39375"` | [repository commits](https://developer.github.com/v3/repos/commits/) |
48+
| `GitCommit` | sha, e.g. `"d069993b320c57b2ba27336406f6ec3a9ae39375"` | [raw git commits](https://developer.github.com/v3/git/commits/) |
4849
| `Branch` | name, e.g. `master` | [repository branches](https://developer.github.com/v3/repos/#get-branch) |
4950
| `Content` | path, e.g. `"src/owners/owners.jl"` | [repository contents](https://developer.github.com/v3/repos/contents/) |
5051
| `Comment` | id, e.g. `162224613` | [commit comments](https://developer.github.com/v3/repos/comments/), [issue comments](https://developer.github.com/v3/issues/comments/), [PR review comments](https://developer.github.com/v3/pulls/comments/) |
@@ -54,6 +55,11 @@ Here's a table that matches up the provided `GitHubType`s with their correspondi
5455
| `Team` | id, e.g. `1` | [teams](https://developer.github.com/v3/orgs/teams) |
5556
| `Gist` | id, e.g. `0bace7cc774df4b3a4b0ee9aaa271ef6` | [gists](https://developer.github.com/v3/gists) |
5657
| `Review` | id, e.g. `1` | [reviews](https://developer.github.com/v3/pulls/reviews/) |
58+
| `Blob` | sha, e.g. `"95c8d1aa2a7b1e6d672e15b67e0df4abbe57dcbe"` | [raw git blobs](https://developer.github.com/v3/git/blobs/)
59+
| `Tree` | sha, e.g. `"78e524d5e979e326a7c144ce195bf94ca9b04fa0"` | [raw git trees](https://developer.github.com/v3/git/trees/)
60+
| `Tag` | tag name, e.g. `v1.0` | [git tags](https://developer.github.com/v3/git/tags/)
61+
| `References` | reference name, e.g. `heads/master` (note: omits leading `refs/`) | [git tags](https://developer.github.com/v3/git/refs/)
62+
5763

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

@@ -182,8 +188,28 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
182188
| `starred_gists()` | `Tuple{Vector{Gist}, Dict}` | [get the starred `gist`s](https://developer.github.com/v3/gists/#list-starred-gists) |
183189
| `unstar_gist(gist)` | `HTTP.Response` | [unstar `gist`](https://developer.github.com/v3/gists/#unstar-a-gist) |
184190

185-
#### GitHub Apps
191+
#### Git Data
186192

193+
| method | return type | documentation |
194+
|------------------------------------------|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
195+
| `blob(repo, sha)` | `Blob` | [Look up a blob in the `repo` by its SHA](https://developer.github.com/v3/git/blobs/#get-a-blob) |
196+
| `create_blob(repo)` | `Blob` | [Create a blob in the `repo`](https://developer.github.com/v3/git/blobs/#create-a-blob) |
197+
| `gitcommit(repo, sha)` | `GitCommit` | [Look up a commit in the `repo` by its SHA](https://developer.github.com/v3/git/commits/#get-a-commit) |
198+
| `create_gitcommit(repo)` | `GitCommit` | [Create a commit in the `repo`](https://developer.github.com/v3/git/commits/#create-a-commit) |
199+
| `tree(repo, sha)` | `Tree` | [Look up a tree in the `repo` by its SHA](https://developer.github.com/v3/git/trees/#get-a-tree) |
200+
| `create_tree(repo)` | `Tree` | [Create a tree in the `repo`](https://developer.github.com/v3/git/trees/create-a-tree) |
201+
| `tag(repo, sha)` | `Tag` | [Look up a tag in the `repo` by its name](https://developer.github.com/v3/git/tag/#get-a-tag) |
202+
| `create_tag(repo)` | `Tag` | [Create a tag in the `repo`](
203+
https://developer.github.com/v3/git/tag/#create-a-tag) |
204+
| `reference(repo, name)` | `Reference` | [Look up a ref in the `repo` by its name](https://developer.github.com/v3/git/refs/#get-a-reference) |
205+
| `references(repo)` | `Vector{Reference}` | [Get all `refs ` of the repo](https://developer.github.com/v3/git/refs/#get-all-references) |
206+
| `create_reference(repo)` | `Reference` | [Create a reference in the `repo`](https://developer.github.com/v3/git/refs/#create-a-reference) |
207+
| `update_reference(repo)` | `Reference` | [Update a reference in the `repo`](https://developer.github.com/v3/git/refs/#create-a-reference) |
208+
| `delete_reference(repo)` | `GitCommit` | [Delete a the `repo`](https://developer.github.com/v3/git/refs/#delete-a-reference) |
209+
| `tag(repo)` | `Reference` | [Update a reference in the `repo`](https://developer.github.com/v3/git/refs/#create-a-reference) |
210+
| `delete_reference(repo)` | `GitCommit` | [Delete a the `repo`](https://developer.github.com/v3/git/refs/#delete-a-reference) |
211+
212+
#### GitHub Apps
187213
| method | return type | documentation |
188214
|------------------------------------------|------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
189215
| `app(id)` | `App` | [get the GitHub app with the specified `id`](https://developer.github.com/v3/apps/#get-a-single-github-app) |
@@ -192,6 +218,7 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
192218
| `installations(auth)` | `Vector{Installation}` | [get the installations for the GitHub app authenticated by the corresponding `auth`](https://developer.github.com/v3/apps/#find-installations) |
193219
| `repos(i::Installation)` | `Tuple{Vector{Repo}, Dict}` | [get the active repositories for this installation](https://developer.github.com/v3/apps/#find-installations) |
194220

221+
195222
#### Miscellaneous
196223

197224
| method | return type | documentation |

src/git/gitcommit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GitCommit(data::Dict) = json2github(GitCommit, data)
1313
namefield(gitcommit::GitCommit) = gitcommit.sha
1414

1515
@api_default function gitcommit(api::GitHubAPI, repo, commit_obj; options...)
16-
result = gh_post_json(api, "/repos/$(name(repo))/git/commits/$(name(commit_obj))"; options...)
16+
result = gh_get_json(api, "/repos/$(name(repo))/git/commits/$(name(commit_obj))"; options...)
1717
return GitCommit(result)
1818
end
1919

src/git/tag.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ Tag(data::Dict) = json2github(Tag, data)
1212
namefield(tag::Tag) = tag.sha
1313

1414
@api_default function tag(api::GitHubAPI, repo, tag_obj; options...)
15-
result = gh_post_json(api, "/repos/$(name(repo))/git/tags/$(name(tag_obj))"; options...)
15+
result = gh_get_json(api, "/repos/$(name(repo))/git/tags/$(name(tag_obj))"; options...)
1616
return Tag(result)
1717
end
1818

1919
@api_default function create_tag(api::GitHubAPI, repo; options...)
2020
result = gh_post_json(api, "/repos/$(name(repo))/git/tags"; options...)
2121
return Tag(result)
2222
end
23-

test/ghtype_tests.jl

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,3 +781,141 @@ end
781781
review_result = App(review_json)
782782
@test name(review_result) == Int(review_json["id"])
783783
end
784+
785+
@testset "Blob" begin
786+
blob_json = JSON.parse("""
787+
{
788+
"content": "Q29udGVudCBvZiB0aGUgYmxvYg==\\n",
789+
"encoding": "base64",
790+
"url": "https://api.github.com/repos/octocat/example/git/blobs/3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15",
791+
"sha": "3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15",
792+
"size": 19
793+
}
794+
""")
795+
796+
blob_result = Blob(blob_json)
797+
@test name(blob_result) == blob_json["sha"]
798+
end
799+
800+
@testset "Git Commit" begin
801+
commit_json = JSON.parse("""
802+
{
803+
"sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
804+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
805+
"author": {
806+
"date": "2014-11-07T22:01:45Z",
807+
"name": "Scott Chacon",
808+
"email": "[email protected]"
809+
},
810+
"committer": {
811+
"date": "2014-11-07T22:01:45Z",
812+
"name": "Scott Chacon",
813+
"email": "[email protected]"
814+
},
815+
"message": "added readme, because im a good github citizen",
816+
"tree": {
817+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
818+
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
819+
},
820+
"parents": [
821+
{
822+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
823+
"sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
824+
}
825+
],
826+
"verification": {
827+
"verified": false,
828+
"reason": "unsigned",
829+
"signature": null,
830+
"payload": null
831+
}
832+
}
833+
""")
834+
commit_result = GitCommit(commit_json)
835+
@test name(commit_result) == commit_json["sha"]
836+
end
837+
838+
@testset "Reference" begin
839+
reference_json = JSON.parse("""
840+
{
841+
"ref": "refs/heads/featureA",
842+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/featureA",
843+
"object": {
844+
"type": "commit",
845+
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
846+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
847+
}
848+
}
849+
""")
850+
851+
reference_result = Reference(reference_json)
852+
@test name(reference_result) == "heads/featureA"
853+
end
854+
855+
@testset "Tag" begin
856+
tag_json = JSON.parse("""
857+
{
858+
"tag": "v0.0.1",
859+
"sha": "940bd336248efae0f9ee5bc7b2d5c985887b16ac",
860+
"url": "https://api.github.com/repos/octocat/Hello-World/git/tags/940bd336248efae0f9ee5bc7b2d5c985887b16ac",
861+
"message": "initial version",
862+
"tagger": {
863+
"name": "Scott Chacon",
864+
"email": "[email protected]",
865+
"date": "2014-11-07T22:01:45Z"
866+
},
867+
"object": {
868+
"type": "commit",
869+
"sha": "c3d0be41ecbe669545ee3e94d31ed9a4bc91ee3c",
870+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/c3d0be41ecbe669545ee3e94d31ed9a4bc91ee3c"
871+
},
872+
"verification": {
873+
"verified": false,
874+
"reason": "unsigned",
875+
"signature": null,
876+
"payload": null
877+
}
878+
}
879+
""")
880+
881+
tag_result = Tag(tag_json)
882+
@test name(tag_result) == tag_json["sha"]
883+
end
884+
885+
@testset "Tree" begin
886+
tree_json = JSON.parse("""
887+
{
888+
"sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
889+
"url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
890+
"tree": [
891+
{
892+
"path": "file.rb",
893+
"mode": "100644",
894+
"type": "blob",
895+
"size": 30,
896+
"sha": "44b4fc6d56897b048c772eb4087f854f46256132",
897+
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132"
898+
},
899+
{
900+
"path": "subdir",
901+
"mode": "040000",
902+
"type": "tree",
903+
"sha": "f484d249c660418515fb01c2b9662073663c242e",
904+
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e"
905+
},
906+
{
907+
"path": "exec_file",
908+
"mode": "100755",
909+
"type": "blob",
910+
"size": 75,
911+
"sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
912+
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057"
913+
}
914+
],
915+
"truncated": false
916+
}
917+
""")
918+
919+
tree_result = Tree(tree_json)
920+
@test name(tree_result) == tree_json["sha"]
921+
end

test/read_only_api_tests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,31 @@ testbot_key =
176176
@test length(installations(jwt)[1]) == 1
177177
end
178178

179+
@testset "Git Data" begin
180+
github_jl = Repo("JuliaWeb/GitHub.jl")
181+
182+
g = gitcommit(github_jl, "0d9f04ce4be061d3c2b12644316a232c8f889b44"; auth=auth)
183+
@test get(g.tree)["sha"] == "e22fee36cb13d9a1850b242f79938458221a5d2e"
184+
185+
t = tree(github_jl, get(g.tree)["sha"]; auth=auth)
186+
for entry in get(t.tree)
187+
if entry["path"] == "README.md"
188+
@test entry["sha"] == "95c8d1aa2a7b1e6d672e15b67e0df4abbe57dcbe"
189+
@test entry["type"] == "blob"
190+
191+
b = blob(github_jl, entry["sha"]; auth=auth)
192+
@test contains(String(base64decode(replace(get(b.content),"\n",""))), "GitHub.jl")
193+
194+
break
195+
end
196+
end
197+
end
198+
199+
@testset "Tags and References" begin
200+
github_jl = Repo("JuliaWeb/GitHub.jl")
201+
ref = reference(github_jl, "heads/master"; auth=auth)
202+
@test get(ref.object)["type"] == "commit"
203+
204+
# All tags in this repo are lightweight tags which are not covered by the API
205+
# Maybe test in the future when we have a use case
206+
end

0 commit comments

Comments
 (0)