Repository: conan-io/conan

Issue:
[bug] conan_build_info --v2 does not work with scm
### Steps to reproduce
- Create a package that uses scm to get sources
- Try to create build info from that package

`conan_build_info --v2` will return an error because it can not find conan_sources.tgz in the repo


Discussion:
Fix it for the minor, typically `conan_sources.tgz` shouldn't be there.

Resolving patch:
diff --git a/conans/build_info/build_info.py b/conans/build_info/build_info.py
--- a/conans/build_info/build_info.py
+++ b/conans/build_info/build_info.py
@@ -46,13 +46,14 @@ def __init__(self, output, build_info_file, lockfile, multi_module=True, skip_en
         self._user = user
         self._password = password
         self._apikey = apikey
+        self._output = output
         self._conan_cache = ClientCache(os.path.join(get_conan_user_home(), ".conan"), output)
 
     def parse_pref(self, pref):
         ref = ConanFileReference.loads(pref, validate=False)
         rrev = ref.revision.split("#")[0].split(":")[0]
         pid = ref.revision.split("#")[0].split(":")[1]
-        prev = ref.revision.split("#")[1]
+        prev = "" if len(ref.revision.split("#")) == 1 else ref.revision.split("#")[1]
         return {
             "name": ref.name,
             "version": ref.version,
@@ -106,14 +107,9 @@ def _get_metadata_artifacts(self, metadata, request_path, use_id=False, name_for
 
             if response.status_code == 200:
                 data = response.json()
-                ret[data["checksums"]["sha1"]] = {"md5": data["checksums"],
-                                                  "name": "conan_sources.tgz",
-                                                  "id": None}
-            elif response.status_code == 401:
-                raise AuthenticationException(response_to_str(response))
-            else:
-                raise RequestErrorException(response_to_str(response))
-
+                ret[data["checksums"]["sha1"]] = {"md5": data["checksums"]["md5"],
+                                                  "name": "conan_sources.tgz" if not use_id else None,
+                                                  "id": "conan_sources.tgz" if use_id else None}
         return set([Artifact(k, **v) for k, v in ret.items()])
 
     def _get_recipe_artifacts(self, pref, add_prefix, use_id):


Test patch:
diff --git a/conans/test/integration/test_build_info_creation.py b/conans/test/integration/test_build_info_creation.py
--- a/conans/test/integration/test_build_info_creation.py
+++ b/conans/test/integration/test_build_info_creation.py
@@ -55,6 +55,13 @@ def mock_response(url, data=None, **kwargs):
         mock_resp.content = None
         return mock_resp
 
+    def mock_response_get(url, data=None, **kwargs):
+        mock_resp = Mock()
+        mock_resp.status_code = 200
+        if "conan_sources.tgz" in url:
+            mock_resp.status_code = 404
+        return mock_resp
+
     def _test_buildinfo(self, client, user_channel):
         conanfile = textwrap.dedent("""
             from conans import ConanFile, load
@@ -171,3 +178,57 @@ def test_build_info_create_update_publish(self, mock_cache, user_home_mock):
         user_channels = ["", "user/channel"]
         for user_channel in user_channels:
             self._test_buildinfo(client, user_channel)
+
+    @patch("conans.build_info.build_info.get_conan_user_home")
+    @patch("conans.build_info.build_info.ClientCache")
+    @patch("conans.build_info.build_info.requests.get", new=mock_response_get)
+    def test_build_info_create_scm(self, mock_cache, user_home_mock):
+        base_folder = temp_folder(True)
+        cache_folder = os.path.join(base_folder, ".conan")
+        servers = {"default": TestServer([("*/*@*/*", "*")], [("*/*@*/*", "*")],
+                                         users={"lasote": "mypass"})}
+        client = TestClient(servers=servers, users={"default": [("lasote", "mypass")]},
+                            cache_folder=cache_folder)
+
+        mock_cache.return_value = client.cache
+        user_home_mock.return_value = base_folder
+        conanfile = textwrap.dedent("""
+            from conans import ConanFile, load
+            import os
+            class Pkg(ConanFile):
+                name = "PkgA"
+                version = "0.1"
+                scm = {"type": "git",
+                        "url": "auto",
+                        "revision": "auto"}
+            
+                def imports(self):
+                    self.copy("myfile.txt", folder=True)
+                def package(self):
+                    self.copy("*myfile.txt")
+                """)
+
+        client.save({"conanfile.py": conanfile,
+                     "myfile.txt": "HelloA"})
+        client.run_command("git init")
+        client.run_command('git config user.email "you@example.com"')
+        client.run_command('git config user.name "Your Name"')
+        client.run_command("git remote add origin https://github.com/fake/fake.git")
+        client.run_command("git add .")
+        client.run_command("git commit -m \"initial commit\"")
+
+        client.run("export .")
+
+        client.run("graph lock .")
+
+        client.run("create . --lockfile")
+        client.run("upload * --confirm -r default --force")
+
+        sys.argv = ["conan_build_info", "--v2", "start", "MyBuildName", "42"]
+        run()
+        sys.argv = ["conan_build_info", "--v2", "create",
+                    os.path.join(client.current_folder, "buildinfo.json"), "--lockfile",
+                    os.path.join(client.current_folder, LOCKFILE)]
+        run()
+        if not os.path.exists(os.path.join(client.current_folder, "buildinfo.json")):
+            self.fail("build info create failed")