Skip to content

Commit

Permalink
Test commit on a throwaway branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace Nassri committed Jan 18, 2018
1 parent 70277ed commit 85463da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 96 deletions.
97 changes: 7 additions & 90 deletions functions/autodeploy/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, Inc.
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -13,99 +13,16 @@
* limitations under the License.
*/

// [START functions_autodeploy]
const google = require('googleapis');

// TODO(developer) configure these settings
const PROJECT_ID = process.env.GCLOUD_PROJECT;
const GCF_REGION = '[YOUR_GCF_REGION]';
const REPO_NAME = `[YOUR_REPO_NAME]`;
const SOURCE_PATH = `functions/directory/`; // Must be a directory, not a file
const FUNCTION_NAME = `[YOUR_FUNCTION_NAME]`;
const BRANCH_NAME = `master`; // Change this to deploy from a different branch

// Create Cloud Source Repository URL to the directory to deploy from
const folderUrl = `https://source.developers.google.com/projects/${PROJECT_ID}/repos/${REPO_NAME}/moveable-aliases/${BRANCH_NAME}/paths/${SOURCE_PATH}`;

// Helper function to create/get an OAuth 2.0 client instance
let _client = null;
function getOAuthClient () {
return new Promise((resolve, reject) => {
if (!_client) {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
reject(err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped(['https://www.googleapis.com/auth/cloud-platform']);
}
_client = google.cloudfunctions({
version: 'v1beta2',
auth: authClient
});
resolve(_client);
});
} else {
// Use existing client instance
resolve(_client);
}
});
}
'use strict';

// [START functions_helloworld_get]
/**
* HTTP Cloud Function that, when triggered, deploys functions from a Cloud
* Source Repository to Google Cloud Functions.
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.deployToGCF = (req, res) => {
// Create configuration objects
const location = `projects/${PROJECT_ID}/locations/${GCF_REGION}`;
const resource = {
sourceRepositoryUrl: folderUrl,
name: `${location}/functions/${FUNCTION_NAME}`,
httpsTrigger: {}
};

return getOAuthClient()
.then((oauthClient) => {
return oauthClient.projects.locations.functions;
})
.then((functionsClient) => {
// Create the Cloud Function if it doesn't exist
return new Promise((resolve, reject) => {
functionsClient.create({ resource, location }, (err) => {
if (err &&
err.errors &&
err.errors[0] &&
err.errors[0].reason === 'alreadyExists') {
return resolve(functionsClient);
} else {
return reject(err);
}
});
});
}).then((functionsClient) => {
// Update the Cloud Function if it does exist
return new Promise((resolve, reject) => {
functionsClient.update({ resource, name: resource.name }, (err) => {
// Report status of update operation
if (err) {
return reject(err);
}
return resolve();
});
});
}).then(() => {
// Report success
res.send(`Function ${FUNCTION_NAME} redeployed successfully!`);
})
.catch((err) => {
// Handle deployment error
console.error(err);
res.status(500).send(`Function ${FUNCTION_NAME} deployment failed.`);
});
exports.helloGET = (req, res) => {
res.send('Hello World!');
};
// [END functions_autodeploy]
// [END functions_helloworld_get]
7 changes: 1 addition & 6 deletions functions/autodeploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@
"node": ">=4.3.2"
},
"scripts": {
"lint": "repo-tools lint",
"pretest": "npm run lint",
"test": "ava -T 20s --verbose test/*.test.js"
},
"dependencies": {
"googleapis": "^24.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "2.1.0",
"ava": "0.23.0",
"proxyquire": "1.8.0",
"sinon": "4.0.2"
"ava": "0.23.0"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
Expand Down
24 changes: 24 additions & 0 deletions functions/autodeploy/test/dumb.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const test = require(`ava`);

test(`can add`, t => {
t.is(2 + 2, 4);
});

test(`can multiply`, t => {
t.is(2 * 2, 4);
});

0 comments on commit 85463da

Please sign in to comment.