Skip to content

Commit

Permalink
Add color picker for number widgets (#40)
Browse files Browse the repository at this point in the history
* Added support for choosing a color in the 1x1 widget

* Add background color option to the number widgets
  • Loading branch information
rajbos authored Dec 16, 2023
1 parent b2f598c commit 4969764
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 74 deletions.
2 changes: 1 addition & 1 deletion vss-extension-dev.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifestVersion": 1,
"id": "GHAzDoWidget-DEV",
"version": "0.2.371",
"version": "0.2.397",
"public": false,
"name": "Advanced Security dashboard Widgets [DEV]",
"description": "[DEV] GitHub Advanced Security for Azure DevOps dashboard widgets",
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifestVersion": 1,
"id": "GHAzDoWidget",
"version": "0.0.1.11",
"version": "0.0.1.14",
"public": true,
"name": "Advanced Security dashboard Widgets",
"description": "GitHub Advanced Security for Azure DevOps dashboard widgets",
Expand Down
2 changes: 1 addition & 1 deletion widgets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}

.GHAzDo-widget {
background-color: rgb(104, 33, 122);
background-color: #68217a;
color: white;
text-align: center;
font-size: 30px;
Expand Down
28 changes: 26 additions & 2 deletions widgets/widgets/widget_1x1/configuration_1x1.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VSS.register("GHAzDoWidget.Configuration_1x1", async function () {
var $repoDropdown = $("#repo-dropdown");
var $repoAlertType = $("#repo-alert-type");
var $colorPicker = $("#color-picker");
const repos = await getRepos(VSS, Service, GitWebApi);
const projects = await getProjects(VSS, Service, RestClient);

Expand All @@ -32,7 +33,8 @@
data: JSON.stringify({
repo: $repoDropdown.val(),
repoId: repo?.id,
repoAlertType: $repoAlertType.val()
repoAlertType: $repoAlertType.val(),
color: $colorPicker.val()
})
};
var eventName = WidgetHelpers.WidgetEvent.ConfigurationChange;
Expand Down Expand Up @@ -60,6 +62,11 @@
$repoAlertType.val(settings.repoAlertType);
}

if (settings && settings.color) {
// select the color that was saved in the settings
$colorPicker.val(settings.color);
}

// register a change event handler for the dropdowns
$repoDropdown.on("change", async function () {
await reloadWidget(widgetConfigurationContext);
Expand All @@ -69,6 +76,10 @@
await reloadWidget(widgetConfigurationContext);
});

$colorPicker.on("change", async function () {
await reloadWidget(widgetConfigurationContext);
});

return WidgetHelpers.WidgetStatusHelper.Success();
},
onSave: async function() {
Expand All @@ -81,7 +92,8 @@
data: JSON.stringify({
repo: $repoDropdown.val(),
repoId: repo?.id,
repoAlertType: $repoAlertType.val()
repoAlertType: $repoAlertType.val(),
color: $colorPicker.val()
})
};
consoleLog(`Saving the 1x1 settings with ${JSON.stringify(customSettings)}`)
Expand All @@ -99,6 +111,10 @@
margin-top: 10px;
min-width: 150px;
}

td {
padding: 5px;
}
</style>
<div class="container">
<table>
Expand All @@ -122,6 +138,14 @@
</select>
</td>
</tr>
<tr>
<td>
<label class="label">Background color: </label>
</td>
<td>
<input type="color" id="color-picker" value="#68217a">
</td>
</tr>
</table>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion widgets/widgets/widget_1x1/widget_1x1.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

</head>
<body>
<div class="widget GHAzDo-widget">
<div class="widget GHAzDo-widget" id="GHAzDo-widget">
<h2 class="ghazdo-title" title="">GitHub Advanced Security Alerts</h2>
<div id="query-info-container" class="column-container">

Expand Down
5 changes: 5 additions & 0 deletions widgets/widgets/widget_1x1/widget_1x1.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ async function loadWidget(widgetSettings, organization, projectName, VSS, Servic
title.attr('title', repoName);
consoleLog(`title set to [${repoName}]`);

// set the color
const color = data.color ? data.color : '#68217a'; // default to purple
const widget = document.getElementById('GHAzDo-widget');
widget.style.backgroundColor = `${color}`;

// GHAS is only available on the SaaS version, so we can hardcode the domain
linkBase = `https://dev.azure.com/${organization}/${projectName}/_git/${repoName}/alerts`;
}
Expand Down
65 changes: 48 additions & 17 deletions widgets/widgets/widget_2x1/configuration_2x1.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,29 @@
VSS.require(["VSS/Service", "TFS/Dashboards/WidgetHelpers", "VSS/Context", "TFS/VersionControl/GitRestClient"],
function (Service, WidgetHelpers, context, GitWebApi) {
VSS.register("GHAzDoWidget.Configuration", async function () {
var $repoDropdown = $("#repo-dropdown");
var $repoDropdown = $("#repo-dropdown")
var $colorPicker = $("#color-picker")
// get a repos in this project
const repos = await getRepos(VSS, Service, GitWebApi);

async function reloadWidget(widgetConfigurationContext) {
let repo;
if (repos) {
// find the repo with this name
repo = repos.find(r => r.name === $repoDropdown.val());
}
var customSettings = {
data: JSON.stringify({
repo: $repoDropdown.val(),
repoId: repo.id,
color: $colorPicker.val()
})
};
var eventName = WidgetHelpers.WidgetEvent.ConfigurationChange;
var eventArgs = WidgetHelpers.WidgetEvent.Args(customSettings);
widgetConfigurationContext.notify(eventName, eventArgs);
}

return {
load: async function (widgetSettings, widgetConfigurationContext) {
var settings = logWidgetSettings(widgetSettings, VSS, "2x1");
Expand All @@ -38,22 +57,17 @@
$repoDropdown.val(settings.repo);
}

$repoDropdown.on("change", function () {
let repo;
if (repos) {
// find the repo with this name
repo = repos.find(r => r.name === $repoDropdown.val());
}
if (settings && settings.color) {
// select the color that was saved in the settings
$colorPicker.val(settings.color);
}

var customSettings = {
data: JSON.stringify({
repo: $repoDropdown.val(),
repoId: repo.id
})
};
var eventName = WidgetHelpers.WidgetEvent.ConfigurationChange;
var eventArgs = WidgetHelpers.WidgetEvent.Args(customSettings);
widgetConfigurationContext.notify(eventName, eventArgs);
$colorPicker.on("change", async function () {
await reloadWidget(widgetConfigurationContext);
});

$repoDropdown.on("change", async function () {
await reloadWidget(widgetConfigurationContext);
});

return WidgetHelpers.WidgetStatusHelper.Success();
Expand All @@ -68,7 +82,8 @@
var customSettings = {
data: JSON.stringify({
repo: $repoDropdown.val(),
repoId: repo.id
repoId: repo.id,
color: $colorPicker.val()
})
};
console.log(`Saving the settings with ${JSON.stringify(customSettings)}`)
Expand All @@ -81,6 +96,12 @@
</script>
</head>
<body>
<style>
td {
padding: 5px;
}
</style>

<div class="container">
<fieldset>
<label class="label">Repository: </label>
Expand All @@ -89,5 +110,15 @@
</select>
</fieldset>
</div>
<table>
<tr>
<td>
<label class="label">Background color: </label>
</td>
<td>
<input type="color" id="color-picker" value="#68217a">
</td>
</tr>
</table>
</body>
</html>
36 changes: 18 additions & 18 deletions widgets/widgets/widget_2x1/widget_2x1.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,46 @@
VSS.init({
explicitNotifyLoaded: true,
usePlatformStyles: true
});
})

VSS.require(
["TFS/Dashboards/WidgetHelpers", "VSS/Context"],
async function (WidgetHelpers, context)
{
WidgetHelpers.IncludeWidgetStyles();
VSS.register("GHAzDoWidget", function () {
const webContext = VSS.getWebContext();
const project = webContext.project;
const organization = webContext.account.name;
const projectId = project.id;
const webContext = VSS.getWebContext()
const project = webContext.project
const organization = webContext.account.name
const projectId = project.id
// convert project.name to url encoding
const projectName = project.name.replace(/ /g, "%20").replace(/&/g, "%26");
const projectName = project.name.replace(/ /g, "%20").replace(/&/g, "%26")

consoleLog('project id: ' + projectId);
consoleLog('project name: ' + projectName);
consoleLog('organization name: ' + organization);
consoleLog('project id: ' + projectId)
consoleLog('project name: ' + projectName)
consoleLog('organization name: ' + organization)

return {
load: async function (widgetSettings) {
await loadWidget(widgetSettings, organization, projectName);
await loadWidget(widgetSettings, organization, projectName)

return WidgetHelpers.WidgetStatusHelper.Success();
return WidgetHelpers.WidgetStatusHelper.Success()
},
reload: async function (widgetSettings) {
consoleLog('reload with widgetSettings: ' + JSON.stringify(widgetSettings));
await loadWidget(widgetSettings, organization, projectName);
return;
consoleLog('reload with widgetSettings: ' + JSON.stringify(widgetSettings))
await loadWidget(widgetSettings, organization, projectName)
return
}
}
});
VSS.notifyLoadSucceeded();
})
VSS.notifyLoadSucceeded()
}
);
)
</script>

</head>
<body>
<div class="widget GHAzDo-widget">
<div class="widget GHAzDo-widget" id="GHAzDo-widget">
<h2 class="ghazdo-title">GitHub Advanced Security Alerts</h2>
<div id="query-info-container" class="column-container">

Expand Down
71 changes: 38 additions & 33 deletions widgets/widgets/widget_2x1/widget_2x1.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
async function loadWidget(widgetSettings, organization, projectName) {
consoleLog(`WidgetSettings inside loadWidget: ${JSON.stringify(widgetSettings)}`);
consoleLog(`Running for organization [${organization}], projectName [${projectName}]`);
consoleLog(`WidgetSettings inside loadWidget: ${JSON.stringify(widgetSettings)}`)
consoleLog(`Running for organization [${organization}], projectName [${projectName}]`)

// data contains a stringified json object, so we need to make a json object from it
const data = JSON.parse(widgetSettings.customSettings.data);
const data = JSON.parse(widgetSettings.customSettings.data)

// init with default values
let alerts = {
dependencyAlerts: 0,
secretAlerts: 0,
codeAlerts: 0
};
let linkBase = 'https://dev.azure.com';
}
let linkBase = 'https://dev.azure.com'

if (data && data.repo) {
const repoName = data.repo;
const repoId = data.repoId;
consoleLog('loaded repoName from widgetSettings: ' + repoName);
const repoName = data.repo
const repoId = data.repoId
consoleLog('loaded repoName from widgetSettings: ' + repoName)

// set the tile
var title = $('h2.ghazdo-title');
title.text(`Security Alerts for ${repoName}`);
title.attr('title', repoName);
alerts = (await getAlerts(organization, projectName, repoId)).values;
consoleLog('alerts: ' + JSON.stringify(alerts));
var title = $('h2.ghazdo-title')
title.text(`Security Alerts for ${repoName}`)
title.attr('title', repoName)
alerts = (await getAlerts(organization, projectName, repoId)).values
consoleLog('alerts: ' + JSON.stringify(alerts))

// GHAS is only available on the SaaS version, so we can hardcode the domain
linkBase = `https://dev.azure.com/${organization}/${projectName}/_git/${repoName}/alerts`;
linkBase = `https://dev.azure.com/${organization}/${projectName}/_git/${repoName}/alerts`

// set the color
const color = data.color ? data.color : '#68217a' // default to purple
const widget = document.getElementById('GHAzDo-widget')
widget.style.backgroundColor = `${color}`
}
else {
consoleLog('configuration is needed first, opening with empty values');
// set the tile to indicate config is needed
var title = $('h2.ghazdo-title');
title.text(`Configure the widget to get Security Alerts`);
var title = $('h2.ghazdo-title')
title.text(`Configure the widget to get Security Alerts`)
}

// set the alert counts
var dependencyAlertCount = $('p.dependencyAlertCount');
dependencyAlertCount.text(alerts.dependencyAlerts);
const dependencyLinkValue = `${linkBase}?_t=dependencies`;
const dependencyLink = $('a.dependency-link');
dependencyLink.attr('href', dependencyLinkValue);

var secretAlertCount = $('p.secretAlertCount');
secretAlertCount.text(alerts.secretAlerts);
const secretLinkValue = `${linkBase}?_t=secrets`;
const secretLink = $('a.secret-link');
secretLink.attr('href', secretLinkValue);

var codeAlertCount = $('p.codeAlertCount');
codeAlertCount.text(alerts.codeAlerts);
const codeLinkValue = `${linkBase}?_t=codescanning`;
const codeLink = $('a.code-link');
codeLink.attr('href', codeLinkValue);
var dependencyAlertCount = $('p.dependencyAlertCount')
dependencyAlertCount.text(alerts.dependencyAlerts)
const dependencyLinkValue = `${linkBase}?_t=dependencies`
const dependencyLink = $('a.dependency-link')
dependencyLink.attr('href', dependencyLinkValue)

var secretAlertCount = $('p.secretAlertCount')
secretAlertCount.text(alerts.secretAlerts)
const secretLinkValue = `${linkBase}?_t=secrets`
const secretLink = $('a.secret-link')
secretLink.attr('href', secretLinkValue)

var codeAlertCount = $('p.codeAlertCount')
codeAlertCount.text(alerts.codeAlerts)
const codeLinkValue = `${linkBase}?_t=codescanning`
const codeLink = $('a.code-link')
codeLink.attr('href', codeLinkValue)
}

0 comments on commit 4969764

Please sign in to comment.