Skip to content

Commit

Permalink
Support GitHub Actions Badges
Browse files Browse the repository at this point in the history
  • Loading branch information
CryZe committed Sep 26, 2019
1 parent 6f5e217 commit d85627c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/components/badge-github-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';

export default Component.extend({
tagName: 'span',
classNames: ['badge'],
repository: alias('badge.attributes.repository'),
workflow: alias('badge.attributes.workflow'),
text: computed('badge', function() {
return `GitHub Actions workflow status for the ${this.workflow} workflow`;
}),
});
6 changes: 6 additions & 0 deletions app/templates/components/badge-github-actions.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<a href="https://github.com/{{ repository }}/actions">
<img
src="https://github.com/{{ repository }}/workflows/{{ workflow }}/badge.svg"
alt="{{ text }}"
title="{{ text }}">
</a>
5 changes: 5 additions & 0 deletions src/models/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ pub enum Badge {
pipeline: String,
build: Option<String>,
},
#[serde(rename = "github-actions")]
GitHubActions {
repository: String,
workflow: String,
},
#[serde(rename = "gitlab")]
GitLab {
repository: String,
Expand Down
67 changes: 67 additions & 0 deletions src/tests/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct BadgeRef {
appveyor_attributes: HashMap<String, String>,
travis_ci: Badge,
travis_ci_attributes: HashMap<String, String>,
github_actions: Badge,
github_actions_attributes: HashMap<String, String>,
gitlab: Badge,
gitlab_attributes: HashMap<String, String>,
azure_devops: Badge,
Expand Down Expand Up @@ -76,6 +78,15 @@ fn set_up() -> (BadgeTestCrate, BadgeRef) {
badge_attributes_travis_ci.insert(String::from("branch"), String::from("beta"));
badge_attributes_travis_ci.insert(String::from("repository"), String::from("rust-lang/rust"));

let github_actions = Badge::GitHubActions {
repository: String::from("rust-lang/rust"),
workflow: String::from("build"),
};
let mut badge_attributes_github_actions = HashMap::new();
badge_attributes_github_actions
.insert(String::from("repository"), String::from("rust-lang/rust"));
badge_attributes_github_actions.insert(String::from("workflow"), String::from("build"));

let gitlab = Badge::GitLab {
branch: Some(String::from("beta")),
repository: String::from("rust-lang/rust"),
Expand Down Expand Up @@ -158,6 +169,8 @@ fn set_up() -> (BadgeTestCrate, BadgeRef) {
appveyor_attributes: badge_attributes_appveyor,
travis_ci,
travis_ci_attributes: badge_attributes_travis_ci,
github_actions,
github_actions_attributes: badge_attributes_github_actions,
gitlab,
gitlab_attributes: badge_attributes_gitlab,
azure_devops,
Expand Down Expand Up @@ -213,6 +226,20 @@ fn update_add_travis_ci() {
assert_eq!(krate.badges(), vec![test_badges.travis_ci]);
}

#[test]
fn update_add_github_actions() {
// Add a github actions badge
let (krate, test_badges) = set_up();

let mut badges = HashMap::new();
badges.insert(
String::from("github-actions"),
test_badges.github_actions_attributes,
);
krate.update(&badges);
assert_eq!(krate.badges(), vec![test_badges.github_actions]);
}

#[test]
fn update_add_gitlab() {
// Add a gitlab badge
Expand Down Expand Up @@ -432,6 +459,46 @@ fn travis_ci_required_keys() {
assert_eq!(krate.badges(), vec![]);
}

#[test]
fn github_actions_required_key_repository() {
// Add a GitHub Actions badge missing the required repository field
let (krate, mut test_badges) = set_up();

let mut badges = HashMap::new();

// Repository is a required key
test_badges.github_actions_attributes.remove("repository");
badges.insert(
String::from("github-actions"),
test_badges.github_actions_attributes,
);

let invalid_badges = krate.update(&badges);
assert_eq!(invalid_badges.len(), 1);
assert_eq!(invalid_badges.first().unwrap(), "github-actions");
assert_eq!(krate.badges(), vec![]);
}

#[test]
fn github_actions_required_key_workflow() {
// Add a GitHub Actions badge missing the required workflow field
let (krate, mut test_badges) = set_up();

let mut badges = HashMap::new();

// Workflow is a required key
test_badges.github_actions_attributes.remove("workflow");
badges.insert(
String::from("github-actions"),
test_badges.github_actions_attributes,
);

let invalid_badges = krate.update(&badges);
assert_eq!(invalid_badges.len(), 1);
assert_eq!(invalid_badges.first().unwrap(), "github-actions");
assert_eq!(krate.badges(), vec![]);
}

#[test]
fn gitlab_required_keys() {
// Add a gitlab badge missing a required field
Expand Down

0 comments on commit d85627c

Please sign in to comment.