Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-workflow_dispatch-event-to-trig
Browse files Browse the repository at this point in the history
  • Loading branch information
prakashchoudhary07 authored Aug 13, 2024
2 parents 5aefc01 + 2aa3551 commit 3922ccc
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 81 deletions.
33 changes: 0 additions & 33 deletions .github/pull_request_template.md

This file was deleted.

6 changes: 5 additions & 1 deletion .github/workflows/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
jobs:
Deploy-to-Cloudflare:
runs-on: ubuntu-latest
environment: ${{ vars.CURRENT_ENVIRONMENT }}
environment: production
steps:
- uses: actions/checkout@v3
- run: npm install
Expand All @@ -21,10 +21,14 @@ jobs:
CRON_JOB_PRIVATE_KEY
DISCORD_BOT_PRIVATE_KEY
DISCORD_BOT_API_URL
DISCORD_SERVICE_PRIVATE_KEY
FF_CRON_DISCORD_KEY_PAIR_FLOW
env:
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
CURRENT_ENVIRONMENT: ${{vars.CURRENT_ENVIRONMENT}}
CRON_JOB_PRIVATE_KEY: ${{secrets.CRON_JOB_PRIVATE_KEY}}
DISCORD_BOT_PRIVATE_KEY: ${{secrets.DISCORD_BOT_PRIVATE_KEY}}
DISCORD_BOT_API_URL: ${{secrets.DISCORD_BOT_API_URL}}
DISCORD_SERVICE_PRIVATE_KEY: ${{secrets.DISCORD_SERVICE_PRIVATE_KEY}}
FF_CRON_DISCORD_KEY_PAIR_FLOW: ${{vars.FF_CRON_DISCORD_KEY_PAIR_FLOW}}
6 changes: 5 additions & 1 deletion .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
jobs:
Deploy-to-Cloudflare:
runs-on: ubuntu-latest
environment: ${{ vars.CURRENT_ENVIRONMENT }}
environment: staging
steps:
- uses: actions/checkout@v3
- run: npm install
Expand All @@ -21,10 +21,14 @@ jobs:
CRON_JOB_PRIVATE_KEY
DISCORD_BOT_PRIVATE_KEY
DISCORD_BOT_API_URL
DISCORD_SERVICE_PRIVATE_KEY
FF_CRON_DISCORD_KEY_PAIR_FLOW
env:
CLOUDFLARE_API_TOKEN: ${{secrets.CLOUDFLARE_API_TOKEN}}
CLOUDFLARE_ACCOUNT_ID: ${{secrets.CLOUDFLARE_ACCOUNT_ID}}
CURRENT_ENVIRONMENT: ${{vars.CURRENT_ENVIRONMENT}}
CRON_JOB_PRIVATE_KEY: ${{secrets.CRON_JOB_PRIVATE_KEY}}
DISCORD_BOT_PRIVATE_KEY: ${{secrets.DISCORD_BOT_PRIVATE_KEY}}
DISCORD_BOT_API_URL: ${{secrets.DISCORD_BOT_API_URL}}
DISCORD_SERVICE_PRIVATE_KEY: ${{secrets.DISCORD_SERVICE_PRIVATE_KEY}}
FF_CRON_DISCORD_KEY_PAIR_FLOW: ${{vars.FF_CRON_DISCORD_KEY_PAIR_FLOW}}
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "5.1.6",
"wrangler": "3.0.0"
"wrangler": "3.1.1"
},
"dependencies": {
"@tsndr/cloudflare-worker-jwt": "2.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/constants/commons.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const MISSED_UPDATES_PROD_ROLE_ID = '1183553844811153458';
export const MISSED_UPDATES_STAGING_ROLE_ID = '1183553844811153458';
export const MISSED_UPDATES_STAGING_ROLE_ID = '1184201657404362772';
export const MISSED_UPDATES_DEVELOPMENT_ROLE_ID = '1181214205081296896';
3 changes: 2 additions & 1 deletion src/handlers/scheduledEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const addMissedUpdatesRole = async (env: env) => {

const missedUpdatesUsers = await getMissedUpdatesUsers(env, cursor);

if (!!missedUpdatesUsers && missedUpdatesUsers.usersToAddRole?.length > 1) {
if (!!missedUpdatesUsers && missedUpdatesUsers.usersToAddRole?.length >= 1) {
const discordUserIdRoleIdList: DiscordUserRole[] = missedUpdatesUsers.usersToAddRole.map((userId) => ({
userid: userId,
roleid: config(env).MISSED_UPDATES_ROLE_ID,
Expand All @@ -89,6 +89,7 @@ export const addMissedUpdatesRole = async (env: env) => {
}
cursor = missedUpdatesUsers?.cursor;
}
// add logs for the results https://github.com/Real-Dev-Squad/website-backend/issues/1784
} catch (err) {
console.error('Error while adding missed updates roles');
}
Expand Down
5 changes: 3 additions & 2 deletions src/services/discordBotServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export const updateUserRoles = async (env: env, payload: DiscordUserRole[]): Pro
try {
const url = config(env).DISCORD_BOT_API_URL;
const token = await generateDiscordBotJwt(env);

const response = await env.DISCORD_BOT.fetch(`${url}/roles?action=add-role`, {
//TODO(@Ajeyakrishna-k): remove dev flag https://github.com/Real-Dev-Squad/discord-slash-commands/issues/193
const devQuery = env.FF_CRON_DISCORD_KEY_PAIR_FLOW === 'true' ? '&dev=true' : '';
const response = await env.DISCORD_BOT.fetch(`${url}/roles?action=add-role${devQuery}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
Expand Down
4 changes: 2 additions & 2 deletions src/services/rdsBackendService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const getMissedUpdatesUsers = async (env: env, cursor: string | undefined
const baseUrl = config(env).RDS_BASE_API_URL;

const url = new URL(`${baseUrl}/tasks/users/discord`);
url.searchParams.append('q', 'status:missed-updates');
url.searchParams.append('q', 'status:missed-updates -days-count:3');
if (cursor) {
url.searchParams.append('cursor', cursor);
}
Expand All @@ -26,7 +26,7 @@ export const getMissedUpdatesUsers = async (env: env, cursor: string | undefined
const responseData: DiscordUsersResponse = await response.json();
return responseData?.data;
} catch (error) {
console.error('Error occurrent while fetching discord user details');
console.error('Error occurred while fetching discord user details');
throw error;
}
};
9 changes: 9 additions & 0 deletions src/tests/handlers/missedRoleHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('addMissedUpdatesRole', () => {
expect(updateUserRoles).toHaveBeenCalledTimes(2);
});

it('should call getMissedUpdatesUsers and updateUserRoles when theres only one user', async () => {
const usersMockData = { ...missedUpdatesUsersMockWithoutCursor };
usersMockData.usersToAddRole = usersMockData.usersToAddRole.slice(0, 1);
(getMissedUpdatesUsers as jest.Mock).mockResolvedValueOnce(usersMockData);
await addMissedUpdatesRole({});
expect(getMissedUpdatesUsers).toHaveBeenCalledTimes(1);
expect(updateUserRoles).toHaveBeenCalledTimes(1);
});

it('should not call updateUserRoles when there are no users to add role', async () => {
(getMissedUpdatesUsers as jest.Mock).mockResolvedValueOnce(missedUpdatesUsersMockWithNoUsers);

Expand Down
6 changes: 3 additions & 3 deletions src/tests/services/rdsBackendService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('rdsBackendService', () => {
} as unknown as Response);
const result = await getMissedUpdatesUsers({}, cursor);
const url = new URL(`${config({}).RDS_BASE_API_URL}/tasks/users/discord`);
url.searchParams.append('q', 'status:missed-updates');
url.searchParams.append('q', 'status:missed-updates -days-count:3');
expect(fetch).toHaveBeenCalledWith(url, {
method: 'GET',
headers: {
Expand All @@ -39,7 +39,7 @@ describe('rdsBackendService', () => {
} as unknown as Response);
const result = await getMissedUpdatesUsers({}, 'cursorValue');
const url = new URL(`${config({}).RDS_BASE_API_URL}/tasks/users/discord`);
url.searchParams.append('q', 'status:missed-updates');
url.searchParams.append('q', 'status:missed-updates -days-count:3');
url.searchParams.append('cursor', 'cursorValue');
expect(fetch).toHaveBeenCalledWith(url, {
method: 'GET',
Expand All @@ -62,7 +62,7 @@ describe('rdsBackendService', () => {
const consoleSpy = jest.spyOn(console, 'error');
jest.spyOn(global, 'fetch').mockRejectedValueOnce(new Error('Error occurred'));
await expect(getMissedUpdatesUsers({}, cursor)).rejects.toThrow('Error occurred');
expect(consoleSpy).toHaveBeenCalledWith('Error occurrent while fetching discord user details');
expect(consoleSpy).toHaveBeenCalledWith('Error occurred while fetching discord user details');
});
});
});
8 changes: 5 additions & 3 deletions src/utils/generateJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const generateJwt = async (env: env) => {
const authToken = await jwt.sign(
{
name: 'Cron Job Handler',
exp: Math.floor(Date.now() / 1000) + 2,
exp: Math.floor(Date.now() / 1000) + 60,
},
env.CRON_JOB_PRIVATE_KEY,
{ algorithm: 'RS256' },
Expand All @@ -21,11 +21,13 @@ export const generateJwt = async (env: env) => {

export const generateDiscordBotJwt = async (env: env) => {
try {
//TODO(@Ajeyakrishna-k): remove dev flag https://github.com/Real-Dev-Squad/discord-slash-commands/issues/193
const privateKey = env.FF_CRON_DISCORD_KEY_PAIR_FLOW === 'true' ? env.DISCORD_SERVICE_PRIVATE_KEY : env.DISCORD_BOT_PRIVATE_KEY;
const authToken = await jwt.sign(
{
exp: Math.floor(Date.now() / 1000) + 2,
exp: Math.floor(Date.now() / 1000) + 60,
},
env.DISCORD_BOT_PRIVATE_KEY,
privateKey,
{ algorithm: 'RS256' },
);
return authToken;
Expand Down
4 changes: 2 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { addMissedUpdatesRole, callDiscordNicknameBatchUpdate } from './handlers
import { env } from './types/global.types';

const EVERY_6_HOURS = '0 */6 * * *';
const EVERY_12_HOURS = '0 */12 * * *';
const EVERY_11_HOURS = '0 */11 * * *';

export default {
// eslint-disable-next-line no-unused-vars
Expand All @@ -11,7 +11,7 @@ export default {
case EVERY_6_HOURS: {
return await callDiscordNicknameBatchUpdate(env);
}
case EVERY_12_HOURS: {
case EVERY_11_HOURS: {
return await addMissedUpdatesRole(env);
}
default:
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services = [
]

[triggers]
crons = ["0 */6 * * *","0 */12 * * *" ]
crons = ["0 */6 * * *","0 */11 * * *" ]

# # Durable Object binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
# [[durable_objects]]
Expand Down

0 comments on commit 3922ccc

Please sign in to comment.