From 35de1cc667c761cc5cbaf78ef210c25692f5a64d Mon Sep 17 00:00:00 2001 From: Paresh Mathur Date: Thu, 24 Nov 2022 10:52:10 +0100 Subject: [PATCH] Add method to get commits from a PR (#190) Co-authored-by: Tim Besard --- README.md | 3 +++ src/repositories/commits.jl | 21 +++++++++++++++++++-- test/read_only_api_tests.jl | 22 ++++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9062d9d..79b7705 100644 --- a/README.md +++ b/README.md @@ -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) | @@ -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) | @@ -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) | diff --git a/src/repositories/commits.jl b/src/repositories/commits.jl index 0f80689..adb45f2 100644 --- a/src/repositories/commits.jl +++ b/src/repositories/commits.jl @@ -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 \ No newline at end of file diff --git a/test/read_only_api_tests.jl b/test/read_only_api_tests.jl index a303b58..ebce193 100644 --- a/test/read_only_api_tests.jl +++ b/test/read_only_api_tests.jl @@ -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)) @@ -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")