-
Notifications
You must be signed in to change notification settings - Fork 495
feat: Webhook chat #1217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Webhook chat #1217
Conversation
Summary of ChangesHello @PierrickVoulet, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new solution for integrating with Google Chat using webhooks. It provides practical Node.js examples that demonstrate how to send both basic and threaded messages to Google Chat spaces, along with the necessary project setup for dependency management. This enables developers to quickly understand and implement webhook-based communication with Google Chat. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a Node.js application for sending messages to Google Chat using webhooks. The implementation has some critical security and correctness issues. Specifically, credentials are hardcoded in the source code, which is a major security risk. Additionally, there is no error handling for failed HTTP requests or for promise rejections, which can lead to application crashes and difficult debugging. I've provided suggestions to address these issues by using environment variables for credentials and adding proper error handling.
| * @return {Object} response | ||
| */ | ||
| async function webhook() { | ||
| const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.
| const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN" | |
| const url = `https://chat.googleapis.com/v1/spaces/${process.env.SPACE_ID}/messages?key=${process.env.KEY}&token=${process.env.TOKEN}` |
| * @return {Object} response | ||
| */ | ||
| async function webhook() { | ||
| const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.
| const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" | |
| const url = `https://chat.googleapis.com/v1/spaces/${process.env.SPACE_ID}/messages?key=${process.env.KEY}&token=${process.env.TOKEN}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD}` |
| text: "Hello from a Node script!" | ||
| }) | ||
| }); | ||
| return await res.json(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). fetch only rejects on network errors, not on HTTP error statuses. If the API returns an error, res.json() will likely fail with an uninformative error if the response body is not JSON. You should check res.ok and throw a descriptive error if the request was not successful.
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
}
return res.json();| return await res.json(); | ||
| } | ||
|
|
||
| webhook().then(res => console.log(res)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The promise returned by webhook() is not being handled for rejections. If the fetch call fails (e.g., network error, or an HTTP error is thrown), it will result in an unhandled promise rejection, which can crash the Node.js process. You should add a .catch() block to handle potential errors.
webhook().then(res => console.log(res)).catch(console.error);| } | ||
| }) | ||
| }); | ||
| return await res.json(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). fetch only rejects on network errors, not on HTTP error statuses. If the API returns an error, res.json() will likely fail with an uninformative error if the response body is not JSON. You should check res.ok and throw a descriptive error if the request was not successful.
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
}
return res.json();| return await res.json(); | ||
| } | ||
|
|
||
| webhook().then(res => console.log(res)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The promise returned by webhook() is not being handled for rejections. If the fetch call fails (e.g., network error, or an HTTP error is thrown), it will result in an unhandled promise rejection, which can crash the Node.js process. You should add a .catch() block to handle potential errors.
webhook().then(res => console.log(res)).catch(console.error);
No description provided.