Skip to content

Commit 4513f41

Browse files
authored
fix(medic#8038): use builds url from env var
1 parent 9643709 commit 4513f41

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

admin/src/js/controllers/upgrade.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const BUILDS_DB = 'https://staging.dev.medicmobile.org/_couch/builds_4';
1+
const DEFAULT_BUILDS_URL = 'https://staging.dev.medicmobile.org/_couch/builds_4';
22

33
angular.module('controllers').controller('UpgradeCtrl',
44
function(
@@ -20,14 +20,15 @@ angular.module('controllers').controller('UpgradeCtrl',
2020
$scope.loading = true;
2121
$scope.versions = {};
2222
$scope.upgraded = $state.params.upgraded;
23-
const buildsDb = pouchDB(BUILDS_DB);
2423

2524
const UPGRADE_URL = '/api/v2/upgrade';
2625
const POLL_URL = '/setup/poll';
2726
const UPGRADE_POLL_FREQ = 2000;
2827
const BUILD_LIST_LIMIT = 50;
2928
const UPGRADE_CONTAINER_WAIT_PERIOD = 3 * 60 * 1000; // 3 minutes
29+
3030
let containerWaitPeriodTimeout;
31+
let apiBuildsUrl;
3132

3233
const logError = (error, key) => {
3334
return $translate
@@ -68,14 +69,15 @@ angular.module('controllers').controller('UpgradeCtrl',
6869
const getCurrentUpgrade = () => {
6970
return $http
7071
.get(UPGRADE_URL)
71-
.then(({ data: { upgradeDoc, indexers } }) => {
72+
.then(({ data: { upgradeDoc, indexers, buildsUrl } }) => {
7273
if ($scope.upgradeDoc && !upgradeDoc) {
7374
const expectedVersion = $scope.upgradeDoc.to && $scope.upgradeDoc.to.build;
7475
getExistingDeployment(true, expectedVersion);
7576
}
7677

7778
$scope.upgradeDoc = upgradeDoc;
7879
$scope.indexerProgress = indexers;
80+
apiBuildsUrl = buildsUrl;
7981

8082
if (upgradeDoc) {
8183
$timeout(getCurrentUpgrade, UPGRADE_POLL_FREQ);
@@ -109,6 +111,7 @@ angular.module('controllers').controller('UpgradeCtrl',
109111

110112
const loadBuilds = () => {
111113
const minVersion = Version.minimumNextRelease($scope.currentDeploy.version);
114+
const buildsDb = pouchDB(apiBuildsUrl || DEFAULT_BUILDS_URL);
112115

113116
// NB: Once our build server is on CouchDB 2.0 we can combine these three calls
114117
// See: http://docs.couchdb.org/en/2.0.0/api/ddoc/views.html#sending-multiple-queries-to-a-view
@@ -141,6 +144,11 @@ angular.module('controllers').controller('UpgradeCtrl',
141144
])
142145
.then(([ branches, betas, releases, featureReleases ]) => {
143146
$scope.versions = { branches, betas, releases, featureReleases };
147+
buildsDb.close();
148+
})
149+
.catch(err => {
150+
buildsDb.close();
151+
throw err;
144152
});
145153
};
146154

admin/tests/unit/controllers/upgrade.spec.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('UpgradeCtrl controller', () => {
1212
let timeout;
1313
let translate;
1414
let state;
15+
let pouchDb;
1516

1617
const nextTick = () => new Promise(r => setTimeout(r));
1718

@@ -27,8 +28,10 @@ describe('UpgradeCtrl controller', () => {
2728

2829
buildsDb = {
2930
query: sinon.stub(),
31+
close: sinon.stub()
3032
};
31-
const pouchDb = sinon.stub().returns(buildsDb);
33+
pouchDb = sinon.stub();
34+
pouchDb.returns(buildsDb);
3235
modal = sinon.stub();
3336
http = {
3437
get: sinon.stub(),
@@ -126,6 +129,8 @@ describe('UpgradeCtrl controller', () => {
126129
createController();
127130
await scope.setupPromise;
128131

132+
expect(pouchDb.callCount).to.equal(1);
133+
expect(pouchDb.args[0][0]).to.equal('https://staging.dev.medicmobile.org/_couch/builds_4');
129134
expect(scope.loading).to.equal(false);
130135
expect(scope.versions).to.deep.equal({
131136
branches: [{ version: 'branch1' }, { version: 'branch2' }],
@@ -161,6 +166,77 @@ describe('UpgradeCtrl controller', () => {
161166
limit: 50,
162167
}
163168
]);
169+
expect(buildsDb.close.callCount).to.equal(1);
170+
});
171+
172+
it('should load builds from configured builds url', async () => {
173+
const deployInfo = { the: 'deplopy info', version: '4.1.0' };
174+
Object.freeze(deployInfo);
175+
176+
http.get.withArgs('/api/deploy-info').resolves({ data: deployInfo });
177+
http.get.withArgs('/api/v2/upgrade').resolves({ data: { upgradeDoc: undefined, indexers: [], buildsUrl: 'https://mybuildurl.com' } });
178+
version.minimumNextRelease.returns({ major: 4, minor: 1, patch: 1, beta: 0 });
179+
180+
buildsDb.query.onCall(0).resolves({
181+
rows: [
182+
{ id: 'medic:medic:branch1', value: { version: 'branch1' } },
183+
{ id: 'medic:medic:branch2', value: { version: 'branch2' } },
184+
],
185+
});
186+
buildsDb.query.onCall(1).resolves({
187+
rows: [
188+
{ id: 'medic:medic:beta1', value: { version: 'beta1' } },
189+
{ id: 'medic:medic:beta2', value: { version: 'beta2' } },
190+
],
191+
});
192+
buildsDb.query.onCall(2).resolves({
193+
rows: [
194+
{ id: 'medic:medic:release1', value: { version: 'release1' } },
195+
{ id: 'medic:medic:release2', value: { version: 'release2' } },
196+
],
197+
});
198+
199+
createController();
200+
await scope.setupPromise;
201+
202+
expect(pouchDb.callCount).to.equal(1);
203+
expect(pouchDb.args[0][0]).to.equal('https://mybuildurl.com');
204+
expect(scope.loading).to.equal(false);
205+
expect(scope.versions).to.deep.equal({
206+
branches: [{ version: 'branch1' }, { version: 'branch2' }],
207+
betas: [{ version: 'beta1' }, { version: 'beta2' }],
208+
releases: [{ version: 'release1' }, { version: 'release2' }],
209+
featureReleases: [],
210+
});
211+
expect(buildsDb.query.callCount).to.equal(3);
212+
expect(buildsDb.query.args[0]).to.deep.equal([
213+
'builds/releases',
214+
{
215+
startkey: [ 'branch', 'medic', 'medic', {}],
216+
endkey: [ 'branch', 'medic', 'medic'],
217+
descending: true,
218+
limit: 50,
219+
}
220+
]);
221+
expect(buildsDb.query.args[1]).to.deep.equal([
222+
'builds/releases',
223+
{
224+
startkey: [ 'beta', 'medic', 'medic', {}],
225+
endkey: [ 'beta', 'medic', 'medic', 4, 1, 1, 0 ],
226+
descending: true,
227+
limit: 50,
228+
}
229+
]);
230+
expect(buildsDb.query.args[2]).to.deep.equal([
231+
'builds/releases',
232+
{
233+
startkey: [ 'release', 'medic', 'medic', {}],
234+
endkey: [ 'release', 'medic', 'medic', 4, 1, 1 ],
235+
descending: true,
236+
limit: 50,
237+
}
238+
]);
239+
expect(buildsDb.close.callCount).to.equal(1);
164240
});
165241

166242
it('should catch couch query errors', async () => {
@@ -185,6 +261,7 @@ describe('UpgradeCtrl controller', () => {
185261

186262
expect(scope.loading).to.equal(false);
187263
expect(scope.versions).to.deep.equal({});
264+
expect(buildsDb.close.callCount).to.equal(1);
188265
});
189266

190267
it('should follow upgrade if already in progress', async () => {

api/src/controllers/upgrade.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const environment = require('../environment');
12
const auth = require('../auth');
23
const serverUtils = require('../server-utils');
34

@@ -40,7 +41,7 @@ const upgradeInProgress = (req, res) => {
4041
service.indexerProgress(),
4142
]))
4243
.then(([upgradeDoc, indexers]) => {
43-
res.json({ upgradeDoc, indexers });
44+
res.json({ upgradeDoc, indexers, buildsUrl: environment.buildsUrl });
4445
})
4546
.catch(err => {
4647
if (err && err.error && err.error.code === 'ECONNREFUSED') {

api/tests/mocha/controllers/upgrade.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const should = require('chai').should();
22

33
const sinon = require('sinon');
4+
const environment = require('../../../src/environment');
45
const auth = require('../../../src/auth');
56
const serverUtils = require('../../../src/server-utils');
67

@@ -27,9 +28,14 @@ describe('Upgrade controller', () => {
2728
sinon.stub(serverUtils, 'error');
2829
sinon.stub(service, 'upgrade').resolves();
2930
sinon.stub(service, 'complete').resolves();
31+
32+
environment.buildsUrl = 'https://mybuild.com';
3033
});
3134

32-
afterEach(() => sinon.restore());
35+
afterEach(() => {
36+
environment.buildsUrl = '';
37+
sinon.restore();
38+
});
3339

3440
describe('Upgrade', () => {
3541
it('checks that user has the right permissions', () => {
@@ -193,6 +199,7 @@ describe('Upgrade controller', () => {
193199
service.indexerProgress.callCount.should.equal(1);
194200
res.json.callCount.should.equal(1);
195201
res.json.args[0].should.deep.equal([ {
202+
buildsUrl: 'https://mybuild.com',
196203
upgradeDoc: { the: 'upgrade' },
197204
indexers: ['indexers'],
198205
}]);

api/tests/mocha/routing.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Routing', () => {
1414
const environment = rewire('./../../src/environment');
1515
const adminUpgrade = rewire('./../../../admin/src/js/controllers/upgrade');
1616
const cspBuildDb = environment.__get__('DEFAULT_BUILDS_URL');
17-
const actualBuildDb = adminUpgrade.__get__('BUILDS_DB');
17+
const actualBuildDb = adminUpgrade.__get__('DEFAULT_BUILDS_URL');
1818
chai.expect(cspBuildDb).to.not.eq(undefined);
1919
chai.expect(cspBuildDb).to.include(actualBuildDb);
2020
});

0 commit comments

Comments
 (0)