Skip to content
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

Changes for migration to be postgres to be merged to main #86

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
102103b
addind print
sasi2312 Jul 31, 2024
0c23308
query converted to ORM
sasi2312 Aug 19, 2024
2908558
Merge branch 'development' of https://github.com/Code4GovTech/discord…
sasi2312 Aug 20, 2024
20eee57
Merge pull request #75 from Code4GovTech/migration_orm
sasi2312 Aug 21, 2024
353b425
Update build-and-push.yml
jaanbaaz Sep 12, 2024
1f6976c
Updated requirements.txt for psycopg2-binary
Srijan-SS02 Sep 30, 2024
5404627
MIgration from supabase to postgres changes
jaanbaaz Nov 14, 2024
4799c8e
Merge branch 'dev' of github.com:Code4GovTech/discord-bot into dev
jaanbaaz Nov 14, 2024
d36cb2e
MIgration from supabase to postgres changes
jaanbaaz Nov 15, 2024
c20b815
MIgration from supabase to postgres changes
jaanbaaz Nov 15, 2024
83c4199
MIgration from supabase to postgres changes
jaanbaaz Nov 15, 2024
469f82a
Fix env variables for postgres
karntrehan Nov 25, 2024
3364a90
postgres initialization changes
jaanbaaz Nov 26, 2024
6a70571
Merge branch 'dev' of github.com:Code4GovTech/discord-bot into dev
jaanbaaz Nov 26, 2024
cd8c28b
Add dependencies and fix logs
karntrehan Nov 28, 2024
ec6de60
migration to postgres bug fixes in postgres queries
jaanbaaz Dec 10, 2024
3644aed
Merge pull request #87 from Code4GovTech/feature/migration-to-postgres
jaanbaaz Dec 12, 2024
ced3991
Print token
karntrehan Dec 12, 2024
8b7aaf4
Print token
karntrehan Dec 12, 2024
23c444e
Adding logs to token
karntrehan Dec 12, 2024
92e9af3
add dev branch in the build worflow
singhalkarun Dec 13, 2024
0fd0464
is_active added to contributors discord
jaanbaaz Dec 13, 2024
74486e9
is_active added
jaanbaaz Dec 13, 2024
163e7d7
registration bug fix
jaanbaaz Dec 13, 2024
3dfb1e8
email check added
jaanbaaz Dec 15, 2024
bc255cc
discord_username and name fix
jaanbaaz Dec 24, 2024
4a0f5de
log added
jaanbaaz Dec 24, 2024
7319844
Merge pull request #88 from Code4GovTech/feature/unmerged-fix
jaanbaaz Dec 24, 2024
ef2c35d
Deploy ready branch
shreyash-work-fl Dec 30, 2024
0c2be2d
Merge pull request #90 from Code4GovTech/feature/shared-migrations-clean
Shreyash-work-em Dec 30, 2024
c67e6dd
removed submodule
shreyash-work-fl Jan 3, 2025
cf4d098
Readded submodule
shreyash-work-fl Jan 3, 2025
d5ee7f4
Updated dockerfile
Srijan-SS02 Jan 3, 2025
82c8300
Removed Submodule
shreyash-work-fl Jan 6, 2025
3cdcacf
Readded Submodule
shreyash-work-fl Jan 6, 2025
7590b5b
Removed Submodule
shreyash-work-fl Jan 6, 2025
5d572f7
Readded Submoduke
shreyash-work-fl Jan 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ SERVER_ID=
INTRODUCTIONS_CHANNEL=
NON_CONTRIBUTOR_ROLES=

FLASK_HOST=
GITHUB_PAT=
WEBHOOK_URL=

GITHUB_AUTHENTICATION_URL=

SUPABASE_URL=
SUPABASE_KEY=
VERIFIED_ROLE_ID=

POSTGRES_DB_HOST=
POSTGRES_DB_NAME=
POSTGRES_DB_USER=
POSTGRES_DB_PASS=
44 changes: 27 additions & 17 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,54 @@ on:
push:
branches:
- main
- dev
workflow_dispatch:

env:
DOCKER_USERNAME: ${{ github.actor }}
DOCKER_IMAGE_NAME: ${{ github.repository }}
DOCKER_REGISTRY: ghcr.io
DOCKER_IMAGE_TAG: ${{ github.ref_name }}
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
steps:
- id: lower-repo
shell: pwsh
run: |
"::set-output name=repository::$($env:DOCKER_IMAGE_NAME.ToLowerInvariant())"

- name: Checkout code
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to Docker registry
uses: docker/login-action@v1
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ env.DOCKER_USERNAME }}
password: ${{ secrets.PAT }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# minimal
type=pep440,pattern={{version}},value=${{ github.ref_name }},enable=${{ github.event_name == 'release' }}
# branch event
type=ref,event=branch
type=raw,value=latest,enable=${{ github.event_name == 'release' }}

- name: Build and Push Docker image
uses: docker/build-push-action@v4
with:
build-args: |
"SERVER_RELEASE_VERSION=${{ github.sha }}"
# build-args:
context: .
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ env.DOCKER_REGISTRY }}/${{ steps.lower-repo.outputs.repository }}:${{env.DOCKER_IMAGE_TAG}}
labels: org.opencontainers.image.source=https://github.com/${{steps.lower-repo.outputs.repository}}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "shared_migrations"]
path = shared_migrations
url = https://github.com/Code4GovTech/shared-models-migrations.git
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ FROM python:3.9-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN apt-get update && \
apt-get install -y --no-install-recommends git openssh-client && \
rm -rf /var/lib/apt/lists/*


COPY . /app

RUN --mount=type=ssh git submodule update --init --recursive

RUN pip3 install -r requirements.txt

COPY . .
Expand Down
16 changes: 9 additions & 7 deletions cogs/badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient
from shared_migrations.db.discord_bot import DiscordBotQueries


class BadgeModal(discord.ui.Modal, title="Your Badges"):
Expand All @@ -17,7 +17,9 @@ async def on_timeout(self, interaction):

class BadgeContents:
def __init__(self, name) -> None:
self.postgres_client = DiscordBotQueries()
apprentinceDesc = f"""Welcome *{name}*!!



Congratulations! 🎉 You have taken the first step to join & introduce yourself to this awesome community and earned the **Apprentice Badge**! 🎓 This badge shows that you are eager to learn and grow with our community! 😎 We are so happy to have you here and we can’t wait to see what you will create and solve! 🚀"""
Expand Down Expand Up @@ -133,7 +135,7 @@ def get_user_badges(self, discord_id):
userBadges = {"points": [], "achievements": []}
if (
len(
SupabaseClient().read(
self.postgres_client.read(
"contributors_registration",
query_key="discord_id",
query_value=discord_id,
Expand All @@ -143,7 +145,7 @@ def get_user_badges(self, discord_id):
):
userBadges["achievements"].append(self.discordXGithubBadge)

discordMemberData = SupabaseClient().read(
discordMemberData = self.postgres_client.read(
"discord_engagement", "contributor", discord_id
)
if discordMemberData:
Expand All @@ -153,17 +155,17 @@ def get_user_badges(self, discord_id):
userBadges["achievements"].append(self.rockstarBadge)
if discordMemberData[0]["has_introduced"]:
userBadges["achievements"].append(self.apprenticeBadge)
contributorData = SupabaseClient().read(
contributorData = self.postgres_client.read(
"contributors_registration", query_key="discord_id", query_value=discord_id
)
if contributorData:
github_id = contributorData[0]["github_id"]
prData = {
"raised": SupabaseClient().read(
"raised": self.postgres_client.read(
table="connected_prs", query_key="raised_by", query_value=github_id
),
"merged": SupabaseClient(table="connected_prs").read(
table="connected_prs", query_key="merged_by", query_value=github_id
"merged": self.postgres_client.read(
"connected_prs", query_key="merged_by", query_value=github_id
),
}
points = 0
Expand Down
52 changes: 12 additions & 40 deletions cogs/discordDataScraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from discord.channel import TextChannel
from discord.ext import commands, tasks

from helpers.supabaseClient import SupabaseClient
from shared_migrations.db.discord_bot import DiscordBotQueries

with open("config.json") as config_file:
config_data = json.load(config_file)
Expand All @@ -24,49 +24,21 @@
class DiscordDataScaper(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
self.postgres_client = DiscordBotQueries()

@commands.Cog.listener()
async def on_message(self, message):
pass
# contributor = SupabaseClient().read(
# "discord_engagement", "contributor", message.author.id
# )
# print("message", len(message.content))
# if not contributor:
# SupabaseClient().insert(
# "discord_engagement",
# {
# "contributor": message.author.id,
# "has_introduced": False,
# "total_message_count": 1,
# "total_reaction_count": 0,
# },
# )
# return
# if len(message.content) > 20:
# if message.channel.id == INTRODUCTIONS_CHANNEL_ID:
# print("intro")
# SupabaseClient().update(
# "discord_engagement",
# {"has_introduced": True},
# "contributor",
# message.author.id,
# )
# SupabaseClient("discord_engagement").update(
# "discord_engagement",
# {"total_message_count": contributor[0]["total_message_count"] + 1},
# "contributor",
# message.author.id,
# )


@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
message = reaction.message
contributor = SupabaseClient().read(
contributor = self.postgres_client.read(
"discord_engagement", "contributor", message.author.id
)[0]
if not contributor:
SupabaseClient().insert(
self.postgres_client.insert(
"discord_engagement",
{
"contributor": message.author.id,
Expand All @@ -77,7 +49,7 @@ async def on_reaction_add(self, reaction, user):
)
return
print("reaction")
SupabaseClient().update(
self.postgres_client.update(
"discord_engagement",
{"total_reaction_count": contributor["total_reaction_count"] + 1},
"contributor",
Expand All @@ -89,7 +61,7 @@ async def add_engagement(self, ctx):
await ctx.channel.send("started")

def addEngagmentData(data):
client = SupabaseClient()
client = self.postgres_client
client.insert("discord_engagement", data)
return

Expand Down Expand Up @@ -145,7 +117,7 @@ async def enable_webhook(self, ctx):
channels = await guild.fetch_channels()
enabled = [
channel["channel_id"]
for channel in SupabaseClient().read_all("discord_channels")
for channel in self.postgres_client.read_all("discord_channels")
]
for channel in channels:
try:
Expand All @@ -156,7 +128,7 @@ async def enable_webhook(self, ctx):
webhook = await channel.create_webhook(name="New Ticket Alert")
feedback = f"""URL: {webhook.url}\n Token:{"Yes" if webhook.token else "No"}"""
await ctx.send(feedback)
SupabaseClient().insert(
self.postgres_client.insert(
"discord_channels",
{
"channel_id": channel.id,
Expand All @@ -177,7 +149,7 @@ async def update_applicants(self, ctx):
await ctx.send("Member List Count: " + str(len(members)))
for member in members:
try:
SupabaseClient().insert(
self.postgres_client.insert(
"applicant",
{"sheet_username": member.name, "discord_id": member.id},
)
Expand Down Expand Up @@ -216,12 +188,12 @@ async def collect_all_messages(self):

async def add_messages(self):
def addMessageData(data):
client = SupabaseClient()
client = self.postgres_client
client.insert("unstructured discord data", data)
return

def getLastMessageObject(channelId):
last_message = SupabaseClient().read_by_order_limit(
last_message = self.postgres_client.read_by_order_limit(
table="unstructured discord data",
query_key="channel",
query_value=channelId,
Expand Down
8 changes: 4 additions & 4 deletions cogs/listeners/member_events_cog.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient

from shared_migrations.db.discord_bot import DiscordBotQueries

class MemberEventsListener(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
self.postgres_client = DiscordBotQueries()
super().__init__()

@commands.Cog.listener("on_member_join")
async def on_member_join(self, member: discord.Member):
SupabaseClient().updateContributor(member)
self.postgres_client.updateContributor(member)

@commands.Cog.listener("on_member_remove")
async def on_member_remove(self, member: discord.Member):
Expand All @@ -20,7 +20,7 @@ async def on_member_remove(self, member: discord.Member):

@commands.Cog.listener("on_member_update")
async def on_member_update(self, before: discord.Member, after: discord.Member):
SupabaseClient().updateContributor(after)
self.postgres_client.updateContributor(after)


async def setup(bot: commands.Bot):
Expand Down
7 changes: 3 additions & 4 deletions cogs/listeners/message_events_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from discord.ext import commands

from config.server import ServerConfig
from helpers.supabaseClient import SupabaseClient

from shared_migrations.db.discord_bot import DiscordBotQueries
serverConfig = ServerConfig()
supabaseClient = SupabaseClient()
postgresClient = DiscordBotQueries()


async def grantVerifiedRole(member: discord.Member):
Expand All @@ -32,7 +31,7 @@ def __init__(self, bot) -> None:
async def on_message(self, message: discord.Message):
# Listen for Introduction
if message.channel.id == serverConfig.Channels.INTRODUCTION_CHANNEL:
if await supabaseClient.memberIsAuthenticated(message.author):
if await postgresClient.memberIsAuthenticated(message.author):
await grantVerifiedRole(message.author)
else:
return
Expand Down
14 changes: 7 additions & 7 deletions cogs/listeners/role_events_cog.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient

from shared_migrations.db.discord_bot import DiscordBotQueries

class RoleEventsListener(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
self.postgres_client = DiscordBotQueries()
super().__init__()

@commands.Cog.listener()
async def on_guild_role_create(self, role: discord.Role):
if role.name.startswith("College:"):
orgName = role.name[len("College: ") :]
SupabaseClient().addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
self.postgres_client.addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
elif role.name.startswith("Corporate:"):
orgName = role.name[len("Corporate: ") :]
SupabaseClient().addChapter(
self.postgres_client.addChapter(
roleId=role.id, orgName=orgName, type="CORPORATE"
)

@commands.Cog.listener()
async def on_guild_role_delete(self, role: discord.Role):
SupabaseClient().deleteChapter(roleID=role.id)
self.postgres_client.deleteChapter(roleID=role.id)

@commands.Cog.listener()
async def on_guild_role_update(self, before: discord.Role, after: discord.Role):
if after.name.startswith("College:"):
orgName = after.name[len("College: ") :]
SupabaseClient().addChapter(
self.postgres_client.addChapter(
roleId=after.id, orgName=orgName, type="COLLEGE"
)
elif after.name.startswith("Corporate:"):
orgName = after.name[len("Corporate: ") :]
SupabaseClient().addChapter(
self.postgres_client.addChapter(
roleId=after.id, orgName=orgName, type="CORPORATE"
)

Expand Down
Loading
Loading