Skip to content

Commit

Permalink
Add Gitlab CI badge support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Susurrus committed Feb 1, 2017
1 parent 29a1dcd commit 547f2bf
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
13 changes: 13 additions & 0 deletions app/components/badge-gitlab.js
@@ -0,0 +1,13 @@
import Ember from 'ember';

export default Ember.Component.extend({
tagName: 'span',
classNames: ['badge'],
repository: Ember.computed.alias('badge.attributes.repository'),
branch: Ember.computed('badge.attributes.branch', function() {
return this.get('badge.attributes.branch') || 'master';
}),
text: Ember.computed('badge', function() {
return `Gitlab build status for the ${ this.get('branch') } branch`;
})
});
7 changes: 7 additions & 0 deletions app/mirage/fixtures/search.js
Expand Up @@ -32,6 +32,13 @@ export default {
"repository": "huonw/external_mixin"
},
"badge_type": "travis-ci"
},
{
"attributes": {
"branch": "master",
"repository": "huonw/external_mixin"
},
"badge_type": "gitlab"
}
],
"versions": null
Expand Down
6 changes: 6 additions & 0 deletions app/templates/components/badge-gitlab.hbs
@@ -0,0 +1,6 @@
<a href="https://ci.appveyor.com/project/{{ repository }}">
<img
src="https://gitlab.com/{{ repository }}/badges/{{ branch }}/build.svg"
alt="{{ text }}"
title="{{ text }}" />
</a>
42 changes: 40 additions & 2 deletions src/badge.rs
Expand Up @@ -15,6 +15,9 @@ pub enum Badge {
Appveyor {
repository: String, branch: Option<String>, service: Option<String>,
},
Gitlab {
repository: String, branch: Option<String>,
},
}

#[derive(RustcEncodable, RustcDecodable, PartialEq, Debug)]
Expand Down Expand Up @@ -58,6 +61,19 @@ impl Model for Badge {
database"),
}
},
"gitlab" => {
Badge::Gitlab {
branch: attributes.get("branch")
.and_then(Json::as_string)
.map(str::to_string),
repository: attributes.get("repository")
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid Gitlab badge \
without repository in the \
database"),
}
},
_ => {
panic!("Unknown badge type {} in the database", badge_type);
},
Expand All @@ -84,6 +100,7 @@ impl Badge {
match *self {
Badge::TravisCi {..} => "travis-ci",
Badge::Appveyor {..} => "appveyor",
Badge::Gitlab{..} => "gitlab",
}
}

Expand Down Expand Up @@ -120,7 +137,16 @@ impl Badge {
service
);
}
}
},
Badge::Gitlab { branch, repository } => {
attributes.insert(String::from("repository"), repository);
if let Some(branch) = branch {
attributes.insert(
String::from("branch"),
branch
);
}
},
}

attributes
Expand Down Expand Up @@ -156,7 +182,19 @@ impl Badge {
},
None => Err(badge_type.to_string()),
}
},
},
"gitlab" => {
match attributes.get("repository") {
Some(repository) => {
Ok(Badge::Gitlab {
repository: repository.to_string(),
branch: attributes.get("branch")
.map(String::to_string),
})
},
None => Err(badge_type.to_string()),
}
},
_ => Err(badge_type.to_string()),
}
}
Expand Down
47 changes: 42 additions & 5 deletions src/tests/badge.rs
Expand Up @@ -45,6 +45,20 @@ fn update_crate() {
String::from("rust-lang/rust")
);

let gitlab = Badge::Gitlab {
branch: Some(String::from("beta")),
repository: String::from("rust-lang/rust"),
};
let mut badge_attributes_gitlab = HashMap::new();
badge_attributes_gitlab.insert(
String::from("branch"),
String::from("beta")
);
badge_attributes_gitlab.insert(
String::from("repository"),
String::from("rust-lang/rust")
);

let mut badges = HashMap::new();

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

// Replacing one badge with another (again)
badges.clear();
badges.insert(
String::from("gitlab"),
badge_attributes_gitlab.clone()
);
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![gitlab.clone()]);

// Updating badge attributes
let travis_ci2 = Badge::TravisCi {
branch: None,
Expand All @@ -90,7 +113,7 @@ fn update_crate() {
Badge::update_crate(tx(&req), &krate, badges.clone()).unwrap();
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![]);

// Adding 2 badges
// Adding 3 badges
badges.insert(
String::from("appveyor"),
badge_attributes_appveyor.clone()
Expand All @@ -99,22 +122,28 @@ fn update_crate() {
String::from("travis-ci"),
badge_attributes_travis_ci.clone()
);
badges.insert(
String::from("gitlab"),
badge_attributes_gitlab.clone()
);
Badge::update_crate(
tx(&req), &krate, badges.clone()
).unwrap();

let current_badges = krate.badges(tx(&req)).unwrap();
assert_eq!(current_badges.len(), 2);
assert_eq!(current_badges.len(), 3);
assert!(current_badges.contains(&appveyor));
assert!(current_badges.contains(&travis_ci));
assert!(current_badges.contains(&gitlab));

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

// Attempting to add one valid badge (appveyor) and two invalid badges
// (travis-ci without a required attribute and an unknown badge type)
// Attempting to add one valid badge (appveyor) and three invalid badges
// (travis-ci and gitlab without a required attribute and an unknown badge
// type)

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

// Repository is a required key
badge_attributes_gitlab.remove("repository");
badges.insert(
String::from("gitlab"),
badge_attributes_gitlab.clone()
);

// This is not a badge that crates.io knows about
let mut invalid_attributes = HashMap::new();
invalid_attributes.insert(
Expand All @@ -147,8 +183,9 @@ fn update_crate() {
let invalid_badges = Badge::update_crate(
tx(&req), &krate, badges.clone()
).unwrap();
assert_eq!(invalid_badges.len(), 2);
assert_eq!(invalid_badges.len(), 3);
assert!(invalid_badges.contains(&String::from("travis-ci")));
assert!(invalid_badges.contains(&String::from("gitlab")));
assert!(invalid_badges.contains(&String::from("not-a-badge")));
assert_eq!(krate.badges(tx(&req)).unwrap(), vec![appveyor.clone()]);
}
1 change: 1 addition & 0 deletions tests/acceptance/search-test.js
Expand Up @@ -26,6 +26,7 @@ test('searching for "rust"', function(assert) {

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"]');
findWithAssert('#crates .row:first .desc .info .badge:eq(1) a img[src="https://travis-ci.org/huonw/external_mixin.svg?branch=master"]');
findWithAssert('#crates .row:first .desc .info .badge:eq(2) a img[src="https://gitlab.com/huonw/external_mixin/badges/master/build.svg"]');

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.)');
hasText(assert, '#crates .row:first .downloads', '477');
Expand Down

0 comments on commit 547f2bf

Please sign in to comment.