Skip to content

Commit

Permalink
Add support for comparing commits.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Nov 24, 2022
1 parent 809994e commit b1c762a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
5 changes: 5 additions & 0 deletions src/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------

Expand Down Expand Up @@ -143,6 +144,10 @@ export # commits.jl
commit,
commits

export # compare.jl
Comparison,
compare

export # branches.jl
Branch,
branch,
Expand Down
28 changes: 28 additions & 0 deletions src/repositories/compare.jl
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/read_only_api_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit b1c762a

Please sign in to comment.