Skip to content

Commit

Permalink
feat: add dev.azure adapter
Browse files Browse the repository at this point in the history
as azure doesn't support filters
  • Loading branch information
stakach committed May 17, 2022
1 parent be6fadb commit 1798181
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
7 changes: 6 additions & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: git-repository
version: 1.1.4
version: 1.2.0

dependencies:
connect-proxy:
github: spider-gazelle/connect-proxy
version: ~> 2.0

development_dependencies:
ameba:
Expand Down
26 changes: 26 additions & 0 deletions spec/dev-azure_spec.cr
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
5 changes: 3 additions & 2 deletions src/git-repository.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ module GitRepository
password = uri.password || password

case downcased_host
# case "www.github.com", "github.com"
# once we add specific providers
when "dev.azure.com"
GitRepository::Adapters::DevAzure.new(uri.to_s, username, password, branch.presence)
else
GitRepository::Generic.new(uri.to_s, username, password, branch.presence)
end
end
end

require "./git-repository/generic"
require "./git-repository/adapters/dev-azure"
78 changes: 78 additions & 0 deletions src/git-repository/adapters/dev-azure.cr
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

0 comments on commit 1798181

Please sign in to comment.