Skip to content

Commit 59d6a7e

Browse files
kevinajianthechenky
authored andcommitted
Set GCLOUD_PROJECT from FIREBASE_CONFIG when missing (#462)
* Set GCLOUD_PROJECT from FIREBASE_CONFIG when missing * Update changelog * Separated test and updated messages * Moved to setup file
1 parent e6bd9a1 commit 59d6a7e

File tree

6 files changed

+123
-35
lines changed

6 files changed

+123
-35
lines changed

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
important - [breaking change] The Firebase Functions SDK no longer supports Node 6. Developers must use Node 8.13.0 or higher.
22
important - [breaking change] The Firebase Functions SDK no longer supports Firebase Tools SDK below version 6.8.0.
33
important - [breaking change] FIREBASE_PROJECT environment variable is no longer supported and has been supplanted by FIREBASE_CONFIG.
4-
fixed - Fixed bug in Node 10 where events from Node 10 were not parsed properly resulting in undefined.
4+
fixed - Fixed bug in Node 10 where events from Node 10 were not parsed properly resulting in undefined.
5+
fixed - Set GCLOUD_PROJECT from FIREBASE_CONFIG when missing.

integration_test/functions/src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ function callScheduleTrigger(functionName: string, region: string) {
5252
{
5353
method: 'POST',
5454
host: 'cloudscheduler.googleapis.com',
55-
path: `projects/${
56-
firebaseConfig.projectId
57-
}/locations/us-central1/jobs/firebase-schedule-${functionName}-${region}:run`,
55+
path: `projects/${firebaseConfig.projectId}/locations/us-central1/jobs/firebase-schedule-${functionName}-${region}:run`,
5856
headers: {
5957
'Content-Type': 'application/json',
6058
},
@@ -199,9 +197,7 @@ export const integrationTests: any = functions
199197
resp
200198
.status(500)
201199
.send(
202-
`FAIL - details at https://${
203-
process.env.GCLOUD_PROJECT
204-
}.firebaseio.com/testRuns/${testId}`
200+
`FAIL - details at https://${process.env.GCLOUD_PROJECT}.firebaseio.com/testRuns/${testId}`
205201
);
206202
});
207203
});

spec/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import './utils.spec';
3232
import './apps.spec';
3333
import './cloud-functions.spec';
3434
import './config.spec';
35+
import './setup.spec';
3536
import './testing.spec';
3637
import './function-builder.spec';
3738
import './providers/analytics.spec';

spec/setup.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017 Firebase
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import { expect } from 'chai';
24+
import { setup } from '../src/setup';
25+
26+
describe('setup()', () => {
27+
afterEach(() => {
28+
delete process.env.FIREBASE_CONFIG;
29+
delete process.env.GCLOUD_PROJECT;
30+
});
31+
32+
it('sets GCLOUD_PROJECT from FIREBASE_CONFIG', () => {
33+
const testProject = 'test-project';
34+
process.env.FIREBASE_CONFIG = JSON.stringify({
35+
projectId: testProject,
36+
});
37+
setup();
38+
expect(process.env.GCLOUD_PROJECT).to.equal(testProject);
39+
});
40+
41+
it('does not set GCLOUD_PROJECT if already defined', () => {
42+
const existingProject = 'test-project';
43+
process.env.GCLOUD_PROJECT = existingProject;
44+
process.env.FIREBASE_CONFIG = JSON.stringify({
45+
projectId: 'new-project',
46+
});
47+
setup();
48+
expect(process.env.GCLOUD_PROJECT).to.equal(existingProject);
49+
});
50+
});

src/index.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import * as https from './providers/https';
3232
import * as pubsub from './providers/pubsub';
3333
import * as remoteConfig from './providers/remoteConfig';
3434
import * as storage from './providers/storage';
35-
import { firebaseConfig } from './config';
3635
import { handler } from './handler-builder';
36+
import { setup } from './setup';
3737

3838
var app = apps.apps();
3939

@@ -51,34 +51,9 @@ export {
5151
storage,
5252
};
5353

54-
// // Exported root types:
54+
// Exported root types:
5555
export * from './config';
5656
export * from './cloud-functions';
5757
export * from './function-builder';
5858

59-
// TEMPORARY WORKAROUND (BUG 63586213):
60-
// Until the Cloud Functions builder can publish FIREBASE_CONFIG, automatically provide it on import based on what
61-
// we can deduce.
62-
if (!process.env.FIREBASE_CONFIG) {
63-
const cfg = firebaseConfig();
64-
if (cfg) {
65-
process.env.FIREBASE_CONFIG = JSON.stringify(cfg);
66-
} else if (process.env.GCLOUD_PROJECT) {
67-
console.warn(
68-
'Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail'
69-
);
70-
process.env.FIREBASE_CONFIG = JSON.stringify({
71-
databaseURL:
72-
`${process.env.DATABASE_URL}` ||
73-
`https://${process.env.GCLOUD_PROJECT}.firebaseio.com`,
74-
storageBucket:
75-
`${process.env.STORAGE_BUCKET_URL}` ||
76-
`${process.env.GCLOUD_PROJECT}.appspot.com`,
77-
projectId: process.env.GCLOUD_PROJECT,
78-
});
79-
} else {
80-
console.warn(
81-
'Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail'
82-
);
83-
}
84-
}
59+
setup();

src/setup.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017 Firebase
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import { firebaseConfig } from './config';
24+
25+
// Set up for config and vars
26+
export function setup() {
27+
// TEMPORARY WORKAROUND (BUG 63586213):
28+
// Until the Cloud Functions builder can publish FIREBASE_CONFIG, automatically provide it on import based on what
29+
// we can deduce.
30+
if (!process.env.FIREBASE_CONFIG) {
31+
const cfg = firebaseConfig();
32+
if (cfg) {
33+
process.env.FIREBASE_CONFIG = JSON.stringify(cfg);
34+
}
35+
}
36+
37+
// WORKAROUND (BUG 134416569): GCLOUD_PROJECT missing in Node 10
38+
if (!process.env.GCLOUD_PROJECT && process.env.FIREBASE_CONFIG) {
39+
process.env.GCLOUD_PROJECT = JSON.parse(
40+
process.env.FIREBASE_CONFIG
41+
).projectId;
42+
}
43+
44+
// If FIREBASE_CONFIG is still not found, try using GCLOUD_PROJECT to estimate
45+
if (!process.env.FIREBASE_CONFIG) {
46+
if (process.env.GCLOUD_PROJECT) {
47+
console.warn(
48+
'Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail'
49+
);
50+
process.env.FIREBASE_CONFIG = JSON.stringify({
51+
databaseURL:
52+
`${process.env.DATABASE_URL}` ||
53+
`https://${process.env.GCLOUD_PROJECT}.firebaseio.com`,
54+
storageBucket:
55+
`${process.env.STORAGE_BUCKET_URL}` ||
56+
`${process.env.GCLOUD_PROJECT}.appspot.com`,
57+
projectId: process.env.GCLOUD_PROJECT,
58+
});
59+
} else {
60+
console.warn(
61+
'Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail'
62+
);
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)