Repository: packit/packit
Issue: The `propose-downstream` seems to not execute `post-upstream-clone` action

Hi everyone. It seems that the `packit propose-downstream <project_url>` doesn't execute the `post-upstream-clone` action. We have this action on Anaconda [.packit.yml](https://github.com/rhinstaller/anaconda/blob/master/.packit.yml#L5) to generate `anaconda.spec` from `anaconda.spec.in`, however, it seems that this action is not executed because we are getting fail that `anaconda.spec` doesn't exists.

Steps to reproduce:
1. Call `packit -d propose-downstream https://github.com/rhinstaller/anaconda`.

```
$ packit -d propose-downstream https://github.com/rhinstaller/anaconda

2021-11-10 17:22:27.868 config.py         DEBUG  Loading user config from directory: /var/home/jkonecny/.config
2021-11-10 17:22:27.868 config.py         DEBUG  Trying to load user config from: /var/home/jkonecny/.config/.distro/source-git.yaml
2021-11-10 17:22:27.868 config.py         DEBUG  Trying to load user config from: /var/home/jkonecny/.config/.packit.yaml
2021-11-10 17:22:27.869 config.py         DEBUG  Trying to load user config from: /var/home/jkonecny/.config/.packit.yml
2021-11-10 17:22:27.869 config.py         DEBUG  Trying to load user config from: /var/home/jkonecny/.config/.packit.json
2021-11-10 17:22:27.869 config.py         DEBUG  Trying to load user config from: /var/home/jkonecny/.config/packit.yaml
2021-11-10 17:22:27.872 config.py         DEBUG  Loaded config: Config(debug='False', fas_user='None', keytab_path='None', kerberos_realm='FEDORAPROJECT.ORG', koji_build_command='koji build', pkg_tool='fedpkg', upstream_git_remote='None', command_handler='RunCommandType.local', command_handler_work_dir='/sandcastle', command_handler_pvc_env_var='SANDCASTLE_PVC', command_handler_image_reference='docker.io/usercont/sandcastle', command_handler_k8s_namespace='myproject', command_handler_pvc_volume_specs='[]', repository_cache='None')
2021-11-10 17:22:27.872 logging.py        DEBUG  Logging set to DEBUG
2021-11-10 17:22:27.873 packit_base.py    DEBUG  Packit 0.40.0 is being used.
2021-11-10 17:22:27.873 repo.py           DEBUG  Parsing git remote URL 'https://github.com/rhinstaller/anaconda' and converting it to https-like URL.
2021-11-10 17:22:27.873 repo.py           DEBUG  Provided input 'https://github.com/rhinstaller/anaconda' is an url.
2021-11-10 17:22:27.873 types.py          DEBUG  Input is a URL to a git repo: https://github.com/rhinstaller/anaconda
2021-11-10 17:22:27.873 local_project.py  DEBUG  Arguments received in the init method of the LocalProject class: 
git_repo: None
working_dir: None
ref: None
git_project: None
git_service: None
git_url: https://github.com/rhinstaller/anaconda
full_name: 
namespace: 
repo_name: 
offline: False
refresh True
remote: None
pr_id: None
cache: None

2021-11-10 17:22:27.873 repo.py           DEBUG  Cloning repo https://github.com/rhinstaller/anaconda -> /tmp/tmppxgxbrkj
2021-11-10 17:23:02.306 local_project.py  DEBUG  Parsed repo <git.repo.base.Repo '/tmp/tmppxgxbrkj/.git'> from url 'https://github.com/rhinstaller/anaconda'.
2021-11-10 17:23:02.306 local_project.py  DEBUG  Parsed ref 'master' from the repo <git.repo.base.Repo '/tmp/tmppxgxbrkj/.git'>.
2021-11-10 17:23:02.306 local_project.py  DEBUG  Parsed working directory /tmp/tmppxgxbrkj from the repo <git.repo.base.Repo '/tmp/tmppxgxbrkj/.git'>.
2021-11-10 17:23:02.306 local_project.py  DEBUG  Parsed namespace and repo name (rhinstaller, anaconda) from url 'https://github.com/rhinstaller/anaconda'.
2021-11-10 17:23:02.306 local_project.py  DEBUG  Parsed full repo name 'rhinstaller/anaconda'.
2021-11-10 17:23:02.306 package_config.py DEBUG  Local package config found: /tmp/tmppxgxbrkj/.packit.yml
2021-11-10 17:23:02.313 package_config.py DEBUG  Package config:
{
    "specfile_path": "anaconda.spec",
    "upstream_package_name": "anaconda",
    "upstream_tag_template": "anaconda-{version}-1",
    "actions": {
        "post-upstream-clone": [
            "./autogen.sh",
            "./configure"
        ],
        "create-archive": [
            "make release",
            "bash -c \\"ls -1 anaconda-*.tar.bz2\\""
        ]
    },
    "jobs": [
        {
            "job": "tests",
            "trigger": "pull_request",
            "metadata": {
                "targets": [
                    "fedora-rawhide"
                ]
            }
        },
        {
            "job": "copr_build",
            "trigger": "pull_request",
            "metadata": {
                "targets": [
                    "fedora-rawhide",
                    "fedora-eln"
                ]
            }
        },
        {
            "job": "copr_build",
            "trigger": "commit",
            "metadata": {
                "targets": [
                    "fedora-rawhide",
                    "fedora-eln"
                ],
                "branch": "master",
                "owner": "@rhinstaller",
                "project": "Anaconda",
                "preserve_project": true
            }
        },
        {
            "job": "copr_build",
            "trigger": "commit",
            "metadata": {
                "targets": [
                    "fedora-latest"
                ],
                "branch": "f35-devel",
                "owner": "@rhinstaller",
                "project": "Anaconda-devel",
                "preserve_project": true,
                "additional_repos": [
                    "copr://@storage/blivet-daily"
                ]
            }
        }
    ]
}
2021-11-10 17:23:02.317 utils.py          DEBUG  Input directory is an upstream repository.
2021-11-10 17:23:02.317 api.py            DEBUG  Won't be doing kinit, no credentials provided.
2021-11-10 17:23:02.322 commands.py       DEBUG  Command: fedpkg -q clone anaconda /tmp/packit-dist-gitd3u4_x8j
2021-11-10 17:23:11.522 local_project.py  DEBUG  Arguments received in the init method of the LocalProject class: 
git_repo: None
working_dir: /tmp/packit-dist-gitd3u4_x8j
ref: 
git_project: PagureProject(namespace="rpms", repo="anaconda")
git_service: None
git_url: https://src.fedoraproject.org/rpms/anaconda.git
full_name: 
namespace: rpms
repo_name: anaconda
offline: False
refresh True
remote: 
pr_id: None
cache: None

2021-11-10 17:23:11.522 local_project.py  DEBUG  Parsed full repo name 'rpms/anaconda'.
2021-11-10 17:23:11.522 local_project.py  DEBUG  `working_dir` is set and `git_repo` is not: let's discover...
2021-11-10 17:23:11.522 local_project.py  DEBUG  It's a git repo!
2021-11-10 17:23:11.523 local_project.py  DEBUG  Parsed service PagureService(instance_url='https://src.fedoraproject.org', token='5***U') from the project PagureProject(namespace="rpms", repo="anaconda").
2021-11-10 17:23:11.523 local_project.py  DEBUG  Parsed ref 'rawhide' from the repo <git.repo.base.Repo '/tmp/packit-dist-gitd3u4_x8j/.git'>.
Proposing update of the following branches: rawhide
2021-11-10 17:23:13.084 __init__.py       INFO   Running 'anitya' versioneer
2021-11-10 17:23:14.004 upstream.py       INFO   Version in upstream registries is '36.8-1'.
2021-11-10 17:23:14.005 repo.py           DEBUG  Parsing git remote URL 'https://github.com/rhinstaller/anaconda' and converting it to https-like URL.
2021-11-10 17:23:14.005 repo.py           DEBUG  Provided input 'https://github.com/rhinstaller/anaconda' is an url.
2021-11-10 17:23:14.005 utils.py          ERROR  Specfile /tmp/tmppxgxbrkj/anaconda.spec not found.
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/packit/cli/utils.py", line 64, in covered_func
    func(config=config, *args, **kwargs)
  File "/usr/lib/python3.10/site-packages/packit/cli/propose_downstream.py", line 142, in propose_downstream
    api.sync_release(
  File "/usr/lib/python3.10/site-packages/packit/api.py", line 262, in sync_release
    version = version or self.up.get_version()
  File "/usr/lib/python3.10/site-packages/packit/upstream.py", line 242, in get_version
    spec_ver = self.get_specfile_version()
  File "/usr/lib/python3.10/site-packages/packit/upstream.py", line 232, in get_specfile_version
    version = self.specfile.get_version()
  File "/usr/lib/python3.10/site-packages/packit/base_git.py", line 102, in specfile
    self.absolute_specfile_path, self.absolute_source_dir
  File "/usr/lib/python3.10/site-packages/packit/base_git.py", line 94, in absolute_specfile_path
    raise FileNotFoundError(f"Specfile {self._specfile_path} not found.")
FileNotFoundError: Specfile /tmp/tmppxgxbrkj/anaconda.spec not found.
2021-11-10 17:23:14.009 local_project.py  DEBUG  Cleaning: /tmp/packit-dist-gitd3u4_x8j
2021-11-10 17:23:14.013 local_project.py  DEBUG  Cleaning: /tmp/tmppxgxbrkj

```

Changed files:
tests/integration/test_update.py, packit/api.py

diff --git a/packit/api.py b/packit/api.py
index 851a8360..a6327d06 100644
--- a/packit/api.py
+++ b/packit/api.py
@@ -250,6 +250,7 @@ class PackitAPI:
 
         :return created PullRequest if create_pr is True, else None
         """
+        self.up.run_action(actions=ActionName.post_upstream_clone)
         dist_git_branch = (
             dist_git_branch or self.dg.local_project.git_project.default_branch
         )
@@ -287,7 +288,6 @@ class PackitAPI:
             upstream_ref or self.package_config.upstream_ref
         )
         create_pr = create_pr and self.package_config.create_pr
-        self.up.run_action(actions=ActionName.post_upstream_clone)
 
         current_up_branch = self.up.active_branch
         try:
diff --git a/tests/integration/test_update.py b/tests/integration/test_update.py
index e4053f07..a636957f 100644
--- a/tests/integration/test_update.py
+++ b/tests/integration/test_update.py
@@ -1,12 +1,14 @@
 # Copyright Contributors to the Packit project.
 # SPDX-License-Identifier: MIT
 
+import shutil
 import subprocess
 from pathlib import Path
 
 import pytest
 from flexmock import flexmock
 
+from packit.actions import ActionName
 from packit.api import PackitAPI, Config
 from packit.config import parse_loaded_config
 from packit.local_project import LocalProject
@@ -57,6 +59,35 @@ def test_basic_local_update(
     assert "0.1.0" in changelog
 
 
+def test_local_update_generated_spec(
+    cwd_upstream, api_instance, mock_remote_functionality_upstream
+):
+    """Check that specfile can be generated on clone."""
+    u, d, api = api_instance
+    mock_spec_download_remote_s(d)
+    flexmock(api).should_receive("init_kerberos_ticket").at_least().once()
+
+    # Simulate generation by moving the spec to a different location
+    current_spec_location = u / "beer.spec"
+    new_spec_location = u / "tmp.spec"
+    shutil.move(current_spec_location, new_spec_location)
+    api.up.package_config.actions = {
+        ActionName.post_upstream_clone: [
+            f"mv {new_spec_location} {current_spec_location}"
+        ]
+    }
+    api.sync_release(dist_git_branch="main")
+
+    assert (d / TARBALL_NAME).is_file()
+    spec = Specfile(d / "beer.spec")
+    assert spec.get_version() == "0.1.0"
+    assert (d / "README.packit").is_file()
+    # assert that we have changelog entries for both versions
+    changelog = "\\n".join(spec.spec_content.section("%changelog"))
+    assert "0.0.0" in changelog
+    assert "0.1.0" in changelog
+
+
 def test_basic_local_update_reset_after_exception(
     cwd_upstream, api_instance, mock_remote_functionality_upstream
 ):