Skip to content

Commit b73f30b

Browse files
authored
Merge pull request #94 from Code4GovTech/dev
Migrations to Prod
2 parents e9b448b + 5d572f7 commit b73f30b

19 files changed

+3473
-145
lines changed

.env.sample

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ SERVER_ID=
44
INTRODUCTIONS_CHANNEL=
55
NON_CONTRIBUTOR_ROLES=
66

7-
FLASK_HOST=
8-
GITHUB_PAT=
97
WEBHOOK_URL=
108

119
GITHUB_AUTHENTICATION_URL=
1210

1311
SUPABASE_URL=
1412
SUPABASE_KEY=
1513
VERIFIED_ROLE_ID=
14+
15+
POSTGRES_DB_HOST=
16+
POSTGRES_DB_NAME=
17+
POSTGRES_DB_USER=
18+
POSTGRES_DB_PASS=

.github/workflows/build-and-push.yml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,54 @@ on:
44
push:
55
branches:
66
- main
7+
- dev
78
workflow_dispatch:
89

910
env:
10-
DOCKER_USERNAME: ${{ github.actor }}
11-
DOCKER_IMAGE_NAME: ${{ github.repository }}
12-
DOCKER_REGISTRY: ghcr.io
13-
DOCKER_IMAGE_TAG: ${{ github.ref_name }}
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: ${{ github.repository }}
1413

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

2423
- name: Checkout code
2524
uses: actions/checkout@v2
2625

2726
- name: Set up Docker Buildx
2827
uses: docker/setup-buildx-action@v2
28+
29+
- name: Log in to the Container registry
30+
uses: docker/login-action@v3
31+
with:
32+
registry: ${{ env.REGISTRY }}
33+
username: ${{ github.actor }}
34+
password: ${{ secrets.GITHUB_TOKEN }}
2935

30-
- name: Login to Docker registry
31-
uses: docker/login-action@v1
36+
- name: Extract metadata (tags, labels) for Docker
37+
id: meta
38+
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
3239
with:
33-
registry: ${{ env.DOCKER_REGISTRY }}
34-
username: ${{ env.DOCKER_USERNAME }}
35-
password: ${{ secrets.PAT }}
40+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
41+
tags: |
42+
# minimal
43+
type=pep440,pattern={{version}},value=${{ github.ref_name }},enable=${{ github.event_name == 'release' }}
44+
# branch event
45+
type=ref,event=branch
46+
type=raw,value=latest,enable=${{ github.event_name == 'release' }}
3647
3748
- name: Build and Push Docker image
3849
uses: docker/build-push-action@v4
3950
with:
40-
build-args: |
41-
"SERVER_RELEASE_VERSION=${{ github.sha }}"
51+
# build-args:
4252
context: .
4353
push: true
4454
cache-from: type=gha
4555
cache-to: type=gha,mode=max
46-
tags: ${{ env.DOCKER_REGISTRY }}/${{ steps.lower-repo.outputs.repository }}:${{env.DOCKER_IMAGE_TAG}}
47-
labels: org.opencontainers.image.source=https://github.com/${{steps.lower-repo.outputs.repository}}
56+
tags: ${{ steps.meta.outputs.tags }}
57+
labels: ${{ steps.meta.outputs.labels }}

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "shared_migrations"]
2+
path = shared_migrations
3+
url = https://github.com/Code4GovTech/shared-models-migrations.git

Dockerfile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ FROM python:3.9-slim-buster
22

33
WORKDIR /app
44

