Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to get commits from a PR #190

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -108,6 +109,7 @@ GitHub.jl implements a bunch of methods that make REST requests to GitHub's API.
| `stats(repo, stat[, attempts = 3])` | `HTTP.Response` | [get information on `stat` (e.g. "contributors", "code_frequency", "commit_activity", etc.)](https://developer.github.com/v3/repos/statistics/) |
| `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 @@ -136,6 +138,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