Skip to content

Commit

Permalink
Add status surrogates 11 - config
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Apr 7, 2024
1 parent d76e56a commit 473224d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 10 deletions.
19 changes: 16 additions & 3 deletions dashgit-web/app/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,22 @@ const cache = {

// to be called on cache reset (at startup and config changes)
resetStatusSurrogates: function (providers) {
// changes this default empty configuration if any surrogate has been defined
// temporal fixed config for test
this.statusSurrogates["1-github"] = "0-github";
this.statusSurrogates = this.getEnabledSurrogates(providers);
},
getEnabledSurrogates: function (providers) {
let surrogates = {};
for (let provider of providers) { // ensure that surrogated and surrogate are linked and enabled
if (provider.enabled && provider.statusSurrogateUser != "") {
for (let surrogate of providers) { // surrogate is the first enabled that match url and user
if (provider.uid != surrogate.uid && surrogate.enabled
&& provider.provider == surrogate.provider && provider.url == surrogate.url && provider.statusSurrogateUser == surrogate.user) {
surrogates[provider.uid] = surrogate.uid;
break;
}
}
}
}
return surrogates;
},
hasStatusSurrogate: function (providerId) {
return this.statusSurrogates[providerId] != undefined;
Expand Down
1 change: 1 addition & 0 deletions dashgit-web/app/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const config = {
this.setDefault(element, "enabled", true);
this.setDefault(element, "enableNotifications", true);
this.setDefault(element, "filterIfLabel", "");
this.setDefault(element, "statusSurrogateUser", "");
if (element.provider == "GitHub") {
this.setDefault(element, "url", "https://github.com");
this.setDefault(element, "api", "https://api.github.com");
Expand Down
17 changes: 11 additions & 6 deletions dashgit-web/app/ConfigController.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ $(document).on('click', '.config-btn-provider-up', function (e) {
$(document).on('change', '#config-common-enableManagerRepo', function (e) {
configView.setToggleDependencies();
});
$(document).on('change', '[id^="config-providers-surrogate-enabled-"]', function (e) {
configView.setToggleProviderSurrogate(this, $(this).is(':checked'));
});

// Data update events

$(document).on('click', '.config-btn-provider-submit', function (e) {
if ($(".config-form")[0].checkValidity()) {
console.log("Saving config data");
configController.saveData();
configController.afterSaveData();
e.preventDefault();
} else
console.log("Can't save config data due to validation issues");
Expand Down Expand Up @@ -84,11 +88,7 @@ $(document).on('click', '#buttonConfigSave', function (e) {
console.log("Save config json");
try {
config.updateFromString($("#configJson").val());
configController.updateMainTarget();
//to have a refresh effect when changing later to another tab
wiController.reset(true);
wiView.reset();
configController.displayToast("Configuration saved");
configController.afterSaveData();
} catch (error) {
wiView.renderAlert("danger", error);
}
Expand Down Expand Up @@ -118,8 +118,13 @@ const configController = {

// Replace the global config data with the local config data value, it is assumed that all data was validated at the ui
config.updateFromString(JSON.stringify(data));
},
afterSaveData: function () {
configController.updateMainTarget();
this.displayToast("Configuration saved");
configController.displayToast("Configuration saved");
//to force a refresh when changing later to another tab
wiController.reset(true);
wiView.reset();
},

displayToast: function (message) {
Expand Down
24 changes: 23 additions & 1 deletion dashgit-web/app/ConfigView.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const configView = {
$("#config-form").html(html);
this.setMoveStatus();
this.setToggleDependencies(); // eg. visibility of elements that depends on a checkbox
for (let i = 0; i < data.providers.length; i++) // toggle between surrogate user and graphql parameters
this.setToggleProviderSurrogate($(`#config-providers-statusSurrogateUser-${i}`), data.providers[i].statusSurrogateUser!="");
// activate tooltips at the input labels
$(`.info-icon`).tooltip({ delay: 200 });
},
Expand Down Expand Up @@ -119,8 +121,11 @@ const configView = {
${this.check2html(`config-providers-enableNotifications-${key}`, "Show notifications", provider.enableNotifications)}
</div>
<div class="card-subtitle h6 mb-1 mt-1 text-body-secondary">GraphQL API parameters:</div>
<div class="row">
<div class="col-auto card-subtitle h6 mb-1 mt-1 text-body-secondary">GraphQL API parameters:</div>
${this.check2html(`config-providers-surrogate-enabled-${key}`, "Use a status surrogate", provider.statusSurrogateUser != "")}
</div>
<div class="row config-providers-graphql-settings">
${this.input2html(`config-graphql-maxProjects-${key}`, "number", "Max projects", provider.graphql.maxProjects, 'required min="2" max="100"', "150", "100",
"Maximum number of repositories/projects that are retrieved to get the branches and statuses")}
${this.input2html(`config-graphql-maxBranches-${key}`, "number", "Max branches", provider.graphql.maxBranches, 'required min="2" max="100"', "150", "100",
Expand All @@ -134,6 +139,10 @@ const configView = {
+ this.check2html(`config-graphql-scope-collaborator-${key}`, "Collaborator", provider.graphql.ownerAffiliations.includes("COLLABORATOR")))
}
</div>
<div class="row config-providers-surrogate-settings">
${this.input2html(`config-providers-statusSurrogateUser-${key}`, "text", "Username of the status surrogate provider", provider.statusSurrogateUser, '', "350", "150",
"The provider with this username and same repository url will be used to get the statuses, instead of calling the GraphQL API")}
</div>
<div class="config-provider-updates-div-container">
<div class="card-subtitle h6 mb-1 mt-1 text-body-secondary">Combined dependency updates, additional parameters:
Expand Down Expand Up @@ -196,6 +205,7 @@ const configView = {
provider.unassignedAdditionalOwner = $(`#config-providers-unassignedAdditionalOwner-${id}`).val().trim().split(" ");
provider.dependabotAdditionalOwner = $(`#config-providers-dependabotAdditionalOwner-${id}`).val().trim().split(" ");
provider.enableNotifications = $(`#config-providers-enableNotifications-${id}`).is(':checked');
provider.statusSurrogateUser = $(`#config-providers-statusSurrogateUser-${id}`).val().trim();

provider.graphql.maxProjects = $(`#config-graphql-maxProjects-${id}`).val().trim();
provider.graphql.maxBranches = $(`#config-graphql-maxBranches-${id}`).val().trim();
Expand Down Expand Up @@ -237,6 +247,18 @@ const configView = {
$(`.config-provider-updates-div-container`).hide();
}
},
setToggleProviderSurrogate: function(check, checked) {
let providerRoot=$(check).closest(".config-provider-card");
if (checked) {
$(providerRoot).find(".config-providers-graphql-settings").hide();
$(providerRoot).find(".config-providers-surrogate-settings").show();
} else {
// if unchecked, also cleans the surrogate user field
$(providerRoot).find('input[id^="config-providers-statusSurrogateUser-"]').val("");
$(providerRoot).find(".config-providers-graphql-settings").show();
$(providerRoot).find(".config-providers-surrogate-settings").hide();
}
},

addProvider: function (provider) {
//get latest id to increase it in the new provider
Expand Down
57 changes: 57 additions & 0 deletions dashgit-web/test/TestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import assert from 'assert';
import { config } from "../app/Config.js"
import { cache } from "../app/Cache.js"

describe("TestConfig - Sanitizing config data", async function () {

Expand All @@ -31,6 +32,7 @@ describe("TestConfig - Sanitizing config data", async function () {
provider: 'GitHub', uid: '', user: '', token: '', enabled: true,
url: 'https://github.com', api: 'https://api.github.com',
enableNotifications: true,
statusSurrogateUser: "",
filterIfLabel: '', unassignedAdditionalOwner: [], dependabotAdditionalOwner: [],
updates: { tokenSecret: "", userEmail: "" },
graphql: { ownerAffiliations: ['OWNER'], maxProjects: 20, maxBranches: 10 }
Expand All @@ -48,6 +50,7 @@ describe("TestConfig - Sanitizing config data", async function () {
provider: 'GitHub', uid: 'repo_user_id', user: 'user', token: 'XXXXXXXXXXXX', enabled: false,
url: 'https://github.com', api: 'https://api.github.com',
enableNotifications: true,
statusSurrogateUser: "",
filterIfLabel: 'lbl', unassignedAdditionalOwner: [], dependabotAdditionalOwner: ["org1", "org2"],
updates: { tokenSecret: "DASHGIT_GITHUB_USER_TOKEN", userEmail: "" },
graphql: { ownerAffiliations: ['OWNER', 'ORGANIZATION_MEMBER'], maxProjects: 10, maxBranches: 20 }
Expand All @@ -56,4 +59,58 @@ describe("TestConfig - Sanitizing config data", async function () {
assert.deepEqual(expected, config.parseAndSanitizeData(JSON.stringify(expected)));
});

// - surrogate match before/after surrogated
// - surrogate does not match because (no surrogate configured, user, url, surrogate disabled, surrogated disabled)
// - multiple potential surrogated (two/only one enabled)
// - multiple surrogated
it("Find effective provider status surrogates as configured by cache", function () {
let providers = getSurrogates();
assert.deepEqual({}, cache.getEnabledSurrogates(providers)); // baseline: no surrogate configured
providers = getSurrogates();
providers[2].statusSurrogateUser = "gh1";
assert.deepEqual({ "2-github": "0-github" }, cache.getEnabledSurrogates(providers)); // match before surrogated
providers = getSurrogates();
providers[0].statusSurrogateUser = "gh3";
assert.deepEqual({ "0-github": "2-github" }, cache.getEnabledSurrogates(providers)); // match, after surrogated

providers = getSurrogates();
providers[2].statusSurrogateUser = "gh4";
assert.deepEqual({}, cache.getEnabledSurrogates(providers)); // no match because: user
providers = getSurrogates();
providers[2].statusSurrogateUser = "gh1";
providers[0].url = "http://other.com";
assert.deepEqual({}, cache.getEnabledSurrogates(providers)); // no match because: url
providers = getSurrogates();
providers[2].statusSurrogateUser = "gh1";
providers[0].enabled = false;
assert.deepEqual({}, cache.getEnabledSurrogates(providers)); // no match because: surrogate disabled
providers = getSurrogates();
providers[2].statusSurrogateUser = "gh1";
providers[2].enabled = false;
assert.deepEqual({}, cache.getEnabledSurrogates(providers)); // no match because: surrogated disabled

providers = getSurrogates();
providers[1].user = "gh1";
providers[2].statusSurrogateUser = "gh1";
assert.deepEqual({ "2-github": "0-github" }, cache.getEnabledSurrogates(providers)); // multiple candidates
providers = getSurrogates();
providers[0].enabled = false;
providers[1].user = "gh1";
providers[2].statusSurrogateUser = "gh1";
assert.deepEqual({ "2-github": "1-github" }, cache.getEnabledSurrogates(providers)); // multiple candidates, one disabled
providers = getSurrogates();
providers[2].statusSurrogateUser = "gh1";
providers[3].statusSurrogateUser = "gl2";
assert.deepEqual({ "2-github": "0-github", "3-gitlab": "4-gitlab" }, cache.getEnabledSurrogates(providers)); // multiple surrogated
});
function getSurrogates() {
return [
{ provider: 'GitHub', user: 'gh1', statusSurrogateUser: "", uid: '0-github', enabled: true, url: 'https://github.com' },
{ provider: 'GitHub', user: 'gh2', statusSurrogateUser: "", uid: '1-github', enabled: true, url: 'https://github.com' },
{ provider: 'GitHub', user: 'gh3', statusSurrogateUser: "", uid: '2-github', enabled: true, url: 'https://github.com' },
{ provider: 'GitLab', user: 'gl1', statusSurrogateUser: "", uid: '3-gitlab', enabled: true, url: 'https://gitlab.com' },
{ provider: 'GitLab', user: 'gl2', statusSurrogateUser: "", uid: '4-gitlab', enabled: true, url: 'https://gitlab.com' },
];
};

});

0 comments on commit 473224d

Please sign in to comment.