Skip to content

Commit

Permalink
feat: slack notification on add feed form (#765)
Browse files Browse the repository at this point in the history
* slack notification on add feed form

* env var fix

* update gtihub actions

* slack webhook logging
  • Loading branch information
Alessandro100 authored Oct 21, 2024
1 parent 0487f9c commit 613f8b8
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 43 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/web-app-deployer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ on:
description: The google sheet id for the feed submit form
type: string
required: false
OP_SLACK_WEBHOOK_URL:
description: 1Password Slack webhook URL secret reference for add feed channel
type: string
required: false
outputs:
hosting_url:
description: "The URL of the deployed web app"
Expand Down Expand Up @@ -137,6 +141,14 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: 1Password - Load Secrets
uses: 1Password/[email protected]
with:
export-env: true
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
SLACK_WEBHOOK_URL: ${{inputs.OP_SLACK_WEBHOOK_URL}}

- name: Authenticate to Google Cloud DEV
if: ${{ inputs.FIREBASE_PROJECT == 'dev' }}
uses: google-github-actions/auth@v2
Expand Down Expand Up @@ -240,7 +252,9 @@ jobs:
- name: Set Firebase Feed Form Environment
working-directory: functions/packages/feed-form
run: echo "FEED_SUBMIT_GOOGLE_SHEET_ID=${{ inputs.FEED_SUBMIT_GOOGLE_SHEET_ID }}" > .env
run: |
echo "FEED_SUBMIT_GOOGLE_SHEET_ID=${{ inputs.FEED_SUBMIT_GOOGLE_SHEET_ID }}" > .env
echo "SLACK_WEBHOOK_URL=${{ env.SLACK_WEBHOOK_URL }}" >> .env
- name: Deploy Firebase Functions
working-directory: functions
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/web-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ jobs:
with:
FIREBASE_PROJECT: dev
FEED_SUBMIT_GOOGLE_SHEET_ID: "1iXwux9hM4p5Li1EGgwx-8hU3sMDnF15yTflqmGjiZqE"
OP_SLACK_WEBHOOK_URL: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/wm52iemzzm2cwfaoakwaufthuq/password"
secrets: inherit
3 changes: 2 additions & 1 deletion .github/workflows/web-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ on:
- "functions/**"
- ".github/workflows/web-*.yml"
jobs:

deploy-web-app:
name: Deploy Web App
uses: ./.github/workflows/web-app-deployer.yml
with:
FIREBASE_PROJECT: dev
PREVIEW_DEPLOYMENT: true
FEED_SUBMIT_GOOGLE_SHEET_ID: "1iXwux9hM4p5Li1EGgwx-8hU3sMDnF15yTflqmGjiZqE"
OP_SLACK_WEBHOOK_URL: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/wm52iemzzm2cwfaoakwaufthuq/password"
PREVIEW_HOST_NAME: "pr-${{ github.event.number }}"
secrets: inherit

Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/web-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ jobs:
FIREBASE_PROJECT: prod
REACT_APP_GOOGLE_ANALYTICS_ID: ${{ vars.PROD_REACT_APP_GOOGLE_ANALYTICS_ID }}
FEED_SUBMIT_GOOGLE_SHEET_ID: "10eIUxWVtLmc2EATiwivgXBf4bOMErOnq7GFIoRedXHU"
secrets: inherit
OP_SLACK_WEBHOOK_URL: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/Slack webhook URLs/rdpfgrmnbxqaelgi5oky3lryz4/internal-add-feeds"
secrets: inherit
1 change: 1 addition & 0 deletions .github/workflows/web-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ jobs:
FIREBASE_PROJECT: qa
REACT_APP_GOOGLE_ANALYTICS_ID: ${{ vars.QA_REACT_APP_GOOGLE_ANALYTICS_ID }}
FEED_SUBMIT_GOOGLE_SHEET_ID: "1GZeO3kFBFr073bSHuClhTiKt7KEad8vWM01Clo-rOVQ"
OP_SLACK_WEBHOOK_URL: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/wm52iemzzm2cwfaoakwaufthuq/password"
secrets: inherit
1 change: 1 addition & 0 deletions functions/packages/feed-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"main": "lib/index.js",
"dependencies": {
"axios": "^1.7.7",
"firebase": "^10.6.0",
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1",
Expand Down
132 changes: 92 additions & 40 deletions functions/packages/feed-form/src/impl/feed-form-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {GoogleAuth} from "google-auth-library";
import * as logger from "firebase-functions/logger";
import {type FeedSubmissionFormRequestBody} from "./types";
import {type CallableRequest, HttpsError} from "firebase-functions/v2/https";
import axios from "axios";

const SCOPES = [
"https://www.googleapis.com/auth/spreadsheets",
Expand All @@ -27,12 +28,13 @@ export const writeToSheet = async (
const formData: FeedSubmissionFormRequestBody = request.data;
const rows = buildFeedRows(formData, uid);
await rawDataSheet.addRows(rows, {insert: true});

sendSlackWebhook(sheetId);
return {message: "Data written to the new sheet successfully!"};
} catch (error) {
logger.error("Error writing to sheet:", error);
throw new HttpsError(
"internal", "An error occurred while writing to the sheet."
"internal",
"An error occurred while writing to the sheet."
);
}
};
Expand Down Expand Up @@ -83,55 +85,42 @@ export function buildFeedRows(
const rowsToAdd: RawRowData[] = [];
if (formData.dataType === "gtfs") {
rowsToAdd.push(
buildFeedRow(
formData,
{
dataTypeName: "GTFS Schedule",
downloadUrl: formData.feedLink ?? "",
currentUrl: formData.oldFeedLink ?? "",
uid,
}
)
buildFeedRow(formData, {
dataTypeName: "GTFS Schedule",
downloadUrl: formData.feedLink ?? "",
currentUrl: formData.oldFeedLink ?? "",
uid,
})
);
} else {
if (formData.tripUpdates) {
rowsToAdd.push(
buildFeedRow(
formData,
{
dataTypeName: "GTFS Realtime - Trip Updates",
downloadUrl: formData.tripUpdates ?? "",
currentUrl: formData.oldTripUpdates ?? "",
uid,
}

)
buildFeedRow(formData, {
dataTypeName: "GTFS Realtime - Trip Updates",
downloadUrl: formData.tripUpdates ?? "",
currentUrl: formData.oldTripUpdates ?? "",
uid,
})
);
}
if (formData.vehiclePositions) {
rowsToAdd.push(
buildFeedRow(
formData,
{
dataTypeName: "GTFS Realtime - Vehicle Positions",
downloadUrl: formData.vehiclePositions ?? "",
currentUrl: formData.oldVehiclePositions ?? "",
uid,
}
)
buildFeedRow(formData, {
dataTypeName: "GTFS Realtime - Vehicle Positions",
downloadUrl: formData.vehiclePositions ?? "",
currentUrl: formData.oldVehiclePositions ?? "",
uid,
})
);
}
if (formData.serviceAlerts) {
rowsToAdd.push(
buildFeedRow(
formData,
{
dataTypeName: "GTFS Realtime - Service Alerts",
downloadUrl: formData.serviceAlerts ?? "",
currentUrl: formData.oldServiceAlerts ?? "",
uid,
}
)
buildFeedRow(formData, {
dataTypeName: "GTFS Realtime - Service Alerts",
downloadUrl: formData.serviceAlerts ?? "",
currentUrl: formData.oldServiceAlerts ?? "",
uid,
})
);
}
}
Expand All @@ -157,7 +146,6 @@ export function buildFeedRow(
formData: FeedSubmissionFormRequestBody,
formRowParameters: BuildRowParameters
): RawRowData {
/* eslint-enable max-len */
const dateNow = new Date();
return {
[SheetCol.Status]: "Feed Submitted",
Expand Down Expand Up @@ -188,3 +176,67 @@ export function buildFeedRow(
[SheetCol.LogoPermission]: formData.hasLogoPermission,
};
}

/**
* Sends a Slack webhook message to the configured Slack webhook URL
* @param {string} spreadsheetId The ID of the Google Sheet
*/
async function sendSlackWebhook(spreadsheetId: string) {
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
const sheetUrl = `https://docs.google.com/spreadsheets/d/${spreadsheetId}/edit`;
if (slackWebhookUrl !== undefined && slackWebhookUrl !== "") {
const slackMessage = {
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "New Feed Added",
},
},
{
type: "rich_text",
elements: [
{
type: "rich_text_section",
elements: [
{
type: "emoji",
name: "inbox_tray",
},
{
type: "text",
text: " A new entry was received in the OpenMobilityData source updates Google Sheet",
},
],
},
],
},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "link",
"url": sheetUrl,
"text": "View Feed",
"style": {
"bold": true,
},
},
],
},
],
},
],
};
await axios.post(slackWebhookUrl, slackMessage).catch((error) => {
logger.error("Error sending Slack webhook:", error);
});
} else {
logger.error("Slack webhook URL is not defined");
}
}
/* eslint-enable max-len */
9 changes: 9 additions & 0 deletions functions/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,15 @@ axios@^1.4.0:
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^1.7.7:
version "1.7.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

babel-jest@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.7.0.tgz#f4369919225b684c56085998ac63dbd05be020d5"
Expand Down

0 comments on commit 613f8b8

Please sign in to comment.