From b1c762a06e95bbd575d6a4e9e85f368fcb7a60d3 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 23 Nov 2022 16:39:58 +0100 Subject: [PATCH] Add support for comparing commits. --- README.md | 1 + src/GitHub.jl | 5 +++++ src/repositories/compare.jl | 28 ++++++++++++++++++++++++++++ test/read_only_api_tests.jl | 7 +++++++ 4 files changed, 41 insertions(+) create mode 100644 src/repositories/compare.jl diff --git a/README.md b/README.md index fcc6530..ae0a4fa 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,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) | +| `compare(repo, base, head)` | `Comparison` | [compare `repo`'s commits](https://docs.github.com/en/rest/commits/commits#compare-two-commits) | | `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) | diff --git a/src/GitHub.jl b/src/GitHub.jl index cda2cb3..bac6edc 100644 --- a/src/GitHub.jl +++ b/src/GitHub.jl @@ -110,6 +110,7 @@ include("repositories/statuses.jl") include("repositories/webhooks.jl") include("repositories/deploykeys.jl") include("repositories/secrets.jl") +include("repositories/compare.jl") # export ------- @@ -143,6 +144,10 @@ export # commits.jl commit, commits +export # compare.jl + Comparison, + compare + export # branches.jl Branch, branch, diff --git a/src/repositories/compare.jl b/src/repositories/compare.jl new file mode 100644 index 0000000..17fbe2a --- /dev/null +++ b/src/repositories/compare.jl @@ -0,0 +1,28 @@ +################### +# Comparison Type # +################### + +@ghdef mutable struct Comparison + url::Union{URIs.URI, Nothing} + html_url::Union{URIs.URI, Nothing} + permalink_url::Union{URIs.URI, Nothing} + diff_url::Union{URIs.URI, Nothing} + patch_url::Union{URIs.URI, Nothing} + base_commit::Union{Commit, Nothing} + merge_base_commit::Union{Commit, Nothing} + status::Union{String, Nothing} + ahead_by::Union{Int, Nothing} + behind_by::Union{Int, Nothing} + total_commits::Union{Int, Nothing} + commits::Union{Vector{Commit}, Nothing} + files::Union{Vector{Content}, Nothing} +end + +############### +# API Methods # +############### + +@api_default function compare(api::GitHubAPI, repo, base, head; options...) + result = gh_get_json(api, "/repos/$(name(repo))/compare/$(name(base))...$(name(head))"; options...) + return Comparison(result) +end diff --git a/test/read_only_api_tests.jl b/test/read_only_api_tests.jl index 391b371..aa8a4e2 100644 --- a/test/read_only_api_tests.jl +++ b/test/read_only_api_tests.jl @@ -78,6 +78,13 @@ end @test name(commit(ghjl, testcommit; auth = auth)) == name(testcommit) @test hasghobj(testcommit, first(commits(ghjl; auth = auth))) + # test GitHub.compare + @test compare(ghjl, "master", "master~"; auth = auth).behind_by == 1 + let comparison = compare(ghjl, "master~", "master"; auth = auth) + @test comparison.ahead_by == 1 + @test length(comparison.commits) == 1 + end + # 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))