-
Notifications
You must be signed in to change notification settings - Fork 528
/
storage.js
123 lines (116 loc) · 5.45 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Client-side local storage for Blue Ocean.
* See https://tfennelly.github.io/jenkins-js-storage/
*/
import * as storage from '@jenkins-cd/storage';
import { blueocean } from './scopes';
import config from './config';
export const jenkinsNS = storage.jenkinsNamespace();
export const installInfo = jenkinsNS.subspace('installInfo');
/**
* Check do we need to clear the jenkinsNS.
* <p>
* Simple process of checking if the version OR list of plugins
* stored (from the last change) have changed based on what was
* delivered with this page.
* <p>
* Internal use only. Exported for testing purposes only.
* @param {string} installVersion The version of the Jenkins instance that's running now
* i.e. that loaded this page.
* @param {Array} installPluginList The list of active plugins installed in the Jenkins instance
* that's running now i.e. that loaded this page.
* @private
*/
export const _clearJenkinsNS = (installVersion, installPluginList) => {
// Info about the Jenkins that was running the last time we loaded this page.
const storedVersion = installInfo.get('version');
const storedPluginList = installInfo.get('plugins');
const doClear = (because) => {
jenkinsNS.clear();
installInfo.set('version', installVersion);
installInfo.set('plugins', installPluginList);
installInfo.set('lastcleared', {
at: Date.now(),
because,
});
};
try {
if (storedVersion && storedPluginList) {
// compare the Jenkins version
if (installVersion !== storedVersion) {
doClear(`Jenkins versions did not match. installVersion: ${installVersion}, storedVersion: ${storedVersion}`);
} else {
// compare the plugin lists
if (installPluginList.length !== storedPluginList.length) {
// Different number of active plugins.
// No need to check the names and versions.
doClear('Different number of active plugins');
} else {
// Same number of plugins. Lets check that they all
// match up i.e. that we can find each plugin in each list and
// that the versions match.
try {
installPluginList.forEach((installedPlugin) => {
let found = false;
storedPluginList.forEach((storedPlugin) => {
if (storedPlugin.hpiPluginId === installedPlugin.hpiPluginId) {
// same plugin.
found = true;
// Check the versions.
if (storedPlugin.hpiPluginVer !== installedPlugin.hpiPluginVer) {
throw new Error(`Different plugin versions for plugin ${installedPlugin.hpiPluginId}`);
}
}
});
if (!found) {
throw new Error(`New plugin installed ${installedPlugin.hpiPluginId}`);
}
});
} catch (e) {
// One of the plugins has been updated or removed.
// See Errors thrown inside above try/catch.
doClear(e.message);
}
}
}
} else {
// Theoretically no need to clear in this case,
// but lets do it anyway.
doClear('No Jenkins info stored. Clearing anyway, just in case.');
}
} catch (e) {
console.error('Unexpected error while checking/clearing Jenkins instance client-side storage. Clearing as a precaution.', e);
doClear(`Unexpected error while checking/clearing Jenkins instance client-side storage: ${e.message}`);
}
};
// Call the clear function automatically.
const installVersion = config.getJenkinsConfig().version;
const installPluginList = blueocean.jsExtensions;
if (installVersion && installPluginList) {
_clearJenkinsNS(installVersion, installPluginList);
} else {
console.warn('Unexpected state. Blue Ocean preload state not on page as expected. This is okay if running in a test.');
}