Skip to content

Commit

Permalink
Merge pull request #144 from VitorHP/tags-api
Browse files Browse the repository at this point in the history
Implemented Tags API based on the current Branches API
  • Loading branch information
hirakiuc authored Nov 6, 2022
2 parents bbba4e8 + 832a254 commit 51dce18
Show file tree
Hide file tree
Showing 17 changed files with 901 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/tinybucket/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Api
[
:BaseApi,
:BranchesApi,
:TagsApi,
:BranchRestrictionsApi,
:BuildStatusApi,
:CommitsApi,
Expand Down
16 changes: 16 additions & 0 deletions lib/tinybucket/api/commits_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ def branch(name, options = {})
get_parser(:collection, Tinybucket::Model::Commit)
)
end

# Send 'GET commits for a tag' request
#
# @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/commits
# GET an individual commit
#
# @param name [String] The branch name or a SHA1 value for the commit.
# @param options [Hash]
# @return [Tinybucket::Model::Commit]
def tag(name, options = {})
get_path(
path_to_tag(name),
options,
get_parser(:collection, Tinybucket::Model::Commit)
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/tinybucket/api/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Helper
:ApiHelper,
:BranchesHelper,
:BranchRestrictionsHelper,
:TagsHelper,
:BuildStatusHelper,
:CommitsHelper,
:CommentsHelper,
Expand Down
7 changes: 7 additions & 0 deletions lib/tinybucket/api/helper/commits_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def path_to_branch(branch)
[branch, 'branch'])
end

def path_to_tag(tag)
build_path(base_path,
'commits',
[tag, 'tag'])
end


def path_to_approve(revision)
build_path(base_path,
'commit',
Expand Down
27 changes: 27 additions & 0 deletions lib/tinybucket/api/helper/tags_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Tinybucket
module Api
module Helper
module TagsHelper
include ::Tinybucket::Api::Helper::ApiHelper

private

def path_to_list
build_path(base_path)
end

def path_to_find(tag)
build_path(base_path,
[tag, 'tag'])
end

def base_path
build_path('/repositories',
[repo_owner, 'repo_owner'],
[repo_slug, 'repo_slug'],
'refs', 'tags')
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/tinybucket/api/tags_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Tinybucket
module Api
# Tags Api client
#
# @!attribute [rw] repo_owner
# @return [String] repository owner name.
# @!attribute [rw] repo_slug
# @return [String] {https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D repo_slug}.
class TagsApi < BaseApi
include Tinybucket::Api::Helper::TagsHelper

attr_accessor :repo_owner, :repo_slug

# Send 'GET a tags list for a repository' request
#
# @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/tags#get
# GET a tags list for a repository
#
# @param options [Hash]
# @return [Tinybucket::Model::Page]
def list(options = {})
get_path(
path_to_list,
options,
get_parser(:collection, Tinybucket::Model::Tag)
)
end

# Send 'GET an individual tag' request
#
# @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/tags/%7Bname%7D#get
# GET an individual tag
#
# @param name [String] The tag name
# @param options [Hash]
# @return [Tinybucket::Model::Tag]
def find(name, options = {})
get_path(
path_to_find(name),
options,
get_parser(:object, Tinybucket::Model::Tag)
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/tinybucket/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Model
:Project,
:PullRequest,
:Repository,
:Tag,
:Team
].each do |klass_name|
autoload klass_name
Expand Down
21 changes: 21 additions & 0 deletions lib/tinybucket/model/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ def branch(branch, options = {})
branches_resource.find(branch, options)
end

# Get tags on this repository
#
# @param options [Hash]
# @return [Tinybucket::Resource::Tags]
def tags(options = {})
tags_resource(options)
end

# Get the specific tag on this repository.
#
# @param branch [String]
# @param options [Hash]
# @return [Tinybucket::Model::Tag]
def tag(tag, options = {})
tags_resource.find(tag, options)
end

# Get the branch restriction information associated with this repository.
#
# @param options [Hash]
Expand Down Expand Up @@ -183,6 +200,10 @@ def branches_resource(options = {})
Tinybucket::Resource::Branches.new(self, options)
end

def tags_resource(options = {})
Tinybucket::Resource::Tags.new(self, options)
end

def commits_resource(options = {})
Tinybucket::Resource::Commits.new(self, options)
end
Expand Down
48 changes: 48 additions & 0 deletions lib/tinybucket/model/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Tinybucket
module Model
# Tag
#
# @see https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/tags
# Tag Resource
#
# @!attribute [rw] links
# @return [Hash]
# @!attribute [rw] type
# @return [String]
# @!attribute [rw] name
# @return [String]
# @!attribute [rw] repository
# @return [Hash]
# @!attribute [rw] target
# @return [Hash]
class Tag < Base
include Tinybucket::Model::Concerns::RepositoryKeys

acceptable_attributes :links, :type, :name, :repository, :target, :tagger, :date, :message

# Returns the commits available for the specific tag
#
# @param options [Hash]
# @return [Tinybucket::Resource::Commits]
def commits(options = {})
commits_resource.tag(name, options)
end

private

def commits_resource(options = {})
Tinybucket::Resource::Commits.new(self, options)
end

def tags_api
create_api('Tags', repo_keys)
end

def load_model
tags_api.find(name, {})
end
end
end
end
1 change: 1 addition & 0 deletions lib/tinybucket/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Resource
:PullRequests,
:Repos,
:Teams,
:Tags,
:Watchers
].each do |klass_name|
autoload klass_name
Expand Down
11 changes: 11 additions & 0 deletions lib/tinybucket/resource/commits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ def branch(name, options = {})
end
end

# Returns the commits for a specific tag
#
# @param name [String]
# @param options [Hash]
# @return [Tinybucket::Iterator]
def tag(name, options = {})
create_enumerator(commits_api, :tag, name, options) do |m|
inject_repo_keys(m, @repo.repo_keys)
end
end

private

def commits_api
Expand Down
35 changes: 35 additions & 0 deletions lib/tinybucket/resource/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Tinybucket
module Resource
class Tags < Tinybucket::Resource::Base
def initialize(repo, options)
@repo = repo
@args = [options]
end

# Find the tag
#
# @param tag [String]
# @param options [Hash]
# @return [Tinybucket::Model::Tag]
def find(tag, options = {})
tags_api.find(tag, options).tap do |m|
inject_repo_keys(m, @repo.repo_keys)
end
end

private

def tags_api
create_api('Tags', @repo.repo_keys)
end

def enumerator
create_enumerator(tags_api, :list, *@args) do |m|
inject_repo_keys(m, @repo.repo_keys)
end
end
end
end
end
Loading

0 comments on commit 51dce18

Please sign in to comment.