matrix-bot: Fix bot spamming changelogs
All checks were successful
buildbot/nix-build .#checks.x86_64-linux.clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-matrix-bot Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-default Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-flake-update Build done.
buildbot/nix-build .#checks.x86_64-linux.treefmt Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-flake-update-pr-clan Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gitea Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-core Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-homepage Build done.
buildbot/nix-build .#checks.x86_64-linux.package-matrix-bot Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-infra Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-create-pr Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-ensure-tea-login Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-web01 Build done.
buildbot/nix-eval Build done.
All checks were successful
buildbot/nix-build .#checks.x86_64-linux.clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-matrix-bot Build done.
buildbot/nix-build .#checks.x86_64-linux.devShell-default Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-flake-update Build done.
buildbot/nix-build .#checks.x86_64-linux.treefmt Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-flake-update-pr-clan Build done.
buildbot/nix-build .#checks.x86_64-linux.package-clan-merge Build done.
buildbot/nix-build .#checks.x86_64-linux.package-gitea Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-core Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-homepage Build done.
buildbot/nix-build .#checks.x86_64-linux.package-matrix-bot Build done.
buildbot/nix-build .#checks.x86_64-linux.package-job-flake-update-clan-infra Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-create-pr Build done.
buildbot/nix-build .#checks.x86_64-linux.package-action-ensure-tea-login Build done.
buildbot/nix-build .#checks.x86_64-linux.nixos-web01 Build done.
buildbot/nix-eval Build done.
This commit is contained in:
parent
ade9c76e6b
commit
c36d176982
@ -120,6 +120,13 @@ def create_parser(prog: str | None = None) -> argparse.ArgumentParser:
|
|||||||
type=Path,
|
type=Path,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--poll-frequency",
|
||||||
|
help="The frequency to poll for new reviews in minutes",
|
||||||
|
default=10,
|
||||||
|
type=float,
|
||||||
|
)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
@ -161,6 +168,7 @@ def main() -> None:
|
|||||||
owner=args.repo_owner,
|
owner=args.repo_owner,
|
||||||
repo=args.repo_name,
|
repo=args.repo_name,
|
||||||
access_token=os.getenv("GITEA_ACCESS_TOKEN"),
|
access_token=os.getenv("GITEA_ACCESS_TOKEN"),
|
||||||
|
poll_frequency=args.poll_frequency,
|
||||||
)
|
)
|
||||||
|
|
||||||
args.data_dir.mkdir(parents=True, exist_ok=True)
|
args.data_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
@ -109,31 +109,32 @@ async def changelog_bot(
|
|||||||
last_run_path = data_dir / "last_changelog_run.json"
|
last_run_path = data_dir / "last_changelog_run.json"
|
||||||
last_run = read_locked_file(last_run_path)
|
last_run = read_locked_file(last_run_path)
|
||||||
|
|
||||||
|
today = datetime.datetime.now()
|
||||||
|
today_weekday = today.strftime("%A")
|
||||||
|
|
||||||
|
if today_weekday != matrix.publish_day:
|
||||||
|
log.debug(f"Changelog not due yet. Due on {matrix.publish_day}")
|
||||||
|
return
|
||||||
|
|
||||||
if last_run == {}:
|
if last_run == {}:
|
||||||
fromdate, todate = last_ndays_to_today(matrix.changelog_frequency)
|
|
||||||
last_run = {
|
|
||||||
"fromdate": fromdate,
|
|
||||||
"todate": todate,
|
|
||||||
"ndays": matrix.changelog_frequency,
|
|
||||||
}
|
|
||||||
log.debug(f"First run. Setting last_run to {last_run}")
|
log.debug(f"First run. Setting last_run to {last_run}")
|
||||||
today = datetime.datetime.now()
|
|
||||||
today_weekday = today.strftime("%A")
|
|
||||||
if today_weekday != matrix.publish_day:
|
|
||||||
log.debug(f"Changelog not due yet. Due on {matrix.publish_day}")
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
last_date = datetime.datetime.strptime(last_run["todate"], "%Y-%m-%d")
|
last_date = datetime.datetime.strptime(last_run["todate"], "%Y-%m-%d")
|
||||||
today = datetime.datetime.now()
|
upper_bound = datetime.timedelta(days=matrix.changelog_frequency)
|
||||||
today_weekday = today.strftime("%A")
|
delta = today - last_date
|
||||||
delta = datetime.timedelta(days=matrix.changelog_frequency)
|
if delta <= upper_bound:
|
||||||
if today - last_date <= delta:
|
log.debug(
|
||||||
log.debug(f"Changelog not due yet. Due in {delta.days} days")
|
f"Changelog not due yet. Due in {upper_bound.days - delta.days} days"
|
||||||
return
|
)
|
||||||
elif today_weekday != matrix.publish_day:
|
|
||||||
log.debug(f"Changelog not due yet. Due on {matrix.publish_day}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
fromdate, todate = last_ndays_to_today(matrix.changelog_frequency)
|
||||||
|
last_run = {
|
||||||
|
"fromdate": fromdate,
|
||||||
|
"todate": todate,
|
||||||
|
"ndays": matrix.changelog_frequency,
|
||||||
|
}
|
||||||
|
|
||||||
# If you made a new room and haven't joined as that user, you can use
|
# If you made a new room and haven't joined as that user, you can use
|
||||||
room: JoinResponse = await client.join(matrix.changelog_room)
|
room: JoinResponse = await client.join(matrix.changelog_room)
|
||||||
|
|
||||||
@ -161,6 +162,11 @@ async def changelog_bot(
|
|||||||
fromdate, todate = last_ndays_to_today(matrix.changelog_frequency)
|
fromdate, todate = last_ndays_to_today(matrix.changelog_frequency)
|
||||||
log.info(f"Generating changelog from {fromdate} to {todate}")
|
log.info(f"Generating changelog from {fromdate} to {todate}")
|
||||||
|
|
||||||
|
# Write the last run to the file before processing the changelog
|
||||||
|
# This ensures that the changelog is only generated once per period
|
||||||
|
# even if openai fails
|
||||||
|
write_locked_file(last_run_path, last_run)
|
||||||
|
|
||||||
system_prompt = f"""
|
system_prompt = f"""
|
||||||
Create a concise changelog
|
Create a concise changelog
|
||||||
Follow these guidelines:
|
Follow these guidelines:
|
||||||
@ -216,8 +222,6 @@ For the last {matrix.changelog_frequency} days from {fromdate} to {todate}
|
|||||||
all_changelogs.append(changelog)
|
all_changelogs.append(changelog)
|
||||||
full_changelog = "\n\n".join(all_changelogs)
|
full_changelog = "\n\n".join(all_changelogs)
|
||||||
|
|
||||||
# Write the last run to the file
|
|
||||||
write_locked_file(last_run_path, last_run)
|
|
||||||
log.info(f"Changelog generated:\n{full_changelog}")
|
log.info(f"Changelog generated:\n{full_changelog}")
|
||||||
|
|
||||||
await send_message(client, room, full_changelog)
|
await send_message(client, room, full_changelog)
|
||||||
|
@ -14,7 +14,8 @@ class GiteaData:
|
|||||||
url: str
|
url: str
|
||||||
owner: str
|
owner: str
|
||||||
repo: str
|
repo: str
|
||||||
access_token: str | None = None
|
poll_frequency: float
|
||||||
|
access_token: str | None
|
||||||
|
|
||||||
|
|
||||||
def endpoint_url(gitea: GiteaData, endpoint: str) -> str:
|
def endpoint_url(gitea: GiteaData, endpoint: str) -> str:
|
||||||
|
@ -59,7 +59,8 @@ async def bot_main(
|
|||||||
client, matrix, f"Review requested bot failed: {e}"
|
client, matrix, f"Review requested bot failed: {e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
await asyncio.sleep(60 * 5)
|
log.debug(f"Sleeping for {60 * gitea.poll_frequency / 60} minutes")
|
||||||
|
await asyncio.sleep(60 * gitea.poll_frequency)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
finally:
|
finally:
|
||||||
|
Loading…
Reference in New Issue
Block a user