Skip to content

Commit ad28266

Browse files
author
Ace Nassri
authored
chore(functions): move helloPubSub to separate directory (GoogleCloudPlatform#2610)
* chore(functions): move helloPubSub to separate directory * Update package.json * html -> yaml * Update package.json * rm e2e-test
1 parent 6e15190 commit ad28266

4 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: functions-v2-hellopubsub
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'functions/v2/helloPubSub/**'
8+
pull_request:
9+
paths:
10+
- 'functions/v2/helloPubSub/**'
11+
pull_request_target:
12+
types: [labeled]
13+
schedule:
14+
- cron: '0 2 * * *'
15+
jobs:
16+
test:
17+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: 'write'
21+
pull-requests: 'write'
22+
id-token: 'write'
23+
steps:
24+
- uses: actions/checkout@v3
25+
with:
26+
ref: ${{github.event.pull_request.head.ref}}
27+
repository: ${{github.event.pull_request.head.repo.full_name}}
28+
- uses: google-github-actions/auth@v0.7.0
29+
with:
30+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
31+
service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com'
32+
create_credentials_file: 'true'
33+
access_token_lifetime: 600s
34+
- uses: actions/setup-node@v3
35+
with:
36+
node-version: 14
37+
- run: npm install
38+
working-directory: functions/v2/helloPubSub
39+
- run: npm test
40+
working-directory: functions/v2/helloPubSub
41+
env:
42+
MOCHA_REPORTER_SUITENAME: functions_v2_helloPubSub
43+
MOCHA_REPORTER_OUTPUT: functions_v2_helloPubSub_sponge_log.xml
44+
MOCHA_REPORTER: xunit
45+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
46+
uses: actions/github-script@v6
47+
with:
48+
github-token: ${{ secrets.GITHUB_TOKEN }}
49+
script: |
50+
try {
51+
await github.rest.issues.removeLabel({
52+
name: 'actions:force-run',
53+
owner: 'GoogleCloudPlatform',
54+
repo: 'nodejs-docs-samples',
55+
issue_number: context.payload.pull_request.number
56+
});
57+
} catch (e) {
58+
if (!e.message.includes('Label does not exist')) {
59+
throw e;
60+
}
61+
}
62+
- if: ${{ github.event_name == 'schedule'}}
63+
run: |
64+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
65+
chmod +x ./flakybot
66+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}

functions/v2/helloPubSub/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START functions_cloudevent_pubsub]
18+
const functions = require('@google-cloud/functions-framework');
19+
20+
// Register a CloudEvent callback with the Functions Framework that will
21+
// be executed when the Pub/Sub trigger topic receives a message.
22+
functions.cloudEvent('helloPubSub', cloudEvent => {
23+
// The Pub/Sub message is passed as the CloudEvent's data payload.
24+
const base64name = cloudEvent.data.message.data;
25+
26+
const name = base64name
27+
? Buffer.from(base64name, 'base64').toString()
28+
: 'World';
29+
30+
console.log(`Hello, ${name}!`);
31+
});
32+
// [END functions_cloudevent_pubsub]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "nodejs-docs-samples-functions-v2",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google LLC",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=12.0.0"
13+
},
14+
"scripts": {
15+
"test": "mocha test/index.test.js"
16+
},
17+
"dependencies": {
18+
"@google-cloud/functions-framework": "^3.0.0"
19+
},
20+
"devDependencies": {
21+
"mocha": "^9.1.3",
22+
"p-retry": "^4.6.1",
23+
"sinon": "^13.0.0",
24+
"supertest": "^6.0.0",
25+
"uuid": "^8.3.2"
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
const assert = require('assert');
16+
const sinon = require('sinon');
17+
const supertest = require('supertest');
18+
19+
const functionsFramework = require('@google-cloud/functions-framework/testing');
20+
21+
beforeEach(() => {
22+
// require the module that includes the functions we are testing
23+
require('../index');
24+
25+
// stub the console so we can use it for side effect assertions
26+
sinon.stub(console, 'log');
27+
sinon.stub(console, 'error');
28+
});
29+
30+
afterEach(() => {
31+
// restore the console stub
32+
console.log.restore();
33+
console.error.restore();
34+
});
35+
36+
describe('functions_cloudevent_pubsub', () => {
37+
it('should process a CloudEvent', async () => {
38+
const event = {
39+
data: {
40+
message: 'd29ybGQ=', // 'World' in base 64
41+
},
42+
};
43+
44+
const server = functionsFramework.getTestServer('helloPubSub');
45+
await supertest(server)
46+
.post('/')
47+
.send(event)
48+
.set('Content-Type', 'application/json')
49+
.expect(204);
50+
assert(console.log.calledWith('Hello, World!'));
51+
});
52+
});

0 commit comments

Comments
 (0)