Skip to content

Commit

Permalink
Feature: template from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ra100 committed Oct 21, 2017
1 parent b9a2de3 commit e72689b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 9 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ just go to options panel and enable or disable features you want.
##### Configure reviewers groups
A "Stash" icon will appear on the top right corner of chrome window. Click on it. It will ask you to add a json to describe which group you want to create with which reviewers.

![GitHub Logo](/docs/configuration_resized.png)
![Configuration](/docs/configuration_resized.png)

Json format is as follow :

Expand All @@ -90,12 +90,20 @@ Json format is as follow :
##### Using centralized lists
If you want to share one list between more users. You need to upload .json file so it is accessible by everyone. Then you just add URL to `URL to json` field. There can be more sources. All lists from files and from JSON field are merged together. Remote lists are reloaded on launch and every 6 hours.

![GitHub Logo](/docs/add_urls.png)
![Add Urls](/docs/add_urls.png)

After that when you will go to pull request creation page or update page a dropdown will appear after reviewers list with a list of groups you defined.

![GitHub Logo](/docs/add_group.png)
![Add Group](/docs/add_group.png)

**Note**: the extension will make a bitbucket server api request to find reviewers. It will simply send the string you added in the reviewers array as search term. Normally if you add email or username as recommanded API should return only one user. You can also enter a name but in this case if the API return more than one user, only the first one will be added.

#### Using centralized template

If you want to share template between more users. You need to upload template
in text format on server accessible to everyone. Then just add URL in template
tab to `Template URL` and save. After save, template is downloaded overwrites
manual template stored. Remote template is reloaded on launch and every 6 hours.

![Template Tab](/docs/configuration_template.png)

Binary file added docs/configuration_template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions extension/chrome/src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ const reloadLists = function() {
}).catch(console.error);
};

const reloadTemplate = function() {
extensionStorage.loadTemplateUrl()
.then(extensionStorage.loadTemplateFromUrl)
.then(template => {
if (!template) {
return
}
return extensionStorage.saveTemplate(template.join('\n'));
})
.then(() => console.info('Teamplate updated'))
.catch(error => {
console.error('Error loading template', error)
})
}

const realoadAll = function() {
reloadLists();
reloadTemplate();
}

let tempTabList = [];
const extensionCommunicationCallback = function(request, sender, callback) {
if (request.action == "xhttp") {
Expand Down Expand Up @@ -236,5 +256,5 @@ chrome.browserAction.onClicked.addListener(function() {
});
});

setInterval(reloadLists, REVIEWERS_LIST_REFRESH);
reloadLists();
setInterval(realoadAll, REVIEWERS_LIST_REFRESH);
realoadAll();
29 changes: 28 additions & 1 deletion extension/chrome/src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ document.addEventListener("DOMContentLoaded", function() {
$('#template_text').value = template.join('\n');
});

extensionStorage.loadTemplateUrl().then(function(templateUrl){
$('#template_url').value = templateUrl;
});

extensionStorage.loadRepoMap().then(function(repoMap){
if(Array.isArray(repoMap) && repoMap.length > 0) {
repoMap.forEach(createNewMapInputs);
Expand Down Expand Up @@ -191,8 +195,31 @@ document.addEventListener("DOMContentLoaded", function() {

function saveTemplate() {
const templateEl = $('#template_text');
const templateUrlEl = $('#template_url');
if (templateUrlEl && templateUrlEl.value) {
const teamplateUrl = templateUrlEl.value
return extensionStorage.loadTemplateFromUrl(teamplateUrl)
.catch(error => {
console.error(error)
throw({msg: 'loading template failed'})
})
.then(template => {
const templateString = template.join('\n')
$('#template_text').value = templateString;
return Promise.all([
extensionStorage.saveTemplateUrl(teamplateUrl),
extensionStorage.saveTemplate(templateString)
])
})
.catch(error => {
throw({msg: error.msg || JSON.stringify(error).message})
})
}
if (templateEl) {
return extensionStorage.saveTemplate(templateEl.value)
return Promise.all([
extensionStorage.saveTemplateUrl(''),
extensionStorage.saveTemplate(templateEl.value)
])
}
return Promise.resolve()
}
Expand Down
43 changes: 42 additions & 1 deletion extension/chrome/src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const extensionStorage = (function() { // eslint-disable-line no-unused-vars
const REVIEWERS_URL_KEY = 'stashplugin.reviewers_url';
const HIPCHAT_KEY = 'stashplugin.hipchat';
const TEMPLATE_KEY = 'stashplugin.template';
const TEMPLATE_URL_KEY = 'stashplugin.template_url';
const BACKGROUNDSTATE_KEY = 'stashplugin.backgroundstate';
const NOTIFSTATE_KEY = 'stashplugin.notifstate';
const NOTIFTYPE_KEY = 'stashplugin.notiftype';
Expand Down Expand Up @@ -63,7 +64,7 @@ const extensionStorage = (function() { // eslint-disable-line no-unused-vars
})
}
}

/**
@method
@memberof storage
Expand Down Expand Up @@ -159,6 +160,10 @@ const extensionStorage = (function() { // eslint-disable-line no-unused-vars
if (!items) {
return loadDefaultTemplate()
}
const templateUrl = items[TEMPLATE_URL_KEY];
if (templateUrl) {
return loadTemplateFromUrl(templateUrl)
}
const template = items[TEMPLATE_KEY];
if (!template) {
return loadDefaultTemplate()
Expand All @@ -167,12 +172,45 @@ const extensionStorage = (function() { // eslint-disable-line no-unused-vars
}
});
}

function saveTemplate(string) {
const data = {};
data[TEMPLATE_KEY] = string.split('\n');
return storagePromised.set(data);
}

function loadTemplateFromUrl(url) {
if (!url) {
return Promise.resolve()
}
return fetch(url)
.then(res => res.text())
.then(data =>
data.replace("\r", '').split("\n"))
.catch(error => Promise.reject(`Error loading template ${error.toString()}`))
}

function loadTemplateUrl() {
return storagePromised.get()
.then(function(items) {
if (!items) {
return ''
}
const templateUrl = items[TEMPLATE_URL_KEY];
if (!templateUrl) {
return ''
} else {
return templateUrl;
}
});
}

function saveTemplateUrl(string) {
const data = {};
data[TEMPLATE_URL_KEY] = string;
return storagePromised.set(data);
}

function loadHipChatUsername() {
return storagePromised.get()
.then(function(items){
Expand Down Expand Up @@ -256,6 +294,9 @@ const extensionStorage = (function() { // eslint-disable-line no-unused-vars
loadUrl,
loadTemplate,
saveTemplate,
loadTemplateFromUrl,
loadTemplateUrl,
saveTemplateUrl,
loadHipChatUsername,
saveHipChatUsername,
loadBackgroundState,
Expand Down
2 changes: 1 addition & 1 deletion extension/chrome/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Bitbucket Server Extension",
"description": "Allow to add group of reviewers for pull request in bitbucket server + other features",
"version": "2.2.11",
"version": "2.2.12",
"permissions": [
"storage",
"alarms",
Expand Down
4 changes: 4 additions & 0 deletions extension/chrome/src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<label for="template_text">Template:</label>
<textarea class="form-control" rows="10" id="template_text"></textarea>
</div>
<div class="form-group">
<label for="template_url">Template URL:</label>
<input class="form-control" id="template_url" type="text"></input>
</div>
</div>

<div id="notification" class="tab-pane">
Expand Down
4 changes: 4 additions & 0 deletions history
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.2.12
===================
- feature: add support for centralized template

2.2.11
===================
- bugfix #31: extension doesnt work with uninitialized 'url to json' in options panel
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.11
2.2.12

0 comments on commit e72689b

Please sign in to comment.