Skip to content

Commit

Permalink
feat: check if build exists already
Browse files Browse the repository at this point in the history
Signed-off-by: Jyotiraditya Panda <jyotiraditya@aospa.co>
  • Loading branch information
imjyotiraditya authored and AntoninoScordino committed Sep 23, 2024
1 parent b5ee367 commit f757277
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dumpyarabot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
console = Console()


async def check_existing_build(url: str, use_alt_dumper: bool) -> tuple[bool, str]:
builds = await utils.get_jenkins_builds()
for build in builds:
build_params = build.get("parameters", {})
if (
build_params.get("URL") == url
and build_params.get("USE_ALT_DUMPER") == str(use_alt_dumper).lower()
):
result = build.get("result")
if result is None:
return (
True,
f"Build #{build['number']} is currently in progress for this URL and settings.",
)
elif result == "SUCCESS":
return (
True,
f"Build #{build['number']} has already successfully completed for this URL and settings.",
)
return False, ""


async def dump_main(
update: Update, context: ContextTypes.DEFAULT_TYPE, use_alt_dumper: bool = False
) -> None:
Expand Down Expand Up @@ -38,6 +60,16 @@ async def dump_main(
)
return

# Check if the URL has already been dumped
exists, message = await check_existing_build(context.args[0], use_alt_dumper)
if exists:
await context.bot.send_message(
chat_id=update.effective_chat.id,
reply_to_message_id=update.effective_message.id,
text=f"This URL has already been dumped. {message}",
)
return

# Try to call jenkins
try:
response_text = await utils.call_jenkins(
Expand Down
23 changes: 23 additions & 0 deletions dumpyarabot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
from dumpyarabot.config import settings


async def get_jenkins_builds() -> list[dict]:
"""
Get all builds from Jenkins.
:return: A list of dictionaries containing build information
"""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{settings.JENKINS_URL}/job/dumpyara/api/json?tree=allBuilds[number,result,actions[parameters[name,value]]]",
auth=(settings.JENKINS_USER_NAME, settings.JENKINS_USER_TOKEN),
)
response.raise_for_status()
builds = response.json()["allBuilds"]
for build in builds:
for action in build["actions"]:
if "parameters" in action:
build["parameters"] = {
param["name"]: param["value"] for param in action["parameters"]
}
break
return builds


async def call_jenkins(args: schemas.DumpArguments) -> str:
"""
Function to call jenkins
Expand Down

0 comments on commit f757277

Please sign in to comment.