From 48b7bd54f3e0a1877aaba52e8e40fe174dae92e5 Mon Sep 17 00:00:00 2001 From: Cesar Varela Date: Thu, 24 Oct 2024 16:42:55 -0300 Subject: [PATCH] Trigger process notifications mutation from Github workflow (#3177) * Add workflows to process notifications post deploy * Delete old netlify plugin * Use var instead --- .github/workflows/process-notifications.yml | 25 +++++++ .github/workflows/production.yml | 7 ++ .github/workflows/staging.yml | 7 ++ site/gatsby-site/deploy-netlify.toml | 3 - .../index.js | 72 ------------------- .../manifest.yml | 7 -- .../package.json | 18 ----- 7 files changed, 39 insertions(+), 100 deletions(-) create mode 100644 .github/workflows/process-notifications.yml delete mode 100644 site/gatsby-site/plugins/netlify-plugin-process-notifications/index.js delete mode 100644 site/gatsby-site/plugins/netlify-plugin-process-notifications/manifest.yml delete mode 100644 site/gatsby-site/plugins/netlify-plugin-process-notifications/package.json diff --git a/.github/workflows/process-notifications.yml b/.github/workflows/process-notifications.yml new file mode 100644 index 0000000000..e2e8618c68 --- /dev/null +++ b/.github/workflows/process-notifications.yml @@ -0,0 +1,25 @@ +name: Process Notifications + +on: + workflow_call: + inputs: + environment: + description: The Github environment to load secrets from + type: string + required: true + +jobs: + execute-mutation: + runs-on: ubuntu-latest + environment: ${{ inputs.environment }} + + steps: + - name: Call GraphQL API + env: + SITE_URL: ${{ vars.SITE_URL }} + PROCESS_NOTIFICATIONS_SECRET: ${{ secrets.PROCESS_NOTIFICATIONS_SECRET }} + run: | + curl -X POST "$SITE_URL/api/graphql" \ + -H "Content-Type: application/json" \ + -H "PROCESS_NOTIFICATIONS_SECRET: $PROCESS_NOTIFICATIONS_SECRET" \ + -d '{"query":"mutation { processNotifications }"}' diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index f5dea96a39..ea2fde4cb5 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -95,3 +95,10 @@ jobs: netlify-alias: runner-label: ${{ vars.PRODUCTION_RUNNER_LABEL }} cache-modifier: ${{ needs.cache-modifier.outputs.cache-modifier }} + + call-process-notifications: + needs: call-deploy + uses: ./.github/workflows/process-notifications.yml + secrets: inherit + with: + environment: production \ No newline at end of file diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 6a9a59437d..0f2b422cf7 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -94,3 +94,10 @@ jobs: netlify-alias: runner-label: ${{ vars.STAGING_RUNNER_LABEL }} cache-modifier: ${{ needs.cache-modifier.outputs.cache-modifier }} + + call-process-notifications: + needs: call-deploy + uses: ./.github/workflows/process-notifications.yml + secrets: inherit + with: + environment: staging diff --git a/site/gatsby-site/deploy-netlify.toml b/site/gatsby-site/deploy-netlify.toml index ebee2f456b..50b010945d 100644 --- a/site/gatsby-site/deploy-netlify.toml +++ b/site/gatsby-site/deploy-netlify.toml @@ -8,9 +8,6 @@ [[plugins]] package = "/plugins/netlify-plugin-create-admin-user" -[[plugins]] - package = "/plugins/netlify-plugin-process-notifications" - [build.processing.html] pretty_urls = false diff --git a/site/gatsby-site/plugins/netlify-plugin-process-notifications/index.js b/site/gatsby-site/plugins/netlify-plugin-process-notifications/index.js deleted file mode 100644 index 7dd04e7a8a..0000000000 --- a/site/gatsby-site/plugins/netlify-plugin-process-notifications/index.js +++ /dev/null @@ -1,72 +0,0 @@ -import apolloClient from '@apollo/client'; - -import { fetch } from 'cross-fetch'; - -const config = { - SITE_URL: process.env.SITE_URL, - PROCESS_NOTIFICATIONS_SECRET: process.env.PROCESS_NOTIFICATIONS_SECRET, -}; - -Object.keys(config).forEach((key) => { - if (config[key] === undefined) { - throw new Error(`Config property ${key} is undefined`); - } -}); - -const client = new apolloClient.ApolloClient({ - link: new apolloClient.HttpLink({ - uri: `${config.SITE_URL}/api/graphql`, - fetch: async (uri, options) => { - options.headers.PROCESS_NOTIFICATIONS_SECRET = config.PROCESS_NOTIFICATIONS_SECRET; - - return fetch(uri, options); - }, - }), - cache: new apolloClient.InMemoryCache(), -}); - -const processNotifications = async () => { - const PROCESS_NOTIFICATIONS = apolloClient.gql` - mutation ProcessNotifications { - processNotifications - } - `; - - const processResult = await client.mutate({ - mutation: PROCESS_NOTIFICATIONS, - variables: {}, - }); - - return processResult; -}; - -// Runs on build success -export const onSuccess = async function ({ - // Core utilities - utils: { - // Utility to report errors. - // See https://github.com/netlify/build#error-reporting - build, - // Utility to display information in the deploy summary. - // See https://github.com/netlify/build#logging - status, - // Utility for running commands. - // See https://github.com/netlify/build/blob/master/packages/run-utils#readme - run, - }, -}) { - try { - // Commands are printed in Netlify logs - await run('echo', ['Processing pending notifications...\n']); - - const result = await processNotifications(); - - await run('echo', [`${result?.data?.processNotifications} notifications were processed!`]); - } catch (error) { - // Report a user error - build.failBuild('Error message', { error }); - } - - // Display success information - status.show({ summary: 'Success!' }); -}; diff --git a/site/gatsby-site/plugins/netlify-plugin-process-notifications/manifest.yml b/site/gatsby-site/plugins/netlify-plugin-process-notifications/manifest.yml deleted file mode 100644 index 327bd77a69..0000000000 --- a/site/gatsby-site/plugins/netlify-plugin-process-notifications/manifest.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: 'netlify-plugin-process-notifications' -inputs: [] -# Example inputs: -# - name: example -# description: Example description -# default: 5 -# required: false diff --git a/site/gatsby-site/plugins/netlify-plugin-process-notifications/package.json b/site/gatsby-site/plugins/netlify-plugin-process-notifications/package.json deleted file mode 100644 index 8179e20e85..0000000000 --- a/site/gatsby-site/plugins/netlify-plugin-process-notifications/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "netlify-plugin-process-notifications", - "version": "0.0.1", - "description": "Netlify Build plugin - This plugin process pending notifications", - "type": "module", - "exports": "./index.js", - "main": "./index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "Pablo Costa ", - "dependencies": { - "@apollo/client": "~3.5.6", - "realm-web": "^1.3.0", - "cross-fetch": "^3.1.5" - }, - "license": "MIT" -}