Repository: packit/packit
Issue: packit uses wrong version in fix-spec-file step if PR bumps version

Example PRs:
- https://github.com/theforeman/foreman_maintain/pull/688
- https://github.com/theforeman/foreman_maintain/pull/686

Our `.packit.yml` contains the following `get-current-version` action:
```yaml
  actions:
    get-current-version:
      - ruby -rrubygems -e 'puts Gem::Specification::load(Dir.glob("*.gemspec").first).version'
```
This reads the actual version of the software from the gemspec and works just fine usually.

However, when we bump the version (as in the PRs linked above), the version is correctly detected by the action but seems not to be used *everywhere* in the code.

Looking at the SRPM [buildlog](https://download.copr.fedorainfracloud.org/results/packit/theforeman-foreman_maintain-686/srpm-builds/05557745/builder-live.log.gz) I see:
```
2023-02-22 16:28:26.729 base_git.py       INFO   Using user-defined script for ActionName.get_current_version: [['ruby', '-rrubygems', '-e', 'puts Gem::Specification::load(Dir.glob("*.gemspec").first).version']]
2023-02-22 16:28:26.729 commands.py       DEBUG  Command: ruby -rrubygems -e puts Gem::Specification::load(Dir.glob("*.gemspec").first).version
2023-02-22 16:28:26.792 logging.py        INFO   1.2.5
2023-02-22 16:28:26.794 upstream.py       DEBUG  Version: 1.2.5
2023-02-22 16:28:26.794 upstream.py       DEBUG  Sanitized version: 1.2.5
2023-02-22 16:28:26.794 upstream.py       DEBUG  Name + version = foreman_maintain-1.2.5
2023-02-22 16:28:26.795 base_git.py       INFO   Using user-defined script for ActionName.create_archive: [['gem', 'build', 'foreman_maintain.gemspec'], ['bash', '-c', 'ls -1t ./foreman_maintain-*.gem | head -n 1']]
2023-02-22 16:28:26.795 commands.py       DEBUG  Command: gem build foreman_maintain.gemspec
2023-02-22 16:28:27.076 logging.py        INFO     Successfully built RubyGem
2023-02-22 16:28:27.076 logging.py        INFO     Name: foreman_maintain
2023-02-22 16:28:27.076 logging.py        INFO     Version: 1.2.5
2023-02-22 16:28:27.077 logging.py        INFO     File: foreman_maintain-1.2.5.gem
2023-02-22 16:28:27.080 commands.py       DEBUG  Command: bash -c ls -1t ./foreman_maintain-*.gem | head -n 1
2023-02-22 16:28:27.082 logging.py        INFO   ./foreman_maintain-1.2.5.gem
2023-02-22 16:28:27.083 upstream.py       INFO   Created archive:
2023-02-22 16:28:27.083 upstream.py       INFO   	parsed   path: /tmp/tmpqz230sov/foreman_maintain-1.2.5.gem
2023-02-22 16:28:27.083 upstream.py       INFO   	absolute path: /tmp/tmpqz230sov/foreman_maintain-1.2.5.gem
2023-02-22 16:28:27.184 commands.py       DEBUG  Command: git describe --tags --long --match *
2023-02-22 16:28:27.188 logging.py        DEBUG  v1.2.4-7-g9da71c9
2023-02-22 16:28:27.250 commands.py       DEBUG  Command: git describe --tags --long --match *
2023-02-22 16:28:27.253 logging.py        DEBUG  v1.2.4-7-g9da71c9
2023-02-22 16:28:27.254 base_git.py       DEBUG  Running ActionName.fix_spec.
2023-02-22 16:28:27.254 base_git.py       DEBUG  Running default implementation for ActionName.fix_spec.
2023-02-22 16:28:27.606 upstream.py       INFO   Version in upstream registries is None.
2023-02-22 16:28:27.639 upstream.py       INFO   Version in spec file is '1.2.4'.
2023-02-22 16:28:27.640 upstream.py       INFO   Picking version '1.2.4' found in spec file.
2023-02-22 16:28:27.640 upstream.py       DEBUG  Trying to extract archive_root_dir from known archives
2023-02-22 16:28:27.641 upstream.py       DEBUG  Archive foreman_maintain-1.2.5.gem is tar.
2023-02-22 16:28:27.642 upstream.py       WARNING No directory found in archive foreman_maintain-1.2.5.gem.
2023-02-22 16:28:27.642 upstream.py       DEBUG  Using archive_root_dir_template config option. If not set it defaults to {{upstream_pkg_name}}-{{version}}. Check https://packit.dev/docs/configuration/#archive_root_dir_template for more details.
2023-02-22 16:28:27.642 upstream.py       DEBUG  archive_root_dir_template is set or defaults to if not set to: {upstream_pkg_name}-{version}
```

Given `Source0` is gem, not a "real" tarball with folders, `get_archive_root_dir_from_tar` returns `None` and so `get_archive_root_dir` continues to use `get_archive_root_dir_from_template` which then uses the version from the spec file (`1.2.4`) and not the one we just generated (`1.2.5`).

This results in a RPM spec having `Version: 1.2.5` (which is correct), but then later using `%setup -q -n  foreman_maintain-1.2.4` and that obviously fails :)

Our spec file (before fix-spec-file) uses `%{version}`, so having #1736 would avoid the issue (and so would using a custom `archive_root_dir_template`, maybe something like `%{gem_name}-%{?version}` so that the `{version}` part is not replaced), but that seems like a bandaid.

Changed files:
packit/upstream.py

diff --git a/packit/upstream.py b/packit/upstream.py
index 539f5a58..ecfcf0a6 100644
--- a/packit/upstream.py
+++ b/packit/upstream.py
@@ -243,11 +243,23 @@ class Upstream(PackitRepositoryBase):
         logger.info(f"Version in spec file is {version!r}.")
         return version
 
+    def get_version_from_action(self) -> str:
+        """provide version from action"""
+        action_output = self.get_output_from_action(
+            action=ActionName.get_current_version
+        )
+        version = action_output[-1].strip() if action_output else None
+        return version
+
     def get_version(self) -> str:
         """
-        Return version of the latest release available: prioritize bigger from upstream
+        Return version given by action, if any.
+        Or the latest release available: prioritize bigger from upstream
         package repositories or the version in spec
         """
+        if action_version := self.get_version_from_action():
+            return action_version
+
         ups_ver = self.get_latest_released_version()
         spec_ver = self.get_specfile_version()
 
@@ -271,12 +283,9 @@ class Upstream(PackitRepositoryBase):
             String containing version, e.g. `"0.1.1"`.
         """
         # Step 1
-        action_output = self.get_output_from_action(
-            action=ActionName.get_current_version
-        )
+        version = self.get_version_from_action()
 
         # Step 2
-        version = action_output[-1].strip() if action_output else None
         if version is None:
             version = self.get_version_from_tag(self.get_last_tag())