Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a replay command #334

Merged
merged 1 commit into from
Mar 20, 2024
Merged

Conversation

Freedisch
Copy link
Contributor

@Freedisch Freedisch commented Mar 4, 2024

Description of changes

  • Related issue Add a replay command #332
  • Added a new command replay is added to the CLI, taking a single argument message_id, which represents the ID of the message to replay.
  • The publish_message function uses Message.load_message to create a Message object from the JSON data, ensuring all headers and properties are preserved, and then publishes it using api.publish.

@Freedisch
Copy link
Contributor Author

Freedisch commented Mar 5, 2024

I'm not quite sure of the reason behind the failing of these CI tests, but I'm exploring it to see what's causing it🤔

@gridhead
Copy link
Member

gridhead commented Mar 7, 2024

The tests might not necessarily fail from the changes that you made as they are installability tests but try adjusting the version requirement for the pytz dependency. From https://github.com/fedora-infra/fedora-messaging/pull/334/checks?check_run_id=22273594026, I got to the log dumps present at https://download.copr.fedorainfracloud.org/results/packit/fedora-infra-fedora-messaging-334/fedora-40-x86_64/07105096-fedora-messaging/builder-live.log.gz and here is an excerpt of what causes the failures.

INFO: Going to install missing dynamic buildrequires
Updating and loading repositories:
 updates                                100% | 803.1 KiB/s |  23.3 KiB |  00m00s
 fedora                                 100% | 691.1 KiB/s |  20.0 KiB |  00m00s
 Copr repository                        100% |  44.4 KiB/s |   1.5 KiB |  00m00s
Repositories loaded.
Failed to resolve the transaction:
Package "pyproject-rpm-macros-1.12.0-1.fc40.noarch" is already installed.
Package "python3-devel-3.12.2-1.fc40.x86_64" is already installed.
Package "python3-sphinx-1:7.2.6-6.fc40.noarch" is already installed.
Package "python3-packaging-23.2-4.fc40.noarch" is already installed.
Package "python3-pip-23.3.2-1.fc40.noarch" is already installed.
Package "python3-poetry-core-1.8.1-3.fc40.noarch" is already installed.
Package "systemd-rpm-macros-255.3-1.fc40.noarch" is already installed.
Problem: nothing provides requested (python3dist(pytz) < 2024~~ with python3dist(pytz) >= 2023.3)

@gridhead gridhead self-requested a review March 7, 2024 05:47
@gridhead gridhead linked an issue Mar 7, 2024 that may be closed by this pull request
Copy link
Contributor

mergify bot commented Mar 7, 2024

⚠️ The sha of the head commit of this PR conflicts with #335. Mergify cannot evaluate rules on this PR. ⚠️

@gridhead
Copy link
Member

gridhead commented Mar 7, 2024

This should do it.

Also as a rule of thumb - please use the simple present verb in your commit messages.

Commit messages describe what a commit does to a repository and not what you did in a commit.

@Freedisch
Copy link
Contributor Author

Freedisch commented Mar 7, 2024

Commit messages describe what a commit does to a repository and not what you did in a commit.

Got it, 🙌🏾

@abompard
Copy link
Member

abompard commented Mar 7, 2024

One more thing: please allow the user to specify the location of the datagrepper instance (defaulting to apps.fedoraproject.org/datagrepper) because we may want to replay messages from the staging environment.

@Freedisch
Copy link
Contributor Author

@abompard, I see it makes sense, I'm on it

@gridhead gridhead requested a review from abompard March 7, 2024 13:02
@Freedisch
Copy link
Contributor Author

@abompard I have added the support to user to specify a custom URL

Copy link
Member

@abompard abompard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very good overall, thanks! A few comments though.

fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
@Freedisch Freedisch requested a review from abompard March 12, 2024 18:44
Copy link
Member

@abompard abompard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. Besides those fixes, it would be great if you could add unit tests for this new code.

fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved
fedora_messaging/cli.py Outdated Show resolved Hide resolved

@cli.command()
@click.argument("message_id")
@click.option("--datagrepper_url", help=_datagrepper_help, default=URL_TEMPLATE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah one minor thing: the convention is that option words are separated by dashes, not underscores. So it should be --datagrepper-url. Click will convert that to underscores for the variable name.

Copy link
Member

@abompard abompard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another minor thing. And please don't forget the unit tests! I can help with that if you're unfamiliar with unit testing.

response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()
except requests.HTTPError as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minor thing (again) but since you're pretty much putting the entire function inside a try/except block, I would just put that block in the calling function instead, something like:

def replay(message_id, datagrepper_url):
    try:
        message_data = _get_message(message_id, datagrepper_url)
    except requests.HTTPError as e:
        raise ...

Copy link
Contributor Author

@Freedisch Freedisch Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering what if I remove the try/except from the _get_message function and wrap all these operations in a single try/except block that catches and handles all exceptions? will this be much cleaner?

 try:
        message_data = _get_message(message_id, datagrepper_url)
        api.publish(message.load_message(message_data))
        click.echo(f"Message with ID {message_id} has been successfully replayed.")
 except requests.HTTPError:
        raise click.ClickException(f"Could not find message with ID {message_id}")
 except Exception as e:
        raise click.ClickException(f"Failed to replay message: {e}")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good question.
I'd prefer keeping the try/except blocks specific to what we want to handle, for example we know how to handle HTTPErrors for the _get_message() call, so we handle it, but we may not want to catch the generic exception there. So, multiple blocks seems cleaner to me.

@Freedisch
Copy link
Contributor Author

Another minor thing. And please don't forget the unit tests! I can help with that if you're unfamiliar with unit testing.

I'm familiar with unit tests, I will check the existing ones and follow the same workflow.

@Freedisch
Copy link
Contributor Author

Freedisch commented Mar 15, 2024

Test cases results:

Screenshot from 2024-03-15 20-50-05

Screenshot from 2024-03-15 20-49-03

@Freedisch Freedisch requested a review from abompard March 17, 2024 16:38
Copy link
Member

@abompard abompard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the last try/except change and it should be all good :-)

response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()
except requests.HTTPError as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good question.
I'd prefer keeping the try/except blocks specific to what we want to handle, for example we know how to handle HTTPErrors for the _get_message() call, so we handle it, but we may not want to catch the generic exception there. So, multiple blocks seems cleaner to me.

fedora_messaging/cli.py Show resolved Hide resolved
@abompard
Copy link
Member

OK great! Only the --datagrepper-url option name to fix! :-)

@Freedisch
Copy link
Contributor Author

OK great! Only the --datagrepper-url option name to fix! :-)

Actually, I thought I fixed it already 😅

Copy link
Member

@abompard abompard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for your work and your attention to detail.
I'll merge it and release a new version soon.

@abompard
Copy link
Member

Ah, yeah, the unit test needs to be adjusted as well.

@abompard
Copy link
Member

OK thanks! Could you rebase and solve the conflicts? If you have any issues doing that please ask.

@Freedisch
Copy link
Contributor Author

Freedisch commented Mar 19, 2024

OK thanks! Could you rebase and solve the conflicts? If you have any issues doing that please ask.

I was wondering also If I should squash all these commits in one?

@abompard
Copy link
Member

I was wondering also If I should squash all these commits in one?

Yeah that would be preferable.

Signed-off-by: freedisch <freeproduc@gmail.com>
@abompard abompard merged commit 5a621dd into fedora-infra:develop Mar 20, 2024
19 checks passed
@abompard
Copy link
Member

Merged, thanks again for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a replay command
3 participants