Repository: packit/packit
Issue: Patches in propose-downstream modified specfile are not propered numbered.

source git: https://github.com/cheese/dde-daemon/tree/fedora-5.12.52
packit version: packit-0.26.0-1.fc32

The generated specfile has:

```
# fix build with go 1.16 on Fedora                                                                                                 
# Author: Robin Lee <cheeselee@fedoraproject.org>                                                                                  
Patch0001: 0004-fix-build-with-go-1.16-on-Fedora.patch                                                                             
                                                                                                                                   
                                                                                                                                   
# feat(image_effect): run `dde-pixmix` under xvfb-run                                                                              
# Author: Robin Lee <cheeselee@fedoraproject.org>                                                                                  
Patch0001: 0003-feat-image_effect-run-dde-pixmix-under-xvfb-run.patch                                                              
                                                                                                                                   
                                                                                                                                   
# fix: rpm fix `deepin-api` path                                                                                                   
# Author: Robin Lee <cheeselee@fedoraproject.org>                                                                                  
Patch0001: 0002-fix-rpm-fix-deepin-api-path.patch                                                                                  
                                                                                                                                   
                                                                                                                                   
# feat: rpm recomments `lshw`                                                                                                      
# Author: Robin Lee <cheeselee@fedoraproject.org>                                                                                  
Patch0001: 0001-feat-rpm-recomments-lshw.patch  
```

All the patches are the same `Patch0001`.

Changed files:
tests/integration/test_source_git.py, packit/specfile.py

diff --git a/packit/specfile.py b/packit/specfile.py
index 63b6d5f5..1caaaec6 100644
--- a/packit/specfile.py
+++ b/packit/specfile.py
@@ -191,15 +191,13 @@ class Specfile(SpecFile):
            * If there are no existing patches, the patch is added after Source definitions
         """
         try:
-            patch_number_offset = max([x.index for x in self.get_applied_patches()])
+            patch_number_offset = max(x.index for x in self.get_applied_patches())
         except ValueError:
             logger.debug("There are no patches in the spec.")
             patch_number_offset = 0
 
         new_content = "\\n# " + "\\n# ".join(patch_metadata.specfile_comment.split("\\n"))
-        new_content += (
-            f"\\nPatch{(1 + patch_number_offset):04d}: {patch_metadata.name}\\n"
-        )
+        new_content += f"\\nPatch{(1 + patch_number_offset):04d}: {patch_metadata.name}"
 
         if self.get_applied_patches():
             last_source_tag_line = [
@@ -210,7 +208,16 @@ class Specfile(SpecFile):
                 t.line for t in self.tags.filter(name="Source*", valid=None)
             ][-1]
 
-        where = last_source_tag_line + 1
+        # Find first empty line after last_source_tag_line
+        for index, line in enumerate(
+            self.spec_content.section("%package")[last_source_tag_line:],
+            start=last_source_tag_line,
+        ):
+            if not line:
+                where = index
+                break
+        else:
+            where = len(self.spec_content.section("%package"))
 
         logger.debug(f"Adding patch {patch_metadata.name} to the spec file.")
         self.spec_content.section("%package")[where:where] = new_content.split("\\n")
diff --git a/tests/integration/test_source_git.py b/tests/integration/test_source_git.py
index 20da3f6c..f4e2c09a 100644
--- a/tests/integration/test_source_git.py
+++ b/tests/integration/test_source_git.py
@@ -132,11 +132,14 @@ def test_basic_local_update_patch_content(
 +Version:        0.1.0"""
         in git_diff
     )
-
-    # make sure the patches are placed after Source0
-    patches = """\\
+    # Make sure the patches are placed after Source0, but outside %if %endif
+    patches = (
+        """\\
 Source0:        %{upstream_name}-%{version}.tar.gz
-+
+ %endif
+ BuildArch:      noarch
+ """  # because linters would remove the trailing whitespace
+        + """
 +# switching to amarillo hops
 +# Author: Packit Test Suite <test@example.com>
 +Patch0001: 0001-switching-to-amarillo-hops.patch
@@ -149,8 +152,8 @@ Source0:        %{upstream_name}-%{version}.tar.gz
 +# Author: Packit Test Suite <test@example.com>
 +Patch0003: 0003-source-change.patch
 +
-+
 """
+    )
     assert patches in git_diff
 
     assert "Patch0004:" not in git_diff
@@ -312,7 +315,6 @@ def test_basic_local_update_patch_content_with_metadata(
 +# Few words for info.
 +Patch0003: testing.patch
 +
-+
 """
     assert patches in git_diff
 
@@ -361,7 +363,6 @@ def test_basic_local_update_patch_content_with_metadata_and_patch_ignored(
 +# Author: Packit Test Suite <test@example.com>
 +Patch0002: 0002-actually-let-s-do-citra.patch
 +
-+
 """
     assert patches in git_diff
 
@@ -403,7 +404,6 @@ def test_basic_local_update_patch_content_with_downstream_patch(
 +# Author: Packit Test Suite <test@example.com>
 +Patch0002: 0002-actually-let-s-do-citra.patch
 +
-+
 """
     assert patches in git_diff