Re: Updating a "patch" state via REST

2018-10-10 Thread Markus Mayer
On Wed, 10 Oct 2018 at 00:33, Stewart Smith  wrote:
>
> Markus Mayer  writes:
> > I am working on a script that will, amongst other things, update the
> > state ("Accepted", "Rejected", etc.) of patches in our own Patchwork
> > installation. The script is using the REST API. All requests in the
> > script, so far, are GET requests. They work fine.
> >
> > Now, I want to issue a PUT request to update the patch state, also
> > using the REST API. However, no matter what I try, the request gets
> > rejected by the server.
>
> I think you want the PATCH request rather than PUT?
>
> https://github.com/stewart-ibm/pwnm-sync is my script I use to update
> patch status (from notmuch mail tags) and that seems to be the request
> type that works for me (with patchwork.ozlabs.org)

Indeed. That works. Thanks a lot!

Regards,
-Markus
___
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork


Re: Updating a "patch" state via REST

2018-10-10 Thread Stephen Finucane
On Wed, 2018-10-10 at 18:33 +1100, Stewart Smith wrote:
> Markus Mayer  writes:
> > I am working on a script that will, amongst other things, update the
> > state ("Accepted", "Rejected", etc.) of patches in our own Patchwork
> > installation. The script is using the REST API. All requests in the
> > script, so far, are GET requests. They work fine.
> > 
> > Now, I want to issue a PUT request to update the patch state, also
> > using the REST API. However, no matter what I try, the request gets
> > rejected by the server.
> 
> I think you want the PATCH request rather than PUT?
> 
> https://github.com/stewart-ibm/pwnm-sync is my script I use to update
> patch status (from notmuch mail tags) and that seems to be the request
> type that works for me (with patchwork.ozlabs.org)

Yeah, you want PATCH. PUT requires an entirely new object (like POST)
which is used to overwrite the existing object. PATCH just needs the
updated fields. The examples used in 'git-pw' does just this [1][2][3]
(though I realize the function is confusing called 'put' - will
update).

Stephen

PS: As an aside, there's a rather good article [4] on all things
POST/PUT...which we (well, Django REST Framework) do the exact opposite
of :) Worth a read though

[1] https://github.com/getpatchwork/git-pw/blob/1.4.0/git_pw/patch.py#L175
[2] https://github.com/getpatchwork/git-pw/blob/1.4.0/git_pw/api.py#L273
[3] https://github.com/getpatchwork/git-pw/blob/1.4.0/git_pw/api.py#L156-L170
[4] https://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/

___
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork


Re: Updating a "patch" state via REST

2018-10-10 Thread Stewart Smith
Markus Mayer  writes:
> I am working on a script that will, amongst other things, update the
> state ("Accepted", "Rejected", etc.) of patches in our own Patchwork
> installation. The script is using the REST API. All requests in the
> script, so far, are GET requests. They work fine.
>
> Now, I want to issue a PUT request to update the patch state, also
> using the REST API. However, no matter what I try, the request gets
> rejected by the server.

I think you want the PATCH request rather than PUT?

https://github.com/stewart-ibm/pwnm-sync is my script I use to update
patch status (from notmuch mail tags) and that seems to be the request
type that works for me (with patchwork.ozlabs.org)

-- 
Stewart Smith
OPAL Architect, IBM.

___
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork


Re: Updating a "patch" state via REST

2018-10-09 Thread Markus Mayer
On Tue, 9 Oct 2018 at 15:33, Markus Mayer  wrote:
>
> Hi,
>
> I am working on a script that will, amongst other things, update the
> state ("Accepted", "Rejected", etc.) of patches in our own Patchwork
> installation. The script is using the REST API. All requests in the
> script, so far, are GET requests. They work fine.
>
> Now, I want to issue a PUT request to update the patch state, also
> using the REST API. However, no matter what I try, the request gets
> rejected by the server.
>
> I have been experimenting with a docker installation of Patchwork.
> This is not the live instance:
>
> $ curl -S -i 'http://localhost:8000/api/1.0/patches/1/'
> HTTP/1.0 200 OK
> Date: Tue, 09 Oct 2018 22:23:56 GMT
> Server: WSGIServer/0.2 CPython/3.6.6
> Content-Type: application/json
> Vary: Accept, Cookie
> Allow: GET, PUT, PATCH, HEAD, OPTIONS
> X-Frame-Options: SAMEORIGIN
>
> {"id":1,"url":"http://localhost:8000/api/1.0/patches/1/","project":{"id":2,"url":"http://localhost:8000/api/1.0/projects/2/","name":"Test","link_name":"Test","list_id":"Test","list_email":"...","web_url":"","scm_url":"","webscm_url":""}
> [...]
>
> Okay, so patch 1 exists and we can retrieve information about it.
>
> First attempt to update the state:
>
> $ curl -i -X PUT -H 'Content-Type: application/json' -d
> '{"state":"Rejected"}'
> 'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
> HTTP/1.0 400 Bad Request
> Date: Tue, 09 Oct 2018 22:22:58 GMT
> Server: WSGIServer/0.2 CPython/3.6.6
> Content-Type: application/json
> Vary: Accept, Cookie
> Allow: GET, PUT, PATCH, HEAD, OPTIONS
> X-Frame-Options: SAMEORIGIN
>
> {"delegate":["This field is required."]}
>
> Okay, so it wants the delegate field to be present. Not sure why,
> since I don't really want to set the delegate yet, but okay.
>
> Second attempt:
>
> $ curl -i -X PUT -H 'Content-Type: application/json' -d
> '{"delegate":1, "state":"Rejected"}'
> 'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
> HTTP/1.0 400 Bad Request
> Date: Tue, 09 Oct 2018 22:25:43 GMT
> Server: WSGIServer/0.2 CPython/3.6.6
> Content-Type: application/json
> Vary: Accept, Cookie
> Allow: GET, PUT, PATCH, HEAD, OPTIONS
> X-Frame-Options: SAMEORIGIN
>
> {"delegate":{"non_field_errors":["Invalid data. Expected a dictionary,
> but got int."]}}
>
> Okay, so delegate needs to be a dictionary.
>
> Third attempt:
>
> $ curl -i -X PUT -H 'Content-Type: application/json' -d '{"delegate" :
> {"id" : 1},"state":"Rejected"}'
> 'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
> HTTP/1.0 500 Internal Server Error
> Date: Tue, 09 Oct 2018 22:26:44 GMT
> Server: WSGIServer/0.2 CPython/3.6.6
> Content-Type: text/plain
> X-Frame-Options: SAMEORIGIN
> Vary: Cookie
>
> AssertionError at /api/1.0/patches/1/
> The `.update()` method does not support writable nested fields by default.
> Write an explicit `.update()` method for serializer
> `patchwork.api.patch.PatchDetailSerializer`, or set `read_only=True`
> on nested serializer fields.
>
> Request Method: PUT
> Request URL: http://localhost:8000/api/1.0/patches/1/
> Django Version: 1.10.8
> Python Executable: /usr/bin/python3
> Python Version: 3.6.6
> Python Path: ['/home/patchwork/patchwork', '/usr/lib/python36.zip',
> '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',
> '/usr/local/lib/python3.6/dist-packages',
> '/usr/lib/python3/dist-packages']
> Server time: Tue, 9 Oct 2018 22:26:44 +
>
> So, does that mean that Patchwork doesn't currently support this type
> of functionality using the REST API? The error message seems to imply
> that code changes to the Patchwork core are needed to make this work.
>
> Can you provide me with some pointers where to go from here?

It starts to work, if I do this:

$ git diff
diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
index 1f0ead1f799a..f032119f17bf 100644
--- a/patchwork/api/patch.py
+++ b/patchwork/api/patch.py
@@ -81,7 +81,7 @@ class PatchListSerializer(HyperlinkedModelSerializer):
 project = ProjectSerializer(read_only=True)
 state = StateField()
 submitter = PersonSerializer(read_only=True)
-delegate = UserSerializer()
+delegate = UserSerializer(read_only=True)
 mbox = SerializerMethodField()
 series = SeriesSerializer(many=True, read_only=True)
 check = SerializerMethodField()

While a minor change, it is still a code change to the Patchwork core.
Is this an acceptable solution? If so, I can send it as a proper
patch.

Regards,
-Markus
___
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork


Updating a "patch" state via REST

2018-10-09 Thread Markus Mayer
Hi,

I am working on a script that will, amongst other things, update the
state ("Accepted", "Rejected", etc.) of patches in our own Patchwork
installation. The script is using the REST API. All requests in the
script, so far, are GET requests. They work fine.

Now, I want to issue a PUT request to update the patch state, also
using the REST API. However, no matter what I try, the request gets
rejected by the server.

I have been experimenting with a docker installation of Patchwork.
This is not the live instance:

$ curl -S -i 'http://localhost:8000/api/1.0/patches/1/'
HTTP/1.0 200 OK
Date: Tue, 09 Oct 2018 22:23:56 GMT
Server: WSGIServer/0.2 CPython/3.6.6
Content-Type: application/json
Vary: Accept, Cookie
Allow: GET, PUT, PATCH, HEAD, OPTIONS
X-Frame-Options: SAMEORIGIN

{"id":1,"url":"http://localhost:8000/api/1.0/patches/1/","project":{"id":2,"url":"http://localhost:8000/api/1.0/projects/2/","name":"Test","link_name":"Test","list_id":"Test","list_email":"...","web_url":"","scm_url":"","webscm_url":""}
[...]

Okay, so patch 1 exists and we can retrieve information about it.

First attempt to update the state:

$ curl -i -X PUT -H 'Content-Type: application/json' -d
'{"state":"Rejected"}'
'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
HTTP/1.0 400 Bad Request
Date: Tue, 09 Oct 2018 22:22:58 GMT
Server: WSGIServer/0.2 CPython/3.6.6
Content-Type: application/json
Vary: Accept, Cookie
Allow: GET, PUT, PATCH, HEAD, OPTIONS
X-Frame-Options: SAMEORIGIN

{"delegate":["This field is required."]}

Okay, so it wants the delegate field to be present. Not sure why,
since I don't really want to set the delegate yet, but okay.

Second attempt:

$ curl -i -X PUT -H 'Content-Type: application/json' -d
'{"delegate":1, "state":"Rejected"}'
'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
HTTP/1.0 400 Bad Request
Date: Tue, 09 Oct 2018 22:25:43 GMT
Server: WSGIServer/0.2 CPython/3.6.6
Content-Type: application/json
Vary: Accept, Cookie
Allow: GET, PUT, PATCH, HEAD, OPTIONS
X-Frame-Options: SAMEORIGIN

{"delegate":{"non_field_errors":["Invalid data. Expected a dictionary,
but got int."]}}

Okay, so delegate needs to be a dictionary.

Third attempt:

$ curl -i -X PUT -H 'Content-Type: application/json' -d '{"delegate" :
{"id" : 1},"state":"Rejected"}'
'http://patchwork:passwd@localhost:8000/api/1.0/patches/1/'
HTTP/1.0 500 Internal Server Error
Date: Tue, 09 Oct 2018 22:26:44 GMT
Server: WSGIServer/0.2 CPython/3.6.6
Content-Type: text/plain
X-Frame-Options: SAMEORIGIN
Vary: Cookie

AssertionError at /api/1.0/patches/1/
The `.update()` method does not support writable nested fields by default.
Write an explicit `.update()` method for serializer
`patchwork.api.patch.PatchDetailSerializer`, or set `read_only=True`
on nested serializer fields.

Request Method: PUT
Request URL: http://localhost:8000/api/1.0/patches/1/
Django Version: 1.10.8
Python Executable: /usr/bin/python3
Python Version: 3.6.6
Python Path: ['/home/patchwork/patchwork', '/usr/lib/python36.zip',
'/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Tue, 9 Oct 2018 22:26:44 +

So, does that mean that Patchwork doesn't currently support this type
of functionality using the REST API? The error message seems to imply
that code changes to the Patchwork core are needed to make this work.

Can you provide me with some pointers where to go from here?

Thanks,
-Markus
___
Patchwork mailing list
Patchwork@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/patchwork