Repository: packit/packit
Issue: Why is the RPM release not set to 1?

### What happened? What is the problem?

When a version was bumped from 1.0.2 to 1.0.3, the release was bumped from 4 to 5.

### What did you expect to happen?

The release should be 1.

### Example URL(s)

https://src.fedoraproject.org/rpms/python-noggin-messages/pull-request/6

### Steps to reproduce

```bash
No idea, see the link.
```


### Workaround

- [ ] There is an existing workaround that can be used until this issue is fixed.

### Participation

- [ ] I am willing to submit a pull request for this issue. (Packit team is happy to help!)

Changed files:
tests/unit/test_base_git.py, packit/base_git.py

diff --git a/packit/base_git.py b/packit/base_git.py
index cdd6166f..58dc076f 100644
--- a/packit/base_git.py
+++ b/packit/base_git.py
@@ -419,10 +419,15 @@ class PackitRepositoryBase:
         if version is not None:
             self.specfile.version = version
         if (
-            previous_version != self.specfile.expanded_version
+            # version in upstream spec doesn't match the desired version
+            # -> ignore release from upstream spec and reset it to 1
+            specfile.expanded_version != self.specfile.expanded_version
+            # previous version in dist-git spec doesn't match the desired version
+            # and previous release in dist-git spec matches release in upstream spec
+            # -> reset release to 1
+            or previous_version != self.specfile.expanded_version
             and previous_release == self.specfile.release
-            and self.specfile.release != "%autorelease"
-        ):
+        ) and self.specfile.release != "%autorelease":
             # This may occur if the upstream forgets to reset release after
             # bumping the version, or if the specfile is not maintained in
             # upstream at all (e.g. post-upstream-clone that uses wget to
diff --git a/tests/unit/test_base_git.py b/tests/unit/test_base_git.py
index 9c5ae469..9762c509 100644
--- a/tests/unit/test_base_git.py
+++ b/tests/unit/test_base_git.py
@@ -458,15 +458,23 @@ def test_set_spec_content(tmp_path):
 
 
 @pytest.mark.parametrize(
-    "dg_raw_release,up_raw_release,expected_dg_release",
+    "dg_raw_version,dg_raw_release,up_raw_version,up_raw_release,expected_dg_release",
     [
-        ("2%{?dist}", "2%{?dist}", "1"),
-        ("2%{?dist}", "3%{?dist}", "3"),
-        ("%autorelease", "%autorelease", "%autorelease"),
+        ("1.0", "2%{?dist}", "1.0", "2%{?dist}", "1"),
+        ("1.0", "2%{?dist}", "1.0", "3%{?dist}", "1"),
+        ("1.0", "2%{?dist}", "1.1", "2%{?dist}", "1"),
+        ("1.0", "2%{?dist}", "1.1", "3%{?dist}", "3"),
+        ("1.0", "%autorelease", "1.0", "%autorelease", "%autorelease"),
+        ("1.0", "%autorelease", "1.1", "%autorelease", "%autorelease"),
     ],
 )
 def test_set_spec_content_reset_release(
-    tmp_path, dg_raw_release, up_raw_release, expected_dg_release
+    tmp_path,
+    dg_raw_version,
+    dg_raw_release,
+    up_raw_version,
+    up_raw_release,
+    expected_dg_release,
 ):
     def changelog(release):
         # %autorelease implies %autochangelog
@@ -479,7 +487,7 @@ def test_set_spec_content_reset_release(
 
     distgit_spec_contents = (
         "Name: bring-me-to-the-life\\n"
-        "Version: 1.0\\n"
+        f"Version: {dg_raw_version}\\n"
         f"Release: {dg_raw_release}\\n"
         "Source0: foo.bar\\n"
         "License: GPLv3+\\n"
@@ -492,7 +500,7 @@ def test_set_spec_content_reset_release(
 
     upstream_spec_contents = (
         "Name: bring-me-to-the-life\\n"
-        "Version: 1.0\\n"
+        f"Version: {up_raw_version}\\n"
         f"Release: {up_raw_release}\\n"
         "Source0: foo.bor\\n"
         "License: MIT\\n"