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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
b7fa7c4d
Commit
b7fa7c4d
authored
Apr 01, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extend build status badge, add html/markdown methods
parent
88fc7ccd
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
8 deletions
+63
-8
app/controllers/projects/badges_controller.rb
app/controllers/projects/badges_controller.rb
+2
-0
app/views/projects/badges/index.html.haml
app/views/projects/badges/index.html.haml
+3
-3
lib/gitlab/badge/build.rb
lib/gitlab/badge/build.rb
+26
-4
spec/lib/gitlab/badge/build_spec.rb
spec/lib/gitlab/badge/build_spec.rb
+32
-1
No files found.
app/controllers/projects/badges_controller.rb
View file @
b7fa7c4d
...
@@ -2,6 +2,8 @@ class Projects::BadgesController < Projects::ApplicationController
...
@@ -2,6 +2,8 @@ class Projects::BadgesController < Projects::ApplicationController
before_action
:no_cache_headers
,
except:
[
:index
]
before_action
:no_cache_headers
,
except:
[
:index
]
def
index
def
index
@ref
=
params
[
:ref
]
||
'master'
@badge
=
Gitlab
::
Badge
::
Build
.
new
(
@project
,
@ref
)
end
end
def
build
def
build
...
...
app/views/projects/badges/index.html.haml
View file @
b7fa7c4d
...
@@ -4,12 +4,12 @@
...
@@ -4,12 +4,12 @@
.panel.panel-default
.panel.panel-default
.panel-heading
.panel-heading
%b
Builds badge
·
%b
Builds badge
·
=
image_tag
(
build_namespace_project_badges_path
(
@project
.
namespace
,
@project
,
:master
,
format: :svg
),
alt:
'Builds badge'
)
=
@badge
.
to_html
.panel-body
.panel-body
%table
.table
%table
.table
%tr
%tr
%td
Markdown
%td
Markdown
%td
=
markdown
(
"```markdown
\n
[![build status](url)](link)
\n
```"
)
%td
=
markdown
(
"```markdown
\n
#{
@badge
.
to_markdown
}
\n
```"
)
%tr
%tr
%td
HTML
%td
HTML
%td
=
markdown
(
"```html
\n
<a href='link'><img src='url' /></a>
\n
```"
)
%td
=
markdown
(
"```html
\n
#{
@badge
.
to_html
}
\n
```"
)
lib/gitlab/badge/build.rb
View file @
b7fa7c4d
...
@@ -4,14 +4,15 @@ module Gitlab
...
@@ -4,14 +4,15 @@ module Gitlab
# Build badge
# Build badge
#
#
class
Build
class
Build
include
Gitlab
::
Application
.
routes
.
url_helpers
include
ActionView
::
Helpers
::
AssetTagHelper
include
ActionView
::
Helpers
::
UrlHelper
def
initialize
(
project
,
ref
)
def
initialize
(
project
,
ref
)
@project
,
@ref
=
project
,
ref
@image
=
::
Ci
::
ImageForBuildService
.
new
.
execute
(
project
,
ref:
ref
)
@image
=
::
Ci
::
ImageForBuildService
.
new
.
execute
(
project
,
ref:
ref
)
end
end
def
to_s
@image
[
:name
].
sub
(
/\.svg$/
,
''
)
end
def
type
def
type
'image/svg+xml'
'image/svg+xml'
end
end
...
@@ -19,6 +20,27 @@ module Gitlab
...
@@ -19,6 +20,27 @@ module Gitlab
def
data
def
data
File
.
read
(
@image
[
:path
])
File
.
read
(
@image
[
:path
])
end
end
def
to_s
@image
[
:name
].
sub
(
/\.svg$/
,
''
)
end
def
to_html
link_to
(
image_tag
(
image_url
,
alt:
'build status'
),
link_url
)
end
def
to_markdown
"[![build status](
#{
image_url
}
)](
#{
link_url
}
)"
end
def
image_url
build_namespace_project_badges_url
(
@project
.
namespace
,
@project
,
@ref
,
format: :svg
)
end
def
link_url
namespace_project_commits_url
(
@project
.
namespace
,
@project
,
id:
@ref
)
end
end
end
end
end
end
end
spec/lib/gitlab/badge/build_spec.rb
View file @
b7fa7c4d
...
@@ -3,13 +3,44 @@ require 'spec_helper'
...
@@ -3,13 +3,44 @@ require 'spec_helper'
describe
Gitlab
::
Badge
::
Build
do
describe
Gitlab
::
Badge
::
Build
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:sha
)
{
project
.
commit
.
sha
}
let
(
:sha
)
{
project
.
commit
.
sha
}
let
(
:badge
)
{
described_class
.
new
(
project
,
'master'
)
}
let
(
:branch
)
{
'master'
}
let
(
:badge
)
{
described_class
.
new
(
project
,
branch
)
}
describe
'#type'
do
describe
'#type'
do
subject
{
badge
.
type
}
subject
{
badge
.
type
}
it
{
is_expected
.
to
eq
'image/svg+xml'
}
it
{
is_expected
.
to
eq
'image/svg+xml'
}
end
end
describe
'#to_html'
do
let
(
:html
)
{
Nokogiri
::
HTML
.
parse
(
badge
.
to_html
)
}
let
(
:a_href
)
{
html
.
at
(
'a'
)
}
it
'points to link'
do
expect
(
a_href
[
:href
]).
to
eq
badge
.
link_url
end
it
'contains clickable image'
do
expect
(
a_href
.
children
.
first
.
name
).
to
eq
'img'
end
end
describe
'#to_markdown'
do
subject
{
badge
.
to_markdown
}
it
{
is_expected
.
to
include
badge
.
image_url
}
it
{
is_expected
.
to
include
badge
.
link_url
}
end
describe
'#image_url'
do
subject
{
badge
.
image_url
}
it
{
is_expected
.
to
include
"badges/
#{
branch
}
/build.svg"
}
end
describe
'#link_url'
do
subject
{
badge
.
link_url
}
it
{
is_expected
.
to
include
"commits/
#{
branch
}
"
}
end
context
'build exists'
do
context
'build exists'
do
let
(
:ci_commit
)
{
create
(
:ci_commit
,
project:
project
,
sha:
sha
)
}
let
(
:ci_commit
)
{
create
(
:ci_commit
,
project:
project
,
sha:
sha
)
}
let!
(
:build
)
{
create
(
:ci_build
,
commit:
ci_commit
)
}
let!
(
:build
)
{
create
(
:ci_build
,
commit:
ci_commit
)
}
...
...
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