-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): create new samples for typed function signature
- Loading branch information
1 parent
68ccdf9
commit 5265cb6
Showing
9 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# 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. | ||
|
||
name: functions-v2-typed-googlechatbot | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'functions/v2/typed/googlechatbot/**' | ||
- '.github/workflows/functions-v2-typed-googlechatbot.yaml' | ||
pull_request: | ||
paths: | ||
- 'functions/v2/typed/googlechatbot/**' | ||
- '.github/workflows/functions-v2-typed-googlechatbot.yaml' | ||
pull_request_target: | ||
types: [labeled] | ||
paths: | ||
- 'functions/v2/typed/googlechatbot/**' | ||
- '.github/workflows/functions-v2-typed-googlechatbot.yaml' | ||
schedule: | ||
- cron: '0 0 * * 0' | ||
jobs: | ||
test: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' | ||
uses: ./.github/workflows/test.yaml | ||
with: | ||
name: 'functions-v2-typed-googlechatbot' | ||
path: 'functions/v2/typed/googlechatbot' | ||
remove_label: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: | | ||
github.event.action == 'labeled' && | ||
github.event.label.name == 'actions:force-run' && | ||
always() | ||
uses: ./.github/workflows/remove-label.yaml | ||
flakybot: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail | ||
uses: ./.github/workflows/flakybot.yaml | ||
needs: [test] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# 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. | ||
|
||
name: functions-v2-typed-greeting | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'functions/v2/typed/greeting/**' | ||
- '.github/workflows/functions-v2-typed-greeting.yaml' | ||
pull_request: | ||
paths: | ||
- 'functions/v2/typed/greeting/**' | ||
- '.github/workflows/functions-v2-typed-greeting.yaml' | ||
pull_request_target: | ||
types: [labeled] | ||
paths: | ||
- 'functions/v2/typed/greeting/**' | ||
- '.github/workflows/functions-v2-typed-greeting.yaml' | ||
schedule: | ||
- cron: '0 0 * * 0' | ||
jobs: | ||
test: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' | ||
uses: ./.github/workflows/test.yaml | ||
with: | ||
name: 'functions-v2-typed-greeting' | ||
path: 'functions/v2/typed/greeting' | ||
remove_label: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: | | ||
github.event.action == 'labeled' && | ||
github.event.label.name == 'actions:force-run' && | ||
always() | ||
uses: ./.github/workflows/remove-label.yaml | ||
flakybot: | ||
# Ref: https://github.com/google-github-actions/auth#usage | ||
permissions: | ||
contents: 'read' | ||
id-token: 'write' | ||
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail | ||
uses: ./.github/workflows/flakybot.yaml | ||
needs: [test] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START functions_typed_googlechatbot] | ||
const functions = require('@google-cloud/functions-framework'); | ||
|
||
functions.typed('chat', req => { | ||
const displayName = req.message.sender.displayName; | ||
const imageUrl = req.message.sender.avatarUrl; | ||
|
||
const cardHeader = { | ||
title: `Hello ${displayName}!`, | ||
}; | ||
|
||
const avatarWidget = { | ||
textParagraph: {text: 'Your avatar picture: '}, | ||
}; | ||
|
||
const avatarImageWidget = { | ||
image: {imageUrl}, | ||
}; | ||
|
||
const avatarSection = { | ||
widgets: [avatarWidget, avatarImageWidget], | ||
}; | ||
|
||
return { | ||
cardsV2: [ | ||
{ | ||
cardId: 'avatarCard', | ||
card: { | ||
name: 'Avatar Card', | ||
header: cardHeader, | ||
sections: [avatarSection], | ||
}, | ||
}, | ||
], | ||
}; | ||
}); | ||
// [END functions_typed_googlechatbot] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "googlechatbot", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "c8 mocha test/*.test.js --timeout=60000" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@google-cloud/functions-framework": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
"c8": "^8.0.0", | ||
"mocha": "^10.2.0", | ||
"sinon": "^15.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
const {getFunction} = require('@google-cloud/functions-framework/testing'); | ||
const assert = require('assert'); | ||
require('..'); | ||
|
||
describe('functions_typed_googlechatbot', () => { | ||
it('should respond to chat events correctly', async () => { | ||
const expected = { | ||
cardsV2: [ | ||
{ | ||
cardId: 'avatarCard', | ||
card: { | ||
name: 'Avatar Card', | ||
header: { | ||
title: 'Hello janedoe!', | ||
}, | ||
sections: [ | ||
{ | ||
widgets: [ | ||
{ | ||
textParagraph: { | ||
text: 'Your avatar picture: ', | ||
}, | ||
}, | ||
{ | ||
image: { | ||
imageUrl: 'example.com/avatar.png', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const chatFn = getFunction('chat').handler; | ||
|
||
const actual = chatFn({ | ||
message: { | ||
sender: { | ||
displayName: 'janedoe', | ||
avatarUrl: 'example.com/avatar.png', | ||
}, | ||
}, | ||
}); | ||
|
||
assert.deepEqual(actual, expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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. | ||
|
||
// [START functions_typed_greeting] | ||
const functions = require('@google-cloud/functions-framework'); | ||
|
||
functions.typed('greeting', req => { | ||
return { | ||
message: `Hello ${req.first_name} ${req.last_name}!`, | ||
}; | ||
}); | ||
// [END functions_typed_greeting] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "greeting", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "c8 mocha test/*.test.js --timeout=60000" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@google-cloud/functions-framework": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
"c8": "^8.0.0", | ||
"mocha": "^10.2.0", | ||
"sinon": "^15.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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. | ||
|
||
'use strict'; | ||
|
||
const {getFunction} = require('@google-cloud/functions-framework/testing'); | ||
const assert = require('assert'); | ||
require('..'); | ||
|
||
describe('functions_typed_greeting', () => { | ||
it('should respond to greetings correctly', async () => { | ||
const expected = { | ||
message: 'Hello Jane Doe!', | ||
}; | ||
|
||
const chatFn = getFunction('greeting').handler; | ||
|
||
const actual = chatFn({ | ||
first_name: 'Jane', | ||
last_name: 'Doe', | ||
}); | ||
|
||
assert.deepEqual(actual, expected); | ||
}); | ||
}); |