5-
COPY requirements.txt requirements.txt
5+
RUN apt-get update && \
6+
apt-get install -y --no-install-recommends git openssh-client && \
7+
rm -rf /var/lib/apt/lists/*
8+
9+
10+
COPY . /app
11+
12+
RUN --mount=type=ssh git submodule update --init --recursive
13+
614
RUN pip3 install -r requirements.txt
715

816
COPY . .

cogs/badges.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import discord
44
from discord.ext import commands
55

6-
from helpers.supabaseClient import SupabaseClient
6+
from shared_migrations.db.discord_bot import DiscordBotQueries
77

88

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

1818
class BadgeContents:
1919
def __init__(self, name) -> None:
20+
self.postgres_client = DiscordBotQueries()
2021
apprentinceDesc = f"""Welcome *{name}*!!
22+
2123
2224
2325
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! 🚀"""
@@ -133,7 +135,7 @@ def get_user_badges(self, discord_id):
133135
userBadges = {"points": [], "achievements": []}
134136
if (
135137
len(
136-
SupabaseClient().read(
138+
self.postgres_client.read(
137139
"contributors_registration",
138140
query_key="discord_id",
139141
query_value=discord_id,
@@ -143,7 +145,7 @@ def get_user_badges(self, discord_id):
143145
):
144146
userBadges["achievements"].append(self.discordXGithubBadge)
145147

146-
discordMemberData = SupabaseClient().read(
148+
discordMemberData = self.postgres_client.read(
147149
"discord_engagement", "contributor", discord_id
148150
)
149151
if discordMemberData:
@@ -153,17 +155,17 @@ def get_user_badges(self, discord_id):
153155
userBadges["achievements"].append(self.rockstarBadge)
154156
if discordMemberData[0]["has_introduced"]:
155157
userBadges["achievements"].append(self.apprenticeBadge)
156-
contributorData = SupabaseClient().read(
158+
contributorData = self.postgres_client.read(
157159
"contributors_registration", query_key="discord_id", query_value=discord_id
158160
)
159161
if contributorData:
160162
github_id = contributorData[0]["github_id"]
161163
prData = {
162-
"raised": SupabaseClient().read(
164+
"raised": self.postgres_client.read(
163165
table="connected_prs", query_key="raised_by", query_value=github_id
164166
),
165-
"merged": SupabaseClient(table="connected_prs").read(
166-
table="connected_prs", query_key="merged_by", query_value=github_id
167+
"merged": self.postgres_client.read(
168+
"connected_prs", query_key="merged_by", query_value=github_id
167169
),
168170
}
169171
points = 0

cogs/discordDataScraper.py

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from discord.channel import TextChannel
1010
from discord.ext import commands, tasks
1111

12-
from helpers.supabaseClient import SupabaseClient
12+
from shared_migrations.db.discord_bot import DiscordBotQueries
1313

1414
with open("config.json") as config_file:
1515
config_data = json.load(config_file)
@@ -24,49 +24,21 @@
2424
class DiscordDataScaper(commands.Cog):
2525
def __init__(self, bot) -> None:
2626
self.bot = bot
27+
self.postgres_client = DiscordBotQueries()
2728

2829
@commands.Cog.listener()
2930
async def on_message(self, message):
3031
pass
31-
# contributor = SupabaseClient().read(
32-
# "discord_engagement", "contributor", message.author.id
33-
# )
34-
# print("message", len(message.content))
35-
# if not contributor:
36-
# SupabaseClient().insert(
37-
# "discord_engagement",
38-
# {
39-
# "contributor": message.author.id,
40-
# "has_introduced": False,
41-
# "total_message_count": 1,
42-
# "total_reaction_count": 0,
43-
# },
44-
# )
45-
# return
46-
# if len(message.content) > 20:
47-
# if message.channel.id == INTRODUCTIONS_CHANNEL_ID:
48-
# print("intro")
49-
# SupabaseClient().update(
50-
# "discord_engagement",
51-
# {"has_introduced": True},
52-
# "contributor",
53-
# message.author.id,
54-
# )
55-
# SupabaseClient("discord_engagement").update(
56-
# "discord_engagement",
57-
# {"total_message_count": contributor[0]["total_message_count"] + 1},
58-
# "contributor",
59-
# message.author.id,
60-
# )
32+
6133

6234
@commands.Cog.listener()
6335
async def on_reaction_add(self, reaction, user):
6436
message = reaction.message
65-
contributor = SupabaseClient().read(
37+
contributor = self.postgres_client.read(
6638
"discord_engagement", "contributor", message.author.id
6739
)[0]
6840
if not contributor:
69-
SupabaseClient().insert(
41+
self.postgres_client.insert(
7042
"discord_engagement",
7143
{
7244
"contributor": message.author.id,
@@ -77,7 +49,7 @@ async def on_reaction_add(self, reaction, user):
7749
)
7850
return
7951
print("reaction")
80-
SupabaseClient().update(
52+
self.postgres_client.update(
8153
"discord_engagement",
8254
{"total_reaction_count": contributor["total_reaction_count"] + 1},
8355
"contributor",
@@ -89,7 +61,7 @@ async def add_engagement(self, ctx):
8961
await ctx.channel.send("started")
9062

9163
def addEngagmentData(data):
92-
client = SupabaseClient()
64+
client = self.postgres_client
9365
client.insert("discord_engagement", data)
9466
return
9567

@@ -145,7 +117,7 @@ async def enable_webhook(self, ctx):
145117
channels = await guild.fetch_channels()
146118
enabled = [
147119
channel["channel_id"]
148-
for channel in SupabaseClient().read_all("discord_channels")
120+
for channel in self.postgres_client.read_all("discord_channels")
149121
]
150122
for channel in channels:
151123
try:
@@ -156,7 +128,7 @@ async def enable_webhook(self, ctx):
156128
webhook = await channel.create_webhook(name="New Ticket Alert")
157129
feedback = f"""URL: {webhook.url}\n Token:{"Yes" if webhook.token else "No"}"""
158130
await ctx.send(feedback)
159-
SupabaseClient().insert(
131+
self.postgres_client.insert(
160132
"discord_channels",
161133
{
162134
"channel_id": channel.id,
@@ -177,7 +149,7 @@ async def update_applicants(self, ctx):
177149
await ctx.send("Member List Count: " + str(len(members)))
178150
for member in members:
179151
try:
180-
SupabaseClient().insert(
152+
self.postgres_client.insert(
181153
"applicant",
182154
{"sheet_username": member.name, "discord_id": member.id},
183155
)
@@ -216,12 +188,12 @@ async def collect_all_messages(self):
216188

217189
async def add_messages(self):
218190
def addMessageData(data):
219-
client = SupabaseClient()
191+
client = self.postgres_client
220192
client.insert("unstructured discord data", data)
221193
return
222194

223195
def getLastMessageObject(channelId):
224-
last_message = SupabaseClient().read_by_order_limit(
196+
last_message = self.postgres_client.read_by_order_limit(
225197
table="unstructured discord data",
226198
query_key="channel",
227199
query_value=channelId,

cogs/listeners/member_events_cog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import discord
22
from discord.ext import commands
33

4-
from helpers.supabaseClient import SupabaseClient
5-
4+
from shared_migrations.db.discord_bot import DiscordBotQueries
65

76
class MemberEventsListener(commands.Cog):
87
def __init__(self, bot) -> None:
98
self.bot = bot
9+
self.postgres_client = DiscordBotQueries()
1010
super().__init__()
1111

1212
@commands.Cog.listener("on_member_join")
1313
async def on_member_join(self, member: discord.Member):
14-
SupabaseClient().updateContributor(member)
14+
self.postgres_client.updateContributor(member)
1515

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

2121
@commands.Cog.listener("on_member_update")
2222
async def on_member_update(self, before: discord.Member, after: discord.Member):
23-
SupabaseClient().updateContributor(after)
23+
self.postgres_client.updateContributor(after)
2424

2525

2626
async def setup(bot: commands.Bot):

cogs/listeners/message_events_cog.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from discord.ext import commands
33

44
from config.server import ServerConfig
5-
from helpers.supabaseClient import SupabaseClient
6-
5+
from shared_migrations.db.discord_bot import DiscordBotQueries
76
serverConfig = ServerConfig()
8-
supabaseClient = SupabaseClient()
7+
postgresClient = DiscordBotQueries()
98

109

1110
async def grantVerifiedRole(member: discord.Member):
@@ -32,7 +31,7 @@ def __init__(self, bot) -> None:
3231
async def on_message(self, message: discord.Message):
3332
# Listen for Introduction
3433
if message.channel.id == serverConfig.Channels.INTRODUCTION_CHANNEL:
35-
if await supabaseClient.memberIsAuthenticated(message.author):
34+
if await postgresClient.memberIsAuthenticated(message.author):
3635
await grantVerifiedRole(message.author)
3736
else:
3837
return

cogs/listeners/role_events_cog.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import discord
22
from discord.ext import commands
33

4-
from helpers.supabaseClient import SupabaseClient
5-
4+
from shared_migrations.db.discord_bot import DiscordBotQueries
65

76
class RoleEventsListener(commands.Cog):
87
def __init__(self, bot) -> None:
98
self.bot = bot
9+
self.postgres_client = DiscordBotQueries()
1010
super().__init__()
1111

1212
@commands.Cog.listener()
1313
async def on_guild_role_create(self, role: discord.Role):
1414
if role.name.startswith("College:"):
1515
orgName = role.name[len("College: ") :]
16-
SupabaseClient().addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
16+
self.postgres_client.addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
1717
elif role.name.startswith("Corporate:"):
1818
orgName = role.name[len("Corporate: ") :]
19-
SupabaseClient().addChapter(
19+
self.postgres_client.addChapter(
2020
roleId=role.id, orgName=orgName, type="CORPORATE"
2121
)
2222

2323
@commands.Cog.listener()
2424
async def on_guild_role_delete(self, role: discord.Role):
25-
SupabaseClient().deleteChapter(roleID=role.id)
25+
self.postgres_client.deleteChapter(roleID=role.id)
2626

2727
@commands.Cog.listener()
2828
async def on_guild_role_update(self, before: discord.Role, after: discord.Role):
2929
if after.name.startswith("College:"):
3030
orgName = after.name[len("College: ") :]
31-
SupabaseClient().addChapter(
31+
self.postgres_client.addChapter(
3232
roleId=after.id, orgName=orgName, type="COLLEGE"
3333
)
3434
elif after.name.startswith("Corporate:"):
3535
orgName = after.name[len("Corporate: ") :]
36-
SupabaseClient().addChapter(
36+
self.postgres_client.addChapter(
3737
roleId=after.id, orgName=orgName, type="CORPORATE"
3838
)
3939

0 commit comments

Comments
 (0)