Skip to content

Commit

Permalink
Merge pull request #3188 from responsible-ai-collaborative/staging
Browse files Browse the repository at this point in the history
Deploy to Production
  • Loading branch information
kepae authored Nov 4, 2024
2 parents 72364e4 + 74515a4 commit ea6af4a
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 22 deletions.
26 changes: 21 additions & 5 deletions .github/workflows/process-notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ 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 }"}'
RESPONSE=$(curl -s -o response.json -w "%{http_code}" -X POST "$SITE_URL/api/graphql" \
-H "Content-Type: application/json" \
-H "PROCESS_NOTIFICATIONS_SECRET: $PROCESS_NOTIFICATIONS_SECRET" \
-d '{"query":"mutation { processNotifications }"}')
HTTP_STATUS=$RESPONSE
if [ "$HTTP_STATUS" -ne 200 ]; then
echo "GraphQL mutation failed with HTTP status: $HTTP_STATUS"
cat response.json
exit 1
fi
if jq -e 'has("errors") or has("errorType")' response.json > /dev/null; then
echo "GraphQL mutation failed with the following response:"
jq '.' response.json
exit 1
fi
echo "GraphQL mutation succeeded!"
jq '.' response.json
39 changes: 38 additions & 1 deletion site/db-backup/bin/taxonomy_csv_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
import csv
import os

def extract_entities(json_data):
entities = []
for item in json_data['attributes']:
if item['short_name'] == 'Entity':
entities.append(item['value_json'])
break
return entities

def extract_snippets(json_data):
snippets = ''
for item in json_data['attributes']:
# convert value_json to string
escapedText = json.loads(item['value_json'])

if(isinstance(escapedText, list)):
escapedText = ', '.join(escapedText)
else:
escapedText = str(escapedText)

snippets = snippets + item['short_name'] + ': ' + escapedText + '\n'
return snippets

def taxonomy_csv_export(namespace, taxa_file, classification_file, target_folder):

