From 0fbcbaac4bbbd94029d4dc1a38382a1af6cdbdb8 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Fri, 13 Sep 2019 11:09:23 +0200 Subject: [PATCH 01/12] Support GitHub Actions Badges Documented here https://help.github.com/en/articles/configuring-a-workflow#adding-a-workflow-status-badge-to-your-repository --- app/components/badge-github-actions.js | 13 ++++ .../components/badge-github-actions.hbs | 6 ++ src/models/badge.rs | 5 ++ src/tests/badge.rs | 67 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 app/components/badge-github-actions.js create mode 100644 app/templates/components/badge-github-actions.hbs diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js new file mode 100644 index 00000000000..be880d7b0f2 --- /dev/null +++ b/app/components/badge-github-actions.js @@ -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`; + }), +}); diff --git a/app/templates/components/badge-github-actions.hbs b/app/templates/components/badge-github-actions.hbs new file mode 100644 index 00000000000..176251c66b4 --- /dev/null +++ b/app/templates/components/badge-github-actions.hbs @@ -0,0 +1,6 @@ + + {{ text }} + diff --git a/src/models/badge.rs b/src/models/badge.rs index 675a18b8665..14a616d7532 100644 --- a/src/models/badge.rs +++ b/src/models/badge.rs @@ -39,6 +39,11 @@ pub enum Badge { pipeline: String, build: Option, }, + #[serde(rename = "github-actions")] + GitHubActions { + repository: String, + workflow: String, + }, #[serde(rename = "gitlab")] GitLab { repository: String, diff --git a/src/tests/badge.rs b/src/tests/badge.rs index db3ccce24d6..06c9189268c 100644 --- a/src/tests/badge.rs +++ b/src/tests/badge.rs @@ -7,6 +7,8 @@ struct BadgeRef { appveyor_attributes: HashMap, travis_ci: Badge, travis_ci_attributes: HashMap, + github_actions: Badge, + github_actions_attributes: HashMap, gitlab: Badge, gitlab_attributes: HashMap, azure_devops: Badge, @@ -78,6 +80,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"), @@ -169,6 +180,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, @@ -226,6 +239,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 @@ -459,6 +486,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 From a9dfee9a83df2118c4cadd805e7b46d07a065999 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Mon, 28 Oct 2019 14:06:41 +0100 Subject: [PATCH 02/12] Escape whitespace --- app/components/badge-github-actions.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index be880d7b0f2..b60bb712a25 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -6,7 +6,12 @@ export default Component.extend({ tagName: 'span', classNames: ['badge'], repository: alias('badge.attributes.repository'), - workflow: alias('badge.attributes.workflow'), + workflow: computed('badge.attributes.workflow', function() { + return this.get('badge.attributes.workflow') + .split('/') + .map(encodeURIComponent) + .join('/'); + }), text: computed('badge', function() { return `GitHub Actions workflow status for the ${this.workflow} workflow`; }), From 7c423ed3e9fe49b72b2743bc05deadd93ec5687e Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Sun, 22 Dec 2019 22:36:14 +0100 Subject: [PATCH 03/12] Implement `event` and `branch` attributes --- app/components/badge-github-actions.js | 27 +++++++++++++++++-- .../components/badge-github-actions.hbs | 5 +--- src/models/badge.rs | 2 ++ src/tests/badge.rs | 2 ++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index b60bb712a25..4033eaaac53 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -6,13 +6,36 @@ export default Component.extend({ tagName: 'span', classNames: ['badge'], repository: alias('badge.attributes.repository'), - workflow: computed('badge.attributes.workflow', function() { + imageUrl: computed('badge.attributes.repository', 'badge.attributes.workflow', 'badge.attributes.branch', 'badge.attributes.event', function () { + const query = Object.entries({ + branch: this.branch, + event: this.event, + }) + .filter(kv => kv[1] != '') + .map(kv => kv.map(encodeURIComponent).join('=')) + .join('&'); + + const base = `https://github.com/${this.repository}/workflows/${this.workflow}/badge.svg`; + + if (query != '') { + return `${base}?${query}`; + } else { + return base; + } + }), + workflow: computed('badge.attributes.workflow', function () { return this.get('badge.attributes.workflow') .split('/') .map(encodeURIComponent) .join('/'); }), - text: computed('badge', function() { + branch: computed('badge.attributes.branch', function () { + return encodeURIComponent(this.get('badge.attributes.branch') || ''); + }), + event: computed('badge.attributes.event', function () { + return encodeURIComponent(this.get('badge.attributes.event') || ''); + }), + text: computed('badge', function () { return `GitHub Actions workflow status for the ${this.workflow} workflow`; }), }); diff --git a/app/templates/components/badge-github-actions.hbs b/app/templates/components/badge-github-actions.hbs index 176251c66b4..6e8b4052533 100644 --- a/app/templates/components/badge-github-actions.hbs +++ b/app/templates/components/badge-github-actions.hbs @@ -1,6 +1,3 @@ - {{ text }} + {{ text }} diff --git a/src/models/badge.rs b/src/models/badge.rs index 14a616d7532..e2a84eb948e 100644 --- a/src/models/badge.rs +++ b/src/models/badge.rs @@ -43,6 +43,8 @@ pub enum Badge { GitHubActions { repository: String, workflow: String, + branch: Option, + event: Option, }, #[serde(rename = "gitlab")] GitLab { diff --git a/src/tests/badge.rs b/src/tests/badge.rs index 06c9189268c..f28ebed7f67 100644 --- a/src/tests/badge.rs +++ b/src/tests/badge.rs @@ -83,6 +83,8 @@ fn set_up() -> (BadgeTestCrate, BadgeRef) { let github_actions = Badge::GitHubActions { repository: String::from("rust-lang/rust"), workflow: String::from("build"), + branch: Some(String::from("beta")), + event: Some(String::from("push")), }; let mut badge_attributes_github_actions = HashMap::new(); badge_attributes_github_actions From eed02e121928e1de704a3c9a29fb2a9a27cff0d6 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Sun, 22 Dec 2019 23:00:59 +0100 Subject: [PATCH 04/12] Apply fixes from rebase --- app/templates/components/badge-github-actions.hbs | 4 ++-- src/tests/badge.rs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/templates/components/badge-github-actions.hbs b/app/templates/components/badge-github-actions.hbs index 6e8b4052533..ce38d5f5d4a 100644 --- a/app/templates/components/badge-github-actions.hbs +++ b/app/templates/components/badge-github-actions.hbs @@ -1,3 +1,3 @@ - - {{ text }} + + {{ this.text }} diff --git a/src/tests/badge.rs b/src/tests/badge.rs index f28ebed7f67..70858db4810 100644 --- a/src/tests/badge.rs +++ b/src/tests/badge.rs @@ -90,6 +90,8 @@ fn set_up() -> (BadgeTestCrate, BadgeRef) { badge_attributes_github_actions .insert(String::from("repository"), String::from("rust-lang/rust")); badge_attributes_github_actions.insert(String::from("workflow"), String::from("build")); + badge_attributes_github_actions.insert(String::from("branch"), String::from("beta")); + badge_attributes_github_actions.insert(String::from("event"), String::from("push")); let gitlab = Badge::GitLab { branch: Some(String::from("beta")), From 31d83c30ca94a2dd1cf4ae0ca2981e836020e3d9 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Sun, 22 Dec 2019 23:05:34 +0100 Subject: [PATCH 05/12] Fix Formatting --- app/components/badge-github-actions.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 4033eaaac53..321e3893857 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -6,7 +6,7 @@ export default Component.extend({ tagName: 'span', classNames: ['badge'], repository: alias('badge.attributes.repository'), - imageUrl: computed('badge.attributes.repository', 'badge.attributes.workflow', 'badge.attributes.branch', 'badge.attributes.event', function () { + imageUrl: computed('badge.attributes.{repository,workflow,branch,event}', function() { const query = Object.entries({ branch: this.branch, event: this.event, @@ -23,19 +23,19 @@ export default Component.extend({ return base; } }), - workflow: computed('badge.attributes.workflow', function () { + workflow: computed('badge.attributes.workflow', function() { return this.get('badge.attributes.workflow') .split('/') .map(encodeURIComponent) .join('/'); }), - branch: computed('badge.attributes.branch', function () { + branch: computed('badge.attributes.branch', function() { return encodeURIComponent(this.get('badge.attributes.branch') || ''); }), - event: computed('badge.attributes.event', function () { + event: computed('badge.attributes.event', function() { return encodeURIComponent(this.get('badge.attributes.event') || ''); }), - text: computed('badge', function () { + text: computed('badge', function() { return `GitHub Actions workflow status for the ${this.workflow} workflow`; }), }); From 9e458d68e2e17e484e38d1884163f3449dc9ae52 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 28 Jan 2020 16:57:45 +0100 Subject: [PATCH 06/12] Use URL to build imageURL query --- app/components/badge-github-actions.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 321e3893857..00d2804d34c 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -7,21 +7,17 @@ export default Component.extend({ classNames: ['badge'], repository: alias('badge.attributes.repository'), imageUrl: computed('badge.attributes.{repository,workflow,branch,event}', function() { - const query = Object.entries({ - branch: this.branch, - event: this.event, - }) - .filter(kv => kv[1] != '') - .map(kv => kv.map(encodeURIComponent).join('=')) - .join('&'); + const url = new URL(`https://github.com/${this.repository}/workflows/${this.workflow}/badge.svg`); - const base = `https://github.com/${this.repository}/workflows/${this.workflow}/badge.svg`; + if (this.branch !== '') { + url.searchParams.set('branch', this.branch); + } - if (query != '') { - return `${base}?${query}`; - } else { - return base; + if (this.event !== '') { + url.searchParams.set('event', this.event); } + + return url.href; }), workflow: computed('badge.attributes.workflow', function() { return this.get('badge.attributes.workflow') From 5c5f505bd91437dc8562908b7ff25d6fd785cc84 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 28 Jan 2020 17:19:35 +0100 Subject: [PATCH 07/12] Fix rebase fallout --- app/{templates => }/components/badge-github-actions.hbs | 1 + app/components/badge-github-actions.js | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) rename app/{templates => }/components/badge-github-actions.hbs (97%) diff --git a/app/templates/components/badge-github-actions.hbs b/app/components/badge-github-actions.hbs similarity index 97% rename from app/templates/components/badge-github-actions.hbs rename to app/components/badge-github-actions.hbs index ce38d5f5d4a..0f53230c58c 100644 --- a/app/templates/components/badge-github-actions.hbs +++ b/app/components/badge-github-actions.hbs @@ -1,3 +1,4 @@ {{ this.text }} + diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 00d2804d34c..dace57921f9 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -4,7 +4,6 @@ import { alias } from '@ember/object/computed'; export default Component.extend({ tagName: 'span', - classNames: ['badge'], repository: alias('badge.attributes.repository'), imageUrl: computed('badge.attributes.{repository,workflow,branch,event}', function() { const url = new URL(`https://github.com/${this.repository}/workflows/${this.workflow}/badge.svg`); From 0daadf047532f3388c51e505387bba9934004b2b Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 28 Jan 2020 17:22:41 +0100 Subject: [PATCH 08/12] Use unencoded workflow name as (alt-)text --- app/components/badge-github-actions.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index dace57921f9..4208a7156e8 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -5,8 +5,8 @@ import { alias } from '@ember/object/computed'; export default Component.extend({ tagName: 'span', repository: alias('badge.attributes.repository'), - imageUrl: computed('badge.attributes.{repository,workflow,branch,event}', function() { - const url = new URL(`https://github.com/${this.repository}/workflows/${this.workflow}/badge.svg`); + imageUrl: computed('badge.attributes.{repository,workflow_enc,branch,event}', function() { + const url = new URL(`https://github.com/${this.repository}/workflows/${this.workflow_enc}/badge.svg`); if (this.branch !== '') { url.searchParams.set('branch', this.branch); @@ -19,6 +19,9 @@ export default Component.extend({ return url.href; }), workflow: computed('badge.attributes.workflow', function() { + return this.get('badge.attributes.workflow'); + }), + workflow_enc: computed('badge.attributes.workflow', function() { return this.get('badge.attributes.workflow') .split('/') .map(encodeURIComponent) From 73f033de918fdbe97b9a2f3897085cf9bc4e690c Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 28 Jan 2020 17:24:06 +0100 Subject: [PATCH 09/12] Use query URL for badge link --- app/components/badge-github-actions.hbs | 2 +- app/components/badge-github-actions.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/components/badge-github-actions.hbs b/app/components/badge-github-actions.hbs index 0f53230c58c..c11aa4f25b0 100644 --- a/app/components/badge-github-actions.hbs +++ b/app/components/badge-github-actions.hbs @@ -1,4 +1,4 @@ - + {{ this.text }} diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 4208a7156e8..054fd001c5c 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -18,6 +18,23 @@ export default Component.extend({ return url.href; }), + url: computed('badge.attributes.{repository,workflow_enc,branch,event}', function() { + const url = new URL(`https://github.com/${this.repository}/actions`); + + if (this.workflow_enc !== '') { + url.searchParams.set('workflow', this.workflow_enc); + } + + if (this.branch !== '') { + url.searchParams.set('branch', this.branch); + } + + if (this.event !== '') { + url.searchParams.set('event', this.event); + } + + return url.href; + }), workflow: computed('badge.attributes.workflow', function() { return this.get('badge.attributes.workflow'); }), From f136c528bd9112f7b6afcb9b287d23a36fe09d14 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 28 Jan 2020 17:25:30 +0100 Subject: [PATCH 10/12] Also show branch name in the (alt-)text Defaults to the master branch --- app/components/badge-github-actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 054fd001c5c..67450b7f70f 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -45,12 +45,12 @@ export default Component.extend({ .join('/'); }), branch: computed('badge.attributes.branch', function() { - return encodeURIComponent(this.get('badge.attributes.branch') || ''); + return encodeURIComponent(this.get('badge.attributes.branch') || 'master'); }), event: computed('badge.attributes.event', function() { return encodeURIComponent(this.get('badge.attributes.event') || ''); }), text: computed('badge', function() { - return `GitHub Actions workflow status for the ${this.workflow} workflow`; + return `GitHub Actions workflow status for the ${this.workflow} workflow on the ${this.branch} branch`; }), }); From 4d55e2d9c2e872691d5bb2a5e403d6d1dc28e02e Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 29 Jan 2020 16:44:52 +0100 Subject: [PATCH 11/12] Fix building of url --- app/components/badge-github-actions.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 67450b7f70f..0818d49f1ee 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -18,19 +18,24 @@ export default Component.extend({ return url.href; }), - url: computed('badge.attributes.{repository,workflow_enc,branch,event}', function() { + url: computed('badge.attributes.{repository,workflow,branch,event}', function() { const url = new URL(`https://github.com/${this.repository}/actions`); - if (this.workflow_enc !== '') { - url.searchParams.set('workflow', this.workflow_enc); + let query = ''; + if (this.workflow !== '') { + query += `workflow:"${this.workflow}"`; } if (this.branch !== '') { - url.searchParams.set('branch', this.branch); + query += `branch:"${this.branch}"`; } if (this.event !== '') { - url.searchParams.set('event', this.event); + query += `event:"${this.event}"`; + } + + if (query !== '') { + url.searchParams.set('query', query); } return url.href; From 3956277bcbad8683b5b2314df0ca293c1ff09468 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 29 Jan 2020 18:41:18 +0100 Subject: [PATCH 12/12] Don't encode branch and event name --- app/components/badge-github-actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/badge-github-actions.js b/app/components/badge-github-actions.js index 0818d49f1ee..01404e4909a 100644 --- a/app/components/badge-github-actions.js +++ b/app/components/badge-github-actions.js @@ -50,10 +50,10 @@ export default Component.extend({ .join('/'); }), branch: computed('badge.attributes.branch', function() { - return encodeURIComponent(this.get('badge.attributes.branch') || 'master'); + return this.get('badge.attributes.branch') || 'master'; }), event: computed('badge.attributes.event', function() { - return encodeURIComponent(this.get('badge.attributes.event') || ''); + return this.get('badge.attributes.event') || ''; }), text: computed('badge', function() { return `GitHub Actions workflow status for the ${this.workflow} workflow on the ${this.branch} branch`;