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

SAM Publish add flag to fail on duplicate semantic version #6470

Merged
merged 10 commits into from
Jan 4, 2024

Conversation

bentvelj
Copy link
Contributor

@bentvelj bentvelj commented Dec 18, 2023

Which issue(s) does this change fix?

#6301

Why is this change necessary?

SAM Publish will succeed in updating a SAR application, even if the semantic version already exists.

This can cause issues for deployment, when CloudFormation will not recognize the updates since the semantic version has not changed.

How does it address the issue?

  1. Add a warning message for customers if a semantic version already exists for their published changes
  2. Add a flag, --fail-on-same-version that hard fails (instead of the warning) if a customer attempts to publish changes to an already existing semantic version.

What side effects does this change have?

N/A

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Add input/output type hints to new functions/methods
  • Write design document if needed (Do I need to write a design document?)
  • Write/update unit tests
  • Write/update integration tests
  • Write/update functional tests if needed
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@github-actions github-actions bot added area/publish sam publish command area/schema JSON schema file pr/internal labels Dec 18, 2023
Comment on lines +9 to +12
def __init__(self, message=None, **kwargs):
"""Init the exception object."""
Exception.__init__(self, self.MESSAGE.format(**kwargs))
message = self.MESSAGE.format(**kwargs) if message is None else message
Exception.__init__(self, message)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to allow publish exceptions to be created with dynamic messages instead of hardcoded in the class

Jared Bentvelsen added 2 commits December 18, 2023 17:47
@bentvelj bentvelj marked this pull request as ready for review December 18, 2023 23:24
@bentvelj bentvelj requested a review from a team as a code owner December 18, 2023 23:24
@bentvelj bentvelj requested review from lucashuy and hnnasit December 18, 2023 23:24
sar_client.get_application(ApplicationId=application_id, SemanticVersion=semantic_version)
return True
except ClientError as error:
if error.response["Error"]["Code"] == "NotFoundException":
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we consider using .get() here instead of indexing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I just kept this consistent with what was already written. I don't think it's necessary given the set structure of the ClientError exception. We also see indexing done in the official ClientError docs

Comment on lines 118 to 131
def _check_app_with_semantic_version_exists(sar_client, application_id, semantic_version):
"""
Checks if a given SAR application exists with a given semantic version

:param sar_client: The boto3 client used to access SAR
:type sar_client: boto3.client
:param application_id: Application Id to check
:type sar_client: str
:param semantic_version: The semantic version to check with Application Id
:type sar_client: str
:return: Whether or not the given Application exists with the given semantic version
:rtype: bool
:raises ClientError
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: bool return type for the function signature, also looks like some of the types are duplicated.

As an aside, could we also update the type hints for this entire file since we are here to follow the the docstrings we've been using lately (ie Parameters/Returns/Raises)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah my bad, copy paste errors.

Rewriting the typehints shouldn't take too long, and I think it's worth it. Honestly that format is atrocious.

Comment on lines +69 to +71
"--fail-on-same-version is set, but no semantic version is specified.\n"
"Please provide a semantic version in either the "
"template metadata or with the --semantic-version option."
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the --fail option is SAM CLI specific, should move parts or all of this message over to SAM CLI instead of the vendor lib? Don't have a particularly strong preference to either

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think if the vendor lib is a part of sam cli now, and it's not going anywhere, this should be fine here?

if application_exists:
raise DuplicateSemanticVersionError(
f"Cannot publish version {semantic_version} for application "
"{application_id} because it already exists"
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing the leading f for the f-string here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops

@@ -222,6 +225,10 @@ def test_publish_existing_application_should_update_application_if_version_exist
self.serverlessrepo_mock.create_application.assert_called_once()
self.serverlessrepo_mock.update_application.assert_called_once()
self.serverlessrepo_mock.create_application_version.assert_called_once()
# If --fail-on-same-version is specified, warning is printed
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the inverse right? If its not specified, we should expect to see a warning, but if we wanted to test that it publishes anyways, should we instead test if the _check_app_with_semantic_version_exists() method was not called, instead of testing log output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my comment is wrong here. Should be # If --fail-on-same-version is NOT specified, warning is printed

I think we can keep the check for the log output, since the warning is important, but we can also check for _check_app_with_semantic_version_exists is not called

@bentvelj bentvelj requested review from mildaniel and removed request for hnnasit December 19, 2023 19:39
@bentvelj bentvelj requested a review from lucashuy December 20, 2023 21:13
Copy link
Contributor

@hawflau hawflau left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@jysheng123 jysheng123 left a comment

Choose a reason for hiding this comment

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

LGTM!

@bentvelj bentvelj enabled auto-merge January 4, 2024 18:47
@bentvelj bentvelj added this pull request to the merge queue Jan 4, 2024
Merged via the queue into aws:develop with commit 5af0918 Jan 4, 2024
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/publish sam publish command area/schema JSON schema file pr/internal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants