-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cog.py
More file actions
145 lines (129 loc) · 5.28 KB
/
test_cog.py
File metadata and controls
145 lines (129 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Contains all of the commands for the test bot."""
import gql
import nextcord
from gql.transport.aiohttp import AIOHTTPTransport
from nextcord.ext import commands
from constants import (
TESTING_GUILDS,
GITHUB_API_KEY,
RAILWAY_API_KEY,
DEV_CONTRIBUTOR_ROLE,
)
class BranchSelect(nextcord.ui.View):
"""Opens a string select dropdown to select the branch to switch the bot to."""
def __init__(self, branches: list[str]):
super().__init__()
self.branches: list[nextcord.SelectOption] = []
for branch in branches:
self.branches.append(nextcord.SelectOption(label=branch))
self.select = nextcord.ui.StringSelect(
placeholder="Select a branch", options=self.branches, row=1
)
self.add_item(self.select)
@nextcord.ui.button(label="Continue", style=nextcord.ButtonStyle.green, row=2)
async def finish(
self, _button: nextcord.ui.Button, _interaction: nextcord.Interaction
):
"""Continues setup"""
self.stop()
class TestCog(commands.Cog):
"""Contains all of the commands for the test bot."""
def __init__(self, bot: nextcord.Client):
self.bot: nextcord.Client = bot
@nextcord.slash_command(name="test", guild_ids=TESTING_GUILDS)
async def test(self, _interaction: nextcord.Interaction):
"""
This is the main slash command that will be the prefix of all commands below.
Also prefixes any test commands in other Cogs
This will never get called since it has subcommands.
"""
@test.subcommand(
name="branch",
description="Switch the branch that the test bot is deployed under.",
)
async def switch_branch(self, interaction: nextcord.Interaction):
"""Switches the branch that the test bot account is currently deployed off of."""
await interaction.response.defer()
if interaction.user.get_role(DEV_CONTRIBUTOR_ROLE) is None:
await interaction.send(
"You do not have the Contributors role and cannot run this command.",
ephemeral=True,
)
return
github_api = AIOHTTPTransport(
url="https://api.github.com/graphql",
headers={"Authorization": f"bearer {GITHUB_API_KEY}"},
)
branch_names = []
async with gql.Client(
transport=github_api,
fetch_schema_from_transport=False,
) as session:
list_branches = gql.gql(
"""
query getBranches {
repository(name: "pugBot", owner: "Ooglely") {
refs(first: 25, refPrefix: "refs/heads/") {
edges {
node {
name
}
}
}
}
}
"""
)
result = await session.execute(list_branches)
for branch in result["repository"]["refs"]["edges"]:
branch_names.append(branch["node"]["name"])
branch_select = BranchSelect(branch_names)
await interaction.send(
"Select a branch to deploy.", view=branch_select, ephemeral=False
)
status = await branch_select.wait()
if not status:
selected_branch = branch_select.select.values[0]
railway_api = AIOHTTPTransport(
url="https://backboard.railway.app/graphql/v2",
headers={"Authorization": f"Bearer {RAILWAY_API_KEY}"},
)
async with gql.Client(
transport=railway_api,
fetch_schema_from_transport=False,
) as session:
set_deployment_trigger = gql.gql(
f"""
mutation setDeploymentTrigger {{
deploymentTriggerUpdate(
id: "275e3203-4ac7-4ada-84de-1c11f8b9b124",
input: {{
branch: "{selected_branch}",
checkSuites: true,
repository: "Ooglely/pugBot",
}}
) {{
id
}}
}}
"""
)
redeploy_environment = gql.gql(
"""
mutation deployNewDeployment {
environmentTriggersDeploy(
input: {
environmentId: "5c2a716b-7bac-4dae-9ee4-78725cb1ee1a",
projectId: "8ffd3860-8187-406a-bf03-69d7356ec462",
serviceId: "01b0b783-64b1-4727-b8c9-5df09701c8ac"
}
)
}
"""
)
await session.execute(set_deployment_trigger)
await session.execute(redeploy_environment)
await interaction.edit_original_message(
content=f"Switching branch to `{selected_branch}`... Please check <#1144720434370203698> to see deployment progress.",
view=None,
)