print(f"Processing namespace '{namespace}'...")
Expand Down Expand Up @@ -52,8 +74,23 @@ def taxonomy_csv_export(namespace, taxa_file, classification_file, target_folder
attribute_dict = {}
for attr in attributes:
value = json.loads(attr['value_json']) if 'value_json' in attr else ''

if isinstance(value, list):
value = ', '.join(map(str, value))
flattened_list = []
for item in value:
# Transform JSON entities data into a list of entities
if isinstance(item, dict) and attr['short_name'] == 'Entities':
flattened_list.append(extract_entities(item))
else:
# Transform JSON snippets data into text
if isinstance(item, dict) and 'Snippets' in attr['short_name']:
flattened_list.append(extract_snippets(item))
else:
flattened_list.append(item)
# Flatten the list and remove extra quotes
flattened_list = [str(item).strip('"') for sublist in flattened_list for item in (sublist if isinstance(sublist, list) else [sublist])]
value = ', '.join(flattened_list)

attribute_dict[attr['short_name']] = value

# Get the corresponding values for each header
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const config = require('../config');

/** @type {import('umzug').MigrationFn<any>} */
exports.up = async ({ context: { client } }) => {
const reportsCollection = client.db(config.realm.production_db.db_name).collection('reports');

// Change "AIAAIC" to "Anonymous" if it is the only element in the `submitters` array
const result1 = await reportsCollection.updateMany(
{ submitters: ['AIAAIC'] },
{ $set: { submitters: ['Anonymous'] } }
);

console.log(`Modified ${result1.modifiedCount} documents where "AIAAIC" was the only submitter`);

// Remove "AIAAIC" from the `submitters` array if there are multiple elements
const result2 = await reportsCollection.updateMany(
{ submitters: 'AIAAIC' },
{ $pull: { submitters: 'AIAAIC' } }
);

console.log(
`Modified ${result2.modifiedCount} documents where "AIAAIC" was one of the submitters`
);
};
26 changes: 26 additions & 0 deletions site/gatsby-site/server/fields/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface UserAdminData {
creationDate?: Date;
lastAuthenticationDate?: Date;
disabled?: boolean;
userId?: string;
}

export const getUserAdminData = async (userId: string) => {
Expand All @@ -106,6 +107,31 @@ export const getUserAdminData = async (userId: string) => {
return response;
}

export const getUsersAdminData = async () => {

const response = await apiRequest({ path: `/users` });

const result: UserAdminData[] = [];

for (const userData of response) {

if (userData.data?.email) {

const user: UserAdminData = {};

user.email = userData.data.email;
user.creationDate = new Date(userData.creation_date * 1000);
user.lastAuthenticationDate = new Date(userData.last_authentication_date * 1000);
user.disabled = userData.disabled;
user.userId = userData._id;

result.push(user);
}
}

return result;
}

interface SendEmailParams {
recipients: {
email: string;
Expand Down
16 changes: 5 additions & 11 deletions site/gatsby-site/server/fields/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLFieldConfigMap, GraphQLInt } from 'graphql';
import { generateQueryFields } from '../utils';
import { Context, DBEntity, DBIncident, DBReport, DBSubscription, DBNotification } from '../interfaces';
import { NotificationType } from '../types/notification';
import { getUserAdminData, sendEmail } from './common';
import { getUserAdminData, getUsersAdminData, sendEmail } from './common';
import * as reporter from '../reporter';
import { hasHeaderSecret, isRole } from '../rules';
import config from '../config';
Expand All @@ -14,18 +14,12 @@ export const queryFields: GraphQLFieldConfigMap<any, Context> = {


const getRecipients = async (userIds: string[]) => {
const recipients = [];
const users = await getUsersAdminData();

for (const userId of userIds) {
const userResponse = await getUserAdminData(userId);
const recipients = users
.filter((user) => userIds.includes(user.userId!))
.map((user) => ({ email: user.email!, userId: user.userId! }));

if (userResponse?.email) {
recipients.push({
email: userResponse.email,
userId,
});
}
}
return recipients;
}

Expand Down
10 changes: 5 additions & 5 deletions site/gatsby-site/server/tests/notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe(`Notifications`, () => {
};

jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })
jest.spyOn(common, 'getUserAdminData').mockResolvedValue({ email: '[email protected]' });
jest.spyOn(common, 'getUsersAdminData').mockResolvedValue([{ userId: '123', email: '[email protected]' }]);
const sendEmailMock = jest.spyOn(common, 'sendEmail').mockResolvedValue();

const response = await makeRequest(url, mutationData, { ['PROCESS_NOTIFICATIONS_SECRET']: config.PROCESS_NOTIFICATIONS_SECRET });;
Expand Down Expand Up @@ -290,7 +290,7 @@ describe(`Notifications`, () => {
};

jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })
jest.spyOn(common, 'getUserAdminData').mockResolvedValue({ email: '[email protected]' });
jest.spyOn(common, 'getUsersAdminData').mockResolvedValue([{ userId: '123', email: '[email protected]' }]);
const sendEmailMock = jest.spyOn(common, 'sendEmail').mockResolvedValue();

const response = await makeRequest(url, mutationData, { ['PROCESS_NOTIFICATIONS_SECRET']: config.PROCESS_NOTIFICATIONS_SECRET });;
Expand Down Expand Up @@ -422,7 +422,7 @@ describe(`Notifications`, () => {
};

jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })
jest.spyOn(common, 'getUserAdminData').mockResolvedValue({ email: '[email protected]' });
jest.spyOn(common, 'getUsersAdminData').mockResolvedValue([{ userId: '123', email: '[email protected]' }]);
const sendEmailMock = jest.spyOn(common, 'sendEmail').mockResolvedValue();

const response = await makeRequest(url, mutationData, { ['PROCESS_NOTIFICATIONS_SECRET']: config.PROCESS_NOTIFICATIONS_SECRET });;
Expand Down Expand Up @@ -548,7 +548,7 @@ describe(`Notifications`, () => {
};

jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })
jest.spyOn(common, 'getUserAdminData').mockResolvedValue({ email: '[email protected]' });
jest.spyOn(common, 'getUsersAdminData').mockResolvedValue([{ userId: '123', email: '[email protected]' }]);
const sendEmailMock = jest.spyOn(common, 'sendEmail').mockResolvedValue();

const response = await makeRequest(url, mutationData, { ['PROCESS_NOTIFICATIONS_SECRET']: config.PROCESS_NOTIFICATIONS_SECRET });;
Expand Down Expand Up @@ -675,7 +675,7 @@ describe(`Notifications`, () => {
};

jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })
jest.spyOn(common, 'getUserAdminData').mockResolvedValue({ email: '[email protected]' });
jest.spyOn(common, 'getUsersAdminData').mockResolvedValue([{ userId: '123', email: '[email protected]' }]);
const sendEmailMock = jest.spyOn(common, 'sendEmail').mockResolvedValue();

const response = await makeRequest(url, mutationData, { ['PROCESS_NOTIFICATIONS_SECRET']: config.PROCESS_NOTIFICATIONS_SECRET });;
Expand Down

0 comments on commit ea6af4a

Please sign in to comment.