From 67357e788376ff13c53b76e3d69197c022375cc1 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: May 13 2015 21:03:15 +0000
Subject: [PATCH 1/3] Be more careful/conservative when retrieving an user from a json blob


---

diff --git a/pagure/lib/git.py b/pagure/lib/git.py
index ebae4a9..f1931c8 100644
--- a/pagure/lib/git.py
+++ b/pagure/lib/git.py
@@ -276,10 +276,25 @@ def get_user_from_json(session, jsondata, key='user'):
     """
     user = None
 
-    username = jsondata.get(key, {}).get('name')
-    fullname = jsondata.get(key, {}).get('fullname')
-    useremails = jsondata.get(key, {}).get('emails')
-    default_email = jsondata.get(key, {}).get('default_email')
+    username = jsondata.get(key, None)
+    if username:
+        username = username.get('name')
+    fullname = jsondata.get(key, {})
+
+    if fullname:
+        fullname = fullname.get('fullname')
+
+    useremails = jsondata.get(key, {})
+    if useremails:
+        useremails = useremails.get('emails')
+
+    default_email = jsondata.get(key, {})
+    if default_email:
+        default_email = default_email.get('name')
+
+    if not default_email and useremails:
+        default_email = useremails[0]
+
     if not username and not useremails:
         return
 

From 824828c876fc36d54cefa882a78374fc8c9ebec1 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: May 13 2015 21:26:39 +0000
Subject: [PATCH 2/3] If the project isn't found but has a parent create a fork not a new project


---

diff --git a/pagure/lib/git.py b/pagure/lib/git.py
index f1931c8..866b24f 100644
--- a/pagure/lib/git.py
+++ b/pagure/lib/git.py
@@ -340,18 +340,29 @@ def get_project_from_json(
                 session, jsondata.get('parent'),
                 gitfolder, forkfolder, docfolder, ticketfolder, requestfolder)
 
-        pagure.lib.new_project(
-            session,
-            user=user.username,
-            name=name,
-            description=jsondata.get('description'),
-            parent_id=parent.id if parent else None,
-            blacklist=pagure.APP.config.get('BLACKLISTED_PROJECTS', []),
-            gitfolder=forkfolder if parent else gitfolder,
-            docfolder=docfolder,
-            ticketfolder=ticketfolder,
-            requestfolder=requestfolder,
-        )
+            pagure.lib.fork_project(
+                session=session,
+                repo=parent,
+                gitfolder=pagure.APP.config['GIT_FOLDER'],
+                forkfolder=pagure.APP.config['FORK_FOLDER'],
+                docfolder=pagure.APP.config['DOCS_FOLDER'],
+                ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
+                requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
+                user=user.username)
+
+        else:
+            pagure.lib.new_project(
+                session,
+                user=user.username,
+                name=name,
+                description=jsondata.get('description'),
+                parent_id=parent.id if parent else None,
+                blacklist=pagure.APP.config.get('BLACKLISTED_PROJECTS', []),
+                gitfolder=forkfolder if parent else gitfolder,
+                docfolder=docfolder,
+                ticketfolder=ticketfolder,
+                requestfolder=requestfolder,
+            )
 
         session.commit()
         project = pagure.lib.get_project(session, name, user=user.username)

From ae1e9312cacb5ccf80b6ab69891d1aea9a9dd6c9 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: May 14 2015 06:08:57 +0000
Subject: [PATCH 3/3] Simplify this code block and reduce redundancy


Thanks at @ralph @ralphbean

---

diff --git a/pagure/lib/git.py b/pagure/lib/git.py
index 866b24f..5e72af5 100644
--- a/pagure/lib/git.py
+++ b/pagure/lib/git.py
@@ -276,21 +276,15 @@ def get_user_from_json(session, jsondata, key='user'):
     """
     user = None
 
-    username = jsondata.get(key, None)
-    if username:
-        username = username.get('name')
-    fullname = jsondata.get(key, {})
+    username = fullname = useremails = default_email = None
 
-    if fullname:
-        fullname = fullname.get('fullname')
+    data = json.data.get(key, None)
 
-    useremails = jsondata.get(key, {})
-    if useremails:
-        useremails = useremails.get('emails')
-
-    default_email = jsondata.get(key, {})
-    if default_email:
-        default_email = default_email.get('name')
+    if data:
+        username = data.get('name')
+        fullname = data.get('fullname')
+        useremails = data.get('emails')
+        default_email = data.get('default_email')
 
     if not default_email and useremails:
         default_email = useremails[0]

