Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
209f2f1e
Commit
209f2f1e
authored
Apr 25, 2016
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a similar approach to branch creation for tag creation
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
44f89eaf
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
53 deletions
+102
-53
app/models/repository.rb
app/models/repository.rb
+10
-7
app/services/create_branch_service.rb
app/services/create_branch_service.rb
+0
-5
app/services/create_tag_service.rb
app/services/create_tag_service.rb
+15
-31
spec/models/repository_spec.rb
spec/models/repository_spec.rb
+24
-10
spec/services/create_tag_service_spec.rb
spec/services/create_tag_service_spec.rb
+53
-0
No files found.
app/models/repository.rb
View file @
209f2f1e
...
@@ -146,17 +146,20 @@ class Repository
...
@@ -146,17 +146,20 @@ class Repository
find_branch
(
branch_name
)
find_branch
(
branch_name
)
end
end
def
add_tag
(
user
,
tag_name
,
ref
,
message
=
nil
)
def
add_tag
(
user
,
tag_name
,
target
,
message
=
nil
)
before_push_tag
oldrev
=
Gitlab
::
Git
::
BLANK_SHA
ref
=
Gitlab
::
Git
::
TAG_REF_PREFIX
+
tag_name
target
=
commit
(
target
).
try
(
:id
)
return
false
unless
target
options
=
{
message:
message
,
tagger:
user_to_committer
(
user
)
}
if
message
options
=
{
message:
message
,
tagger:
user_to_committer
(
user
)
}
if
message
tag
=
rugged
.
tags
.
create
(
tag_name
,
ref
,
options
)
GitHooksService
.
new
.
execute
(
user
,
path_to_repo
,
oldrev
,
target
,
ref
)
do
if
tag
.
annotated?
rugged
.
tags
.
create
(
tag_name
,
target
,
options
)
Gitlab
::
Git
::
Tag
.
new
(
tag_name
,
ref
,
tag
.
annotation
.
message
)
else
Gitlab
::
Git
::
Tag
.
new
(
tag_name
,
ref
)
end
end
find_tag
(
tag_name
)
end
end
def
rm_branch
(
user
,
branch_name
)
def
rm_branch
(
user
,
branch_name
)
...
...
app/services/create_branch_service.rb
View file @
209f2f1e
...
@@ -43,9 +43,4 @@ class CreateBranchService < BaseService
...
@@ -43,9 +43,4 @@ class CreateBranchService < BaseService
out
[
:branch
]
=
branch
out
[
:branch
]
=
branch
out
out
end
end
def
build_push_data
(
project
,
user
,
branch
)
Gitlab
::
PushDataBuilder
.
build
(
project
,
user
,
Gitlab
::
Git
::
BLANK_SHA
,
branch
.
target
,
"
#{
Gitlab
::
Git
::
BRANCH_REF_PREFIX
}#{
branch
.
name
}
"
,
[])
end
end
end
app/services/create_tag_service.rb
View file @
209f2f1e
require_relative
'base_service'
require_relative
'base_service'
class
CreateTagService
<
BaseService
class
CreateTagService
<
BaseService
def
execute
(
tag_name
,
ref
,
message
,
release_description
=
nil
)
def
execute
(
tag_name
,
target
,
message
,
release_description
=
nil
)
valid_tag
=
Gitlab
::
GitRefValidator
.
validate
(
tag_name
)
valid_tag
=
Gitlab
::
GitRefValidator
.
validate
(
tag_name
)
if
valid_tag
==
false
return
error
(
'Tag name invalid'
)
unless
valid_tag
return
error
(
'Tag name invalid'
)
end
repository
=
project
.
repository
repository
=
project
.
repository
message
.
strip!
if
message
message
.
strip!
if
message
new_tag
=
nil
begin
begin
new_tag
=
repository
.
add_tag
(
current_user
,
tag_name
,
ref
,
message
)
new_tag
=
repository
.
add_tag
(
current_user
,
tag_name
,
target
,
message
)
rescue
Rugged
::
TagError
rescue
Rugged
::
TagError
return
error
(
"Tag
#{
tag_name
}
already exists"
)
return
error
(
"Tag
#{
tag_name
}
already exists"
)
rescue
Rugged
::
Referenc
eError
rescue
GitHooksService
::
PreReceiv
eError
return
error
(
"Target
#{
ref
}
is invalid"
)
return
error
(
'Tag creation was rejected by Git hook'
)
end
end
push_data
=
create_push_data
(
project
,
current_user
,
new_tag
)
if
new_tag
EventCreateService
.
new
.
push
(
project
,
current_user
,
push_data
)
project
.
execute_hooks
(
push_data
.
dup
,
:tag_push_hooks
)
project
.
execute_services
(
push_data
.
dup
,
:tag_push_hooks
)
CreateCommitBuildsService
.
new
.
execute
(
project
,
current_user
,
push_data
)
if
release_description
if
release_description
CreateReleaseService
.
new
(
@project
,
@current_user
).
CreateReleaseService
.
new
(
@project
,
@current_user
).
execute
(
tag_name
,
release_description
)
execute
(
tag_name
,
release_description
)
end
end
success
.
merge
(
tag:
new_tag
)
success
(
new_tag
)
else
end
error
(
"Target
#{
target
}
is invalid"
)
def
success
(
branch
)
out
=
super
()
out
[
:tag
]
=
branch
out
end
end
def
create_push_data
(
project
,
user
,
tag
)
commits
=
[
project
.
commit
(
tag
.
target
)].
compact
Gitlab
::
PushDataBuilder
.
build
(
project
,
user
,
Gitlab
::
Git
::
BLANK_SHA
,
tag
.
target
,
"
#{
Gitlab
::
Git
::
TAG_REF_PREFIX
}#{
tag
.
name
}
"
,
commits
,
tag
.
message
)
end
end
end
end
spec/models/repository_spec.rb
View file @
209f2f1e
...
@@ -858,17 +858,31 @@ describe Repository, models: true do
...
@@ -858,17 +858,31 @@ describe Repository, models: true do
end
end
describe
'#add_tag'
do
describe
'#add_tag'
do
it
'adds a tag'
do
context
'with a valid target'
do
user
=
build_stubbed
(
:user
)
let
(
:user
)
{
build_stubbed
(
:user
)
}
expect
(
repository
).
to
receive
(
:before_push_tag
)
it
'creates the tag using rugged'
do
expect
(
repository
.
rugged
.
tags
).
to
receive
(
:create
).
expect
(
repository
.
rugged
.
tags
).
to
receive
(
:create
).
with
(
'8.5'
,
'master'
,
with
(
'8.5'
,
repository
.
commit
(
'master'
).
id
,
hash_including
(
message:
'foo'
,
hash_including
(
message:
'foo'
,
tagger:
hash_including
(
name:
user
.
name
,
email:
user
.
email
))).
tagger:
hash_including
(
name:
user
.
name
,
email:
user
.
email
))).
and_call_original
and_call_original
repository
.
add_tag
(
user
,
'8.5'
,
'master'
,
'foo'
)
repository
.
add_tag
(
user
,
'8.5'
,
'master'
,
'foo'
)
end
end
it
'returns a Gitlab::Git::Tag object'
do
tag
=
repository
.
add_tag
(
user
,
'8.5'
,
'master'
,
'foo'
)
expect
(
tag
).
to
be_a
(
Gitlab
::
Git
::
Tag
)
end
end
context
'with an invalid target'
do
it
'returns false'
do
expect
(
repository
.
add_tag
(
user
,
'8.5'
,
'bar'
,
'foo'
)).
to
be
false
end
end
end
end
describe
'#rm_branch'
do
describe
'#rm_branch'
do
...
...
spec/services/create_tag_service_spec.rb
0 → 100644
View file @
209f2f1e
require
'spec_helper'
describe
CreateTagService
,
services:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:repository
)
{
project
.
repository
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:service
)
{
described_class
.
new
(
project
,
user
)
}
describe
'#execute'
do
it
'creates the tag and returns success'
do
response
=
service
.
execute
(
'v42.42.42'
,
'master'
,
'Foo'
)
expect
(
response
[
:status
]).
to
eq
(
:success
)
expect
(
response
[
:tag
]).
to
be_a
Gitlab
::
Git
::
Tag
expect
(
response
[
:tag
].
name
).
to
eq
(
'v42.42.42'
)
end
context
'when target is invalid'
do
it
'returns an error'
do
response
=
service
.
execute
(
'v1.1.0'
,
'foo'
,
'Foo'
)
expect
(
response
).
to
eq
(
status: :error
,
message:
'Target foo is invalid'
)
end
end
context
'when tag already exists'
do
it
'returns an error'
do
expect
(
repository
).
to
receive
(
:add_tag
).
with
(
user
,
'v1.1.0'
,
'master'
,
'Foo'
).
and_raise
(
Rugged
::
TagError
)
response
=
service
.
execute
(
'v1.1.0'
,
'master'
,
'Foo'
)
expect
(
response
).
to
eq
(
status: :error
,
message:
'Tag v1.1.0 already exists'
)
end
end
context
'when pre-receive hook fails'
do
it
'returns an error'
do
expect
(
repository
).
to
receive
(
:add_tag
).
with
(
user
,
'v1.1.0'
,
'master'
,
'Foo'
).
and_raise
(
GitHooksService
::
PreReceiveError
)
response
=
service
.
execute
(
'v1.1.0'
,
'master'
,
'Foo'
)
expect
(
response
).
to
eq
(
status: :error
,
message:
'Tag creation was rejected by Git hook'
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment