Skip to content

Commit e2ad32c

Browse files
committed
Add support for badges from Gitlab's CI infrastructure
1 parent 073cfd8 commit e2ad32c

File tree

6 files changed

+113
-10
lines changed

6 files changed

+113
-10
lines changed

app/components/badge-gitlab.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Component.extend({
4+
tagName: 'span',
5+
classNames: ['badge'],
6+
repository: Ember.computed.alias('badge.attributes.repository'),
7+
branch: Ember.computed('badge.attributes.branch', function() {
8+
return this.get('badge.attributes.branch') || 'master';
9+
}),
10+
text: Ember.computed('badge', function() {
11+
return `Gitlab build status for the ${ this.get('branch') } branch`;
12+
})
13+
});

app/mirage/fixtures/search.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export default {
3232
"repository": "huonw/external_mixin"
3333
},
3434
"badge_type": "travis-ci"
35+
},
36+
{
37+
"attributes": {
38+
"branch": "master",
39+
"repository": "huonw/external_mixin"
40+
},
41+
"badge_type": "gitlab"
3542
}
3643
],
3744
"versions": null
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<a href="https://gitlab.com/{{ repository }}/pipelines">
2+
<img
3+
src="https://gitlab.com/{{ repository }}/badges/{{ branch }}/build.svg"
4+
alt="{{ text }}"
5+
title="{{ text }}" />
6+
</a>

src/badge.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub enum Badge {
1515
Appveyor {
1616
repository: String, branch: Option<String>, service: Option<String>,
1717
},
18+
Gitlab {
19+
repository: String, branch: Option<String>,
20+
},
1821
}
1922

2023
#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)]
@@ -58,6 +61,19 @@ impl Model for Badge {
5861
database"),
5962
}
6063
},
64+
"gitlab" => {
65+
Badge::Gitlab {
66+
branch: attributes.get("branch")
67+
.and_then(Json::as_string)
68+
.map(str::to_string),
69+
repository: attributes.get("repository")
70+
.and_then(Json::as_string)
71+
.map(str::to_string)
72+
.expect("Invalid Gitlab badge \
73+
without repository in the \
74+
database"),
75+
}
76+
},
6177
_ => {
6278
panic!("Unknown badge type {} in the database", badge_type);
6379
},
@@ -84,6 +100,7 @@ impl Badge {
84100
match *self {
85101
Badge::TravisCi {..} => "travis-ci",
86102
Badge::Appveyor {..} => "appveyor",
103+
Badge::Gitlab{..} => "gitlab",
87104
}
88105
}
89106

@@ -120,7 +137,16 @@ impl Badge {
120137
service
121138
);
122139
}
123-
}
140+
},
141+
Badge::Gitlab { branch, repository } => {
142+
attributes.insert(String::from("repository"), repository);
143+
if let Some(branch) = branch {
144+
attributes.insert(
145+
String::from("branch"),
146+
branch
147+
);
148+
}
149+
},
124150
}
125151

126152
attributes
@@ -156,7 +182,19 @@ impl Badge {
156182
},
157183
None => Err(badge_type.to_string()),
158184
}
159-
},
185+
},
186+
"gitlab" => {
187+
match attributes.get("repository") {
188+
Some(repository) => {
189+
Ok(Badge::Gitlab {
190+
repository: repository.to_string(),
191+
branch: attributes.get("branch")
192+
.map(String::to_string),
193+
})
194+
},
195+
None => Err(badge_type.to_string()),
196+
}
197+
},
160198
_ => Err(badge_type.to_string()),
161199
}
162200
}

src/tests/badge.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ fn update_crate() {
4545
String::from("rust-lang/rust")
4646
);
4747

48+
let gitlab = Badge::Gitlab {
49+
branch: Some(String::from("beta")),
50+
repository: String::from("rust-lang/rust"),
51+
};
52+
let mut badge_attributes_gitlab = HashMap::new();
53+
badge_attributes_gitlab.insert(
54+
String::from("branch"),
55+
String::from("beta")
56+
);
57+
badge_attributes_gitlab.insert(
58+
String::from("repository"),
59+
String::from("rust-lang/rust")
60+
);
61+
4862
let mut badges = HashMap::new();
4963

5064
// Updating with no badges has no effect
@@ -68,6 +82,15 @@ fn update_crate() {
6882
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
6983
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![travis_ci.clone()]);
7084

85+
// Replacing one badge with another (again)
86+
badges.clear();
87+
badges.insert(
88+
String::from("gitlab"),
89+
badge_attributes_gitlab.clone()
90+
);
91+
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
92+
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![gitlab.clone()]);
93+
7194
// Updating badge attributes
7295
let travis_ci2 = Badge::TravisCi {
7396
branch: None,
@@ -83,14 +106,14 @@ fn update_crate() {
83106
badge_attributes_travis_ci2.clone()
84107
);
85108
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
86-
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![travis_ci2.clone()]);
109+
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![gitlab.clone(), travis_ci2.clone()]);
87110

88111
// Removing one badge
89112
badges.clear();
90113
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
91114
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
92115

93-
// Adding 2 badges
116+
// Adding 3 badges
94117
badges.insert(
95118
String::from("appveyor"),
96119
badge_attributes_appveyor.clone()
@@ -99,22 +122,28 @@ fn update_crate() {
99122
String::from("travis-ci"),
100123
badge_attributes_travis_ci.clone()
101124
);
125+
badges.insert(
126+
String::from("gitlab"),
127+
badge_attributes_gitlab.clone()
128+
);
102129
Badge::update_crate(
103130
tx(&req), &krate, badges.clone()
104131
).unwrap();
105132

106133
let current_badges = krate.badges(tx(&req)).unwrap();
107-
assert_eq!(current_badges.len(), 2);
134+
assert_eq!(current_badges.len(), 3);
108135
assert!(current_badges.contains(&appveyor));
109136
assert!(current_badges.contains(&travis_ci));
137+
assert!(current_badges.contains(&gitlab));
110138

111139
// Removing all badges
112140
badges.clear();
113141
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
114142
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);
115143

116-
// Attempting to add one valid badge (appveyor) and two invalid badges
117-
// (travis-ci without a required attribute and an unknown badge type)
144+
// Attempting to add one valid badge (appveyor) and three invalid badges
145+
// (travis-ci and gitlab without a required attribute and an unknown badge
146+
// type)
118147

119148
// Extra invalid keys are fine, we'll just ignore those
120149
badge_attributes_appveyor.insert(
@@ -133,6 +162,13 @@ fn update_crate() {
133162
badge_attributes_travis_ci.clone()
134163
);
135164

165+
// Repository is a required key
166+
badge_attributes_gitlab.remove("repository");
167+
badges.insert(
168+
String::from("gitlab"),
169+
badge_attributes_gitlab.clone()
170+
);
171+
136172
// This is not a badge that crates.io knows about
137173
let mut invalid_attributes = HashMap::new();
138174
invalid_attributes.insert(
@@ -147,8 +183,9 @@ fn update_crate() {
147183
let invalid_badges = Badge::update_crate(
148184
tx(&req), &krate, badges.clone()
149185
).unwrap();
150-
assert_eq!(invalid_badges.len(), 2);
186+
assert_eq!(invalid_badges.len(), 3);
151187
assert!(invalid_badges.contains(&String::from("travis-ci")));
188+
assert!(invalid_badges.contains(&String::from("gitlab")));
152189
assert!(invalid_badges.contains(&String::from("not-a-badge")));
153190
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![appveyor.clone()]);
154191
}

tests/acceptance/search-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ test('searching for "rust"', function(assert) {
2626

2727
findWithAssert('#crates .row:first .desc .info .badge:first a[href="https://ci.appveyor.com/project/huonw/external_mixin"]');
2828
findWithAssert('#crates .row:first .desc .info .badge:first a img[src="https://ci.appveyor.com/api/projects/status/github/huonw/external_mixin?svg=true&branch=master"]');
29-
findWithAssert('#crates .row:first .desc .info .badge:eq(1) a[href="https://travis-ci.org/huonw/external_mixin"]');
30-
findWithAssert('#crates .row:first .desc .info .badge:eq(1) a img[src="https://travis-ci.org/huonw/external_mixin.svg?branch=master"]');
29+
findWithAssert('#crates .row:first .desc .info .badge:eq(1) a[href="https://gitlab.com/huonw/external_mixin/pipelines"]');
30+
findWithAssert('#crates .row:first .desc .info .badge:eq(1) a img[src="https://gitlab.com/huonw/external_mixin/badges/master/build.svg"]');
31+
findWithAssert('#crates .row:first .desc .info .badge:eq(2) a[href="https://travis-ci.org/huonw/external_mixin"]');
32+
findWithAssert('#crates .row:first .desc .info .badge:eq(2) a img[src="https://travis-ci.org/huonw/external_mixin.svg?branch=master"]');
3133

3234
hasText(assert, '#crates .row:first .desc .summary', 'Yo dawg, use Rust to generate Rust, right in your Rust. (See `external_mixin` to use scripting languages.)');
3335
hasText(assert, '#crates .row:first .downloads', '477');

0 commit comments

Comments
 (0)