-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
as azure doesn't support filters
- Loading branch information
Showing
4 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "./spec_helper" | ||
|
||
module GitRepository::Adapters | ||
describe GitRepository::Adapters::DevAzure do | ||
client = DevAzure.new("https://dev.azure.com/steve0261/devops-api/_git/devops-api") | ||
|
||
it "should return the default branch" do | ||
client.default_branch.should eq "master" | ||
end | ||
|
||
it "should return the list of branches" do | ||
branches = client.branches | ||
branches.keys.includes?("master").should eq true | ||
branches.size.should eq 1 | ||
end | ||
|
||
it "should return commits" do | ||
branch_commits = client.commits("master", 5) | ||
branch_commits.size.should eq 2 | ||
file_commits = client.commits("master", "testing.txt", 5) | ||
file_commits.size.should eq 1 | ||
branch_commits = client.commits("master", 1) | ||
branch_commits.size.should eq 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require "connect-proxy" | ||
require "../generic" | ||
|
||
# API Docs: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get-commits?view=azure-devops-rest-5.0#on-a-branch | ||
class GitRepository::Adapters::DevAzure < GitRepository::Generic | ||
getter organization : String | ||
getter project : String | ||
getter repo_id : String | ||
|
||
def initialize(@repository : String, @username : String? = nil, @password : String? = nil, branch : String? = nil) | ||
super | ||
|
||
# https://dev.azure.com/{organization}/{project}/_git/{repositoryId} | ||
uri = URI.parse(@repository) | ||
path_components = URI.decode(uri.path).split('/') | ||
@organization = path_components[1] | ||
@project = path_components[2] | ||
@repo_id = path_components[4] | ||
end | ||
|
||
# no need to cache as we're using an API | ||
protected def build_cache | ||
@use_cache = false | ||
end | ||
|
||
protected def get_commits(branch : String, file : String? = nil, depth : Int? = 50) : Array(Commit) | ||
uri = URI.parse("https://dev.azure.com/#{organization}/#{project}/_apis/git/repositories/#{repo_id}/commits?searchCriteria.itemVersion.version=#{branch}&api-version=5.0") | ||
client = ConnectProxy::HTTPClient.new(uri) | ||
|
||
if username && password | ||
client.basic_auth(username, password) | ||
end | ||
|
||
params = uri.query_params | ||
params["searchCriteria.$top"] = depth.to_s if depth | ||
params["searchCriteria.itemPath"] = "/#{file}" if file | ||
uri.query_params = params | ||
response = client.get(uri.request_target) | ||
|
||
raise GitCommandError.new("dev.azure.com commits API request failed with #{response.status_code}\n#{response.body}") unless response.success? | ||
|
||
Commits.from_json(response.body).to_commits | ||
end | ||
|
||
struct Committer | ||
include JSON::Serializable | ||
|
||
getter name : String | ||
getter email : String? | ||
getter date : String | ||
end | ||
|
||
struct AzureCommit | ||
include JSON::Serializable | ||
|
||
getter author : Committer | ||
getter committer : Committer | ||
|
||
@[JSON::Field(key: "commitId")] | ||
getter hash : String | ||
getter comment : String | ||
|
||
def to_commit | ||
Commit.new(hash, comment, author.name, author.date) | ||
end | ||
end | ||
|
||
struct Commits | ||
include JSON::Serializable | ||
|
||
getter count : Int32 | ||
getter value : Array(AzureCommit) | ||
|
||
def to_commits | ||
value.map &.to_commit | ||
end | ||
end | ||
end |