Merge pull request 'matrix-bot: Fix bot spamming changelogs' (#214) from Qubasa/clan-infra:Qubasa-main into main
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.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-job-flake-update-clan-infra 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-matrix-bot Build done.
buildbot/nix-build .#checks.x86_64-linux.treefmt 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:
clan-bot 2024-07-10 08:42:17 +00:00
commit 10bd57de32
4 changed files with 37 additions and 23 deletions

View File

@ -120,6 +120,13 @@ def create_parser(prog: str | None = None) -> argparse.ArgumentParser:
type=Path,
)
parser.add_argument(
"--poll-frequency",
help="The frequency to poll for new reviews in minutes",
default=10,
type=float,
)
return parser
@ -161,6 +168,7 @@ def main() -> None:
owner=args.repo_owner,
repo=args.repo_name,
access_token=os.getenv("GITEA_ACCESS_TOKEN"),
poll_frequency=args.poll_frequency,
)
args.data_dir.mkdir(parents=True, exist_ok=True)

View File

@ -109,31 +109,32 @@ async def changelog_bot(
last_run_path = data_dir / "last_changelog_run.json"
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 == {}:
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}")
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:
last_date = datetime.datetime.strptime(last_run["todate"], "%Y-%m-%d")
today = datetime.datetime.now()
today_weekday = today.strftime("%A")
delta = datetime.timedelta(days=matrix.changelog_frequency)
if today - last_date <= delta:
log.debug(f"Changelog not due yet. Due in {delta.days} days")
return
elif today_weekday != matrix.publish_day:
log.debug(f"Changelog not due yet. Due on {matrix.publish_day}")
upper_bound = datetime.timedelta(days=matrix.changelog_frequency)
delta = today - last_date
if delta <= upper_bound:
log.debug(
f"Changelog not due yet. Due in {upper_bound.days - delta.days} days"
)
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
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)
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"""
Create a concise changelog
Follow these guidelines:
@ -216,8 +222,6 @@ For the last {matrix.changelog_frequency} days from {fromdate} to {todate}
all_changelogs.append(changelog)
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}")
await send_message(client, room, full_changelog)

View File

@ -14,7 +14,8 @@ class GiteaData:
url: str
owner: str
repo: str
access_token: str | None = None
poll_frequency: float
access_token: str | None
def endpoint_url(gitea: GiteaData, endpoint: str) -> str:

View File

@ -59,7 +59,8 @@ async def bot_main(
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:
log.exception(e)
finally: