Skip to content

Commit

Permalink
Merge pull request #260 from ljay79/develop
Browse files Browse the repository at this point in the history
Merge into Master
  • Loading branch information
ljay79 authored Jun 24, 2020
2 parents 7b81062 + 5580a57 commit 2a20de9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @OnlyCurrentDoc Limits the script to only accessing the current spreadsheet.
*/

var BUILD = '1.4.5';
var BUILD = '1.4.6';

/**
* Add a nice menu option for the users.
Expand Down
15 changes: 7 additions & 8 deletions src/customFunctions.gs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function customFunctionAllowed_() {
throw new Error("The document owner (you) must enable custom functions. Open 'Add-ons > Project Aid for Jira > Settings' and toggle 'Custom Functions' to enabled. If you are not the document owner, ask him to enable custom functions.");
}

// no return value; throws Error is feature is suspended
// no return value; throws Error if feature is suspended
customFunctionsSuspended_();
}

Expand Down Expand Up @@ -241,7 +241,6 @@ CustomFunctionErrorException.prototype._countHandler = function() {


/**
* @TODO: Not activated yet - soft launch with logging before lauching such critical code
* @desc Check for CustomFunctions Error count and decide to suspend any further calls for a while or not.
* @throws Error
* @return void
Expand All @@ -255,7 +254,7 @@ function customFunctionsSuspended_() {
var _timeUntil = docProps.get(key_time);
var timeUntil = new Date();

console.info('customFunctionsSuspended_(): Counter is at: %s', count);
debug.info('customFunctionsSuspended_(): Counter is at: %s', count);

if (_timeUntil != null) {
// suspension time is set, convert to Date object
Expand All @@ -268,27 +267,27 @@ function customFunctionsSuspended_() {
if (timeUntilSeconds > (nowSeconds+3)) {
var _delay_seconds = timeUntilSeconds - nowSeconds;
var _msg = "Suspension of custom functions for about " + _delay_seconds + " seconds because of to many errors! Please correct all your custom function calls in this document and wait before re-trying.";
console.info("customFunctionsSuspended_():" + _msg + " Now: %s < Until: %s", now.toString(), timeUntil.toString());
//@TODO: throw new Error(_msg); // NOT YET ACTIVE
debug.warn("customFunctionsSuspended_():" + _msg + " Now: %s < Until: %s", now.toString(), timeUntil.toString());
throw new Error(_msg);
}

// else
if (count >= 100) {
// set suspension +300s
console.info('customFunctionsSuspended_(): ... setting 300s suspension!');
debug.info('customFunctionsSuspended_(): ... setting 300s suspension!');
docProps.put(key_time, now.getTime() + (300*1000));

// reset error counter
docProps.put(key_count, 0);

} else if (count >= 25 && count < 30) {
// set suspension +300s
console.info('customFunctionsSuspended_(): ... setting 60s suspension!');
debug.info('customFunctionsSuspended_(): ... setting 60s suspension!');
docProps.put(key_time, now.getTime() + (60*1000));

} else if (count >= 10 && count < 15) {
// set suspension +30s
console.info('customFunctionsSuspended_(): ... setting 30s suspension!');
debug.info('customFunctionsSuspended_(): ... setting 30s suspension!');
docProps.put(key_time, now.getTime() + (30*1000));
}

Expand Down
2 changes: 1 addition & 1 deletion src/debug.gs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ StorageCounter = {
},

log: function() {
console.info("StorageCounter: %o", JSON.parse(this._cache.get(this._id)));
debug.info("StorageCounter: %o", JSON.parse(this._cache.get(this._id)));
}

};
Expand Down
3 changes: 2 additions & 1 deletion src/jiraApi.gs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ var restMethods = {
'filter' : {method: '/filter/{filterId}'},
//'search': {method: '/search', queryparams: {jql:'', fields: [], properties: [], maxResults: 100, validateQuery: 'strict'}} // GET
'search' : {method: '/search'}, // POST
'myFilters' : {method: '/filter/my', queryparams: {includeFavourites: 'true'}},
// https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-filter-search-get
'myFilters' : {method: '/filter/search', queryparams: {expand: 'favourite,jql,owner', startAt:0, maxResults: 100, orderBy: 'IS_FAVOURITE'}},

// https://SITENAME.atlassian.net/rest/api/2/user/search?startAt=0&maxResults=1000&query=
'userSearch' : {method: '/user/search', queryparams: {startAt:0, maxResults: 250, username:''}},
Expand Down
7 changes: 6 additions & 1 deletion src/jiraCommon.gs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ function getMyFilters() {
debug.log("getMyFilters()->ok(): %s", responseData);

if (responseData) {
/*
* responseData = array (server: api v2 /filter/favourite)
* responseData.values = array (cloud: api v2 /filter/search)
*/
var values = responseData.values ? responseData.values : responseData;
// add data to export
filters.list.push.apply(filters.list, responseData.map(function (filter) {
filters.list.push.apply(filters.list, values.map(function (filter) {
return {
id: parseInt(filter.id),
name: filter.name,
Expand Down
13 changes: 6 additions & 7 deletions test/customFunctions.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
CacheService = require('test/mocks/CacheService');
const getCfg_ = require("../src/settings.gs").getCfg_;
const setCfg_ = require("../src/settings.gs").setCfg_;
const customFunctions = require('src/customFunctions.gs');

beforeEach(() => {
jest.resetModules();
CacheService.resetMocks();
CacheService.resetMockUserData();
});


test("customFunctionAllowed_ throws exceptions", () => {
// custom_fn_enabled not defined
setCfg_('custom_fn_enabled', null);
Expand All @@ -32,6 +31,7 @@ test("customFunctionsSuspended_ for 30 seconds", () => {
var key_count = 'CUSTOM_FUNCTIONS_ERROR_COUNT';
var key_time = 'CUSTOM_FUNCTIONS_ERROR_TIME';
var now = new Date(), suspension = null, seconds = 0;

// custom_fn_enabled enabled
setCfg_('custom_fn_enabled', 1)

Expand All @@ -47,22 +47,22 @@ test("customFunctionsSuspended_ for 30 seconds", () => {
expect(seconds).toBeGreaterThanOrEqual(30);
expect(seconds).toBeLessThan(40);

// not yet
//expect(customFunctions.customFunctionAllowed_).toThrowError();
expect(customFunctions.customFunctionAllowed_).toThrowError();
});

test("customFunctionsSuspended_ for 60 seconds", () => {
var docProps = CacheService.getDocumentCache();
var key_count = 'CUSTOM_FUNCTIONS_ERROR_COUNT';
var key_time = 'CUSTOM_FUNCTIONS_ERROR_TIME';
var now = new Date(), suspension = null, seconds = 0;

// custom_fn_enabled enabled
setCfg_('custom_fn_enabled', 1)

/* error count = >= 25 */
docProps.put(key_count, 25, 60*60);
now = new Date(); suspension = new Date();

expect(customFunctions.customFunctionAllowed_).not.toThrowError();
// verify suspension time
suspension.setTime( docProps.get(key_time) );
Expand All @@ -71,8 +71,7 @@ test("customFunctionsSuspended_ for 60 seconds", () => {
expect(seconds).toBeGreaterThanOrEqual(60);
expect(seconds).toBeLessThan(70);

// not yet
//expect(customFunctions.customFunctionAllowed_).toThrowError();
expect(customFunctions.customFunctionAllowed_).toThrowError();
});

test("customFunctionsSuspended_ for 300 seconds", () => {
Expand Down

0 comments on commit 2a20de9

Please sign in to comment.