Skip to content

Commit

Permalink
Add method to get commits from a PR (#190)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Besard <tim.besard@gmail.com>
  • Loading branch information
rick2047 and maleadt authored Nov 24, 2022
1 parent d7be401 commit 35de1cc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Here's a table that matches up the provided `GitHubType`s with their correspondi
| `Label` | name, e.g. `bug` | [issue labels](https://docs.github.com/en/rest/reference/issues#labels)
| `Status` | id, e.g. `366961773` | [commit statuses](https://developer.github.com/v3/repos/statuses/) |
| `PullRequest` | number, e.g. `44` | [pull requests](https://developer.github.com/v3/pulls/) |
| `PullRequestFile` | filename, e.g. `file1.txt` | [pull request files](https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files) |
| `Issue` | number, e.g. `31` | [issues](https://developer.github.com/v3/issues/) |
| `Team` | id, e.g. `1` | [teams](https://developer.github.com/v3/orgs/teams) |
| `Gist` | id, e.g. `0bace7cc774df4b3a4b0ee9aaa271ef6` | [gists](https://developer.github.com/v3/gists) |
Expand Down Expand Up @@ -110,6 +111,7 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
| `set_topics(repo, topics)` | `Vector{String}` | [set the list of topics of a repository.)](https://docs.github.com/en/rest/repos/repos#replace-all-repository-topics) |
| `commit(repo, sha)` | `Commit` | [get the commit specified by `sha`](https://developer.github.com/v3/repos/commits/#get-a-single-commit) |
| `commits(repo)` | `Tuple{Vector{Commit}, Dict}` | [get `repo`'s commits](https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository) |
| `commits(repo, pr)` | `Tuple{Vector{Commit}, Dict}` | [get `pr`'s commits for `repo`](https://docs.github.com/en/rest/reference/pulls#list-commits-on-a-pull-request) |
| `branch(repo, branch)` | `Branch` | [get the branch specified by `branch`](https://developer.github.com/v3/repos/#get-branch) |
| `branches(repo)` | `Tuple{Vector{Branch}, Dict}` | [get `repo`'s branches](https://developer.github.com/v3/repos/#list-branches) |
| `file(repo, path)` | `Content` | [get the file specified by `path`](https://developer.github.com/v3/repos/contents/#get-contents) |
Expand Down Expand Up @@ -138,6 +140,7 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
|---------------------------------|------------------------------------|------------------------------------------------------------------------------------------------------------|
| `pull_request(repo, pr)` | `PullRequest` | [get the pull request specified by `pr`](https://developer.github.com/v3/pulls/#get-a-single-pull-request) |
| `pull_requests(repo)` | `Tuple{Vector{PullRequest}, Dict}` | [get `repo`'s pull requests](https://developer.github.com/v3/pulls/#list-pull-requests) |
| `pull_request_files(repo, pr)` | `Tuple{Vector{PullRequestFiles}, Dict}` | [get this `repo`'s `pr`'s file changes](https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files) |
| `create_pull_request(repo)` | `PullRequest` | [create pull request in `repo`](https://developer.github.com/v3/pulls/#create-a-pull-request) |
| `update_pull_request(repo, pr)` | `PullRequest` | [update the given `pr` in `repo`](https://developer.github.com/v3/pulls/#update-a-pull-request) |
| `close_pull_request(repo, pr)` | `PullRequest` | [close the given `pr` in `repo`](https://developer.github.com/v3/pulls/#update-a-pull-request) |
Expand Down
21 changes: 19 additions & 2 deletions src/repositories/commits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ namefield(commit::Commit) = commit.sha
# API Methods #
###############

@api_default function commits(api::GitHubAPI, repo; options...)
# repo #
#------#

@api_default function commits(api::GitHubAPI, repo::Union{Repo,String}; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/commits"; options...)
return map(Commit, results), page_data
end

@api_default function commit(api::GitHubAPI, repo, sha; options...)
@api_default function commit(api::GitHubAPI, repo, sha::Union{Commit,String}; options...)
result = gh_get_json(api, "/repos/$(name(repo))/commits/$(name(sha))"; options...)
return Commit(result)
end

# pull request #
#--------------#

@api_default function commits(api::GitHubAPI, pr; options...)
repo = pr.base.repo
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/pulls/$(name(pr))/commits"; options...)
return map(Commit, results), page_data
end

@api_default function commits(api::GitHubAPI, repo, pr; options...)
results, page_data = gh_get_paged_json(api, "/repos/$(name(repo))/pulls/$(name(pr))/commits"; options...)
return map(Commit, results), page_data
end
22 changes: 18 additions & 4 deletions test/read_only_api_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ end
@test name(branch(ghjl, "master"; auth = auth)) == "master"
@test hasghobj("master", first(branches(ghjl; auth = auth)))

# test GitHub.commit, GitHub.commits
@test name(commit(ghjl, testcommit; auth = auth)) == name(testcommit)
@test hasghobj(testcommit, first(commits(ghjl; auth = auth)))

# test GitHub.file, GitHub.directory, GitHub.readme, GitHub.permalink
readme_file = file(ghjl, "README.md"; auth = auth)
src_dir = first(directory(ghjl, "src"; auth = auth))
Expand Down Expand Up @@ -106,6 +102,24 @@ end
# @test iscollaborator(ghjl, "jrevels"; auth = auth)
end

@testset "Commits" begin
# of a repo
@test name(commit(ghjl, testcommit; auth = auth)) == name(testcommit)
@test hasghobj(testcommit, first(commits(ghjl; auth = auth)))

# of a pull request
let pr = pull_request(ghjl, 37; auth = auth)
commit_vec, page_data = commits(pr; auth = auth)
@test commit_vec isa Vector{Commit}
@test length(commit_vec) == 1
end
let
commit_vec, page_data = commits(ghjl, 37; auth = auth)
@test commit_vec isa Vector{Commit}
@test length(commit_vec) == 1
end
end

@testset "Issues" begin
state_param = Dict("state" => "all")

Expand Down

0 comments on commit 35de1cc

Please sign in to comment.