Repository: pycontribs/jira

Issue:
add_simple_link fails to complete 
I have this code:

```
newissue = jira.create_issue(fields=issue_dict)
            link = {'url': bug.weburl, 'title': 'Eclipse: " + bug.summary'}
            jira.add_simple_link(newissue,
                                 object=link)
```

it results in this error:

```
Traceback (most recent call last):
  File "bzira.py", line 119, in <module>
    object=link)
  File "/usr/local/lib/python2.7/site-packages/jira/client.py", line 1014, in add_simple_link
    url, data=json.dumps(data))
  File "/usr/local/lib/python2.7/site-packages/jira/resilientsession.py", line 81, in post
    return self.__verb('POST', url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/jira/resilientsession.py", line 74, in __verb
    raise_on_error(r, verb=verb, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/jira/utils.py", line 120, in raise_on_error
    r.status_code, error, r.url, request=request, response=r, **kwargs)
jira.utils.JIRAError: JiraError HTTP 400
    text: 'URL' is required., 'Link Text' is required.
    url: https://issues.stage.jboss.org/rest/api/2/issue/ERT-77/remotelink
    response headers = {'X-AUSERNAME': 'jiralint', 'X-ASEN': 'SEN-1095081', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'X-Seraph-LoginReason': 'OK', 'Vary': 'User-Agent', 'X-AREQUESTID': '254x195823x1', 'X-ASESSIONID': '1iq4ohz', 'Connection': 'close', 'Cache-Control': 'no-cache, no-store, no-transform', 'Date': 'Sun, 20 Dec 2015 09:14:47 GMT', 'Server': 'Apache-Coyote/1.1', 'Content-Type': 'application/json;charset=UTF-8'}
    response text = {"errorMessages":[],"errors":{"title":"'Link Text' is required.","url":"'URL' is required."}}
```

Might be related to https://github.com/pycontribs/jira/issues/156 
## <bountysource-plugin>

Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/29279237-add_simple_link-fails-to-complete?utm_campaign=plugin&utm_content=tracker%2F3650997&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F3650997&utm_medium=issues&utm_source=github).
</bountysource-plugin>



Discussion:
You should first check whether bug still exists in the newest version.

To upgrade, you can for example use pip. Check the documentation for details.

pip fails to upgrade which is why I linked to https://github.com/pycontribs/jira/issues/156

Hey @maxandersen, the error returned from the API suggests that the title and URL passed in for the remote link aren't valid. Have you tried debugging to see what `bug.weburl` and `bug.summary` might be set to, at the point where this error occurs?

After forcefully install the latest version (see #156) I ran this again and got the same error.

Here are the contents from python -i:

```
>>> print bug.weburl
https://bugs.eclipse.org/bugs/show_bug.cgi?id=473559
>>> print bug.summary
[language support] Add support for ECMAScript 2015
```

I'm wondering if the []'s needs quoting. I'll experiment a bit ;)

Seems if just plain text it fails too. Here is what the simplest 'link' fails with:

```
    response text = {"errorMessages":[],"errors":{"title":"'Link Text' is required.","url":"'URL' is required."}}

>>> print link
{'url': 'https://bugs.eclipse.org/bugs/show_bug.cgi?id=477031', 'title': 'Eclipse bug'}
```

Okey, I figured out the issue.

add_simple_link is not sending:

```
{ 
    "object": { 
        "url":"http://www.mycompany.com/support?id=1", 
        "title":"Crazy customer support issue"
    } 
}
```

it is only sending:

```
{ 
        "url":"http://www.mycompany.com/support?id=1", 
        "title":"Crazy customer support issue"
}
```

Causing the error.

Workaround for me at this point is to do this:

```
newissue = jira.create_issue(fields=issue_dict)
            link = {"object": {'url': bug.weburl, 'title': 'Eclipse: " + bug.summary'}}
            jira.add_simple_link(newissue,
                                 object=link)
```

Notice the nested level. 

But this seems counter to what is done elsewhere in the api. 

Nice one. Actually, looking at the docs from Atlassian, I think that might be the right thing to do:
- https://docs.atlassian.com/jira/REST/ondemand/#api/2/issue-createOrUpdateRemoteIssueLink
- https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/jira-rest-api-for-remote-issue-links

To be honest though, I think `add_simple_link()` could do with a bit of cleanup to bring it in line with the behaviour of `add_remote_link()`. There is some odd code in there, like the double initialisation of the `data` variable.


Resolving patch:
diff --git a/jira/client.py b/jira/client.py
--- a/jira/client.py
+++ b/jira/client.py
@@ -1108,11 +1108,7 @@ def add_simple_link(self, issue, object):
         :param object: the dictionary used to create remotelink data
         """
 
-        data = {}
-
-        # hard code data dict to be passed as ``object`` to avoid any permissions errors
-        data = object
-
+        data = {"object": object}
         url = self._get_url('issue/' + str(issue) + '/remotelink')
         r = self._session.post(
             url, data=json.dumps(data))


Test patch: