Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active April 20, 2019 09:19
Show Gist options
  • Save snipsnipsnip/3b5e215b0882c2412871f22da6a11abd to your computer and use it in GitHub Desktop.
Save snipsnipsnip/3b5e215b0882c2412871f22da6a11abd to your computer and use it in GitHub Desktop.
trac-update-field.py writes a single field with xmlrpc
from __future__ import annotations
from xmlrpc.client import Transport, ServerProxy
import sys
import typing
if typing.TYPE_CHECKING:
import http.client
from typing import Dict, List, Tuple, Optional
class CookieTransport(Transport):
def __init__(self, cookie: str, *argv) -> None:
super().__init__(*argv)
self._cookie = cookie
def send_headers(self, connection: http.client.HTTPConnection, headers: List[Tuple[str, str]]) -> None:
super().send_headers(connection, [*headers, ("Cookie", self._cookie)])
def make_server(endpoint: str, cookie: str) -> ServerProxy:
return ServerProxy(endpoint, CookieTransport(cookie))
def make_update_params(server: ServerProxy, ticket_id: int, comment: str, key: str, val: str) -> Optional[Tuple[int, str, Dict[str, str]]]:
id, created, changed, attrs = server.ticket.get(ticket_id)
if ticket_id != id or not attrs or not attrs.get('_ts'):
return None
print("#", id, ": ", attrs.get('summary'))
return (
ticket_id,
comment,
{
'action': 'leave',
'_ts': attrs['_ts'],
key: val,
}
)
def update_ticket(server: ServerProxy, query: str, comment: str, key: str, val: str) -> None:
ticket_id = min(server.ticket.query(query))
if not ticket_id:
print("ticket not found for query: ", query, file=sys.stderr)
return
params = make_update_params(server, ticket_id, comment, key, val)
if not params:
print("Couldn't fetch timestamp to update: #", ticket_id, " (", query, ")", file=sys.stderr)
return
print((query, *params))
server.ticket.update(*params)
def main() -> None:
a = sys.argv
if len(a) != 8:
print("usage: %s endpoint cookie query-format query-param comment field-name field-value" % a[0], file=sys.stderr)
return
server = make_server(endpoint=a[1], cookie=a[2])
update_ticket(server=server, query=a[3] % a[4], comment=a[5], key=a[6], val=a[7])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment