Skip to content

Commit c3e8758

Browse files
committed
Prepare for required server version
We want to introduce server version-dependent functionality in git-pw. While using the versionless URL might be slightly shorter, it does make things a lot tougher to figure out. Start warning users if the server URL provided doesn't come with a server version. This can be an error in future versions. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent a573972 commit c3e8758

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

README.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ To begin, you'll need to configure Git settings appropriately. The following
7272
settings are **required**:
7373

7474
``pw.server``
75-
The URL for the Patchwork instance. This will typically look like. For
76-
example::
75+
The URL for the Patchwork instance's API. This should include the API
76+
version::
7777

78-
https://patchwork.ozlabs.org/
78+
https://patchwork.ozlabs.org/api/1.1
79+
80+
You can discover the API version supported by your instance by comparing the
81+
server version, found at ``/about``, with the API versions provided in the
82+
`documentation`__.
83+
84+
__ https://patchwork.readthedocs.io/en/stable-2.1/api/rest/#rest-api-versions
7985

8086
``pw.project``
8187
The project name or list-id. This will appear in the URL when using the web
@@ -101,7 +107,7 @@ configure the Patchwork project, run:
101107

102108
.. code-block:: bash
103109
104-
$ git config pw.server 'https://patchwork.ozlabs.org/api/1.0/'
110+
$ git config pw.server 'https://patchwork.ozlabs.org/api/1.1/'
105111
$ git config pw.project 'patchwork'
106112
107113
Development

git_pw/api.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,21 @@ def _get_headers(): # type: () -> Dict[str, str]
5757

5858
def _get_server(): # type: () -> str
5959
if CONF.server:
60-
return CONF.server.rstrip('/')
60+
server = CONF.server.rstrip('/')
61+
62+
if not re.match(r'.*/api/\d\.\d$', server):
63+
LOG.warning('Server version missing')
64+
LOG.warning('You should provide the server version in the URL '
65+
'configured via git-config or --server')
66+
LOG.warning('This will be required in git-pw 2.0')
67+
68+
if not re.match(r'.*/api(/\d\.\d)?$', server):
69+
# NOTE(stephenfin): We've already handled this particular error
70+
# above so we don't warn twice. We also don't stick on a version
71+
# number since the user clearly wants the latest
72+
server += '/api'
73+
74+
return server
6175
else:
6276
LOG.error('Server information missing')
6377
LOG.error('You must provide server information via git-config or via '
@@ -193,7 +207,7 @@ def index(resource_type, params=None):
193207
A list of dictionaries, representing the summary view of each resource.
194208
"""
195209
# NOTE(stephenfin): All resources must have a trailing '/'
196-
url = '/'.join([_get_server(), 'api', resource_type, ''])
210+
url = '/'.join([_get_server(), resource_type, ''])
197211

198212
# NOTE(stephenfin): Not all endpoints in the Patchwork API allow filtering
199213
# by project, but all the ones we care about here do.
@@ -218,8 +232,7 @@ def detail(resource_type, resource_id, params=None):
218232
A dictionary representing the detailed view of a given resource.
219233
"""
220234
# NOTE(stephenfin): All resources must have a trailing '/'
221-
url = '/'.join([_get_server(), 'api', resource_type,
222-
str(resource_id), ''])
235+
url = '/'.join([_get_server(), resource_type, str(resource_id), ''])
223236

224237
return get(url, params, stream=False).json()
225238

@@ -238,7 +251,7 @@ def update(resource_type, resource_id, data):
238251
Returns:
239252
A dictionary representing the detailed view of a given resource.
240253
"""
241-
url = '/'.join([CONF.server.rstrip('/'), 'api', '1.0', resource_type,
242-
str(resource_id), ''])
254+
# NOTE(stephenfin): All resources must have a trailing '/'
255+
url = '/'.join([_get_server(), resource_type, str(resource_id), ''])
243256

244257
return put(url, data).json()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
upgrade:
3+
- |
4+
Configuring a server without the API version, e.g. via ``git config
5+
pw.server`` will now result in a warning. An error will be raised in a
6+
future release of *git-pw*.

0 commit comments

Comments
 (0)