Compare commits

..

No commits in common. "main" and "ics-no-caldav" have entirely different histories.

View File

@ -1,6 +1,5 @@
#!/usr/bin/env python3
import datetime
import json
import urllib.request
@ -15,27 +14,29 @@ GITEA_REPO_URL="https://git.bitmessage.org/api/v1"
combined = {}
def retrieve(token, kind):
if kind not in ("review_requested", "assigned"):
raise cherrypy.HTTPError(401, 'Unauthorized')
req = urllib.request.Request(GITEA_REPO_URL
+ f"/repos/issues/search?state=open&{kind}=true")
req.add_header("Accept", "application/json")
req.add_header("Authorization", "token " + token)
def get_combined(token):
for q in ("assigned", "review_requested"):
req = urllib.request.Request(GITEA_REPO_URL
+ f"/repos/issues/search?{q}=true")
req.add_header("Accept", "application/json")
req.add_header("Content-Type", "application/json")
req.add_header("Authorization", "token " + token)
retval = {}
with urllib.request.urlopen(req) \
as response:
issues = json.load(response)
for issue in issues:
_id = issue['id']
if _id in combined:
combined[_id]['categories'].append(q)
else:
combined[_id] = issue
combined[_id]['categories'] = [q]
return combined
with urllib.request.urlopen(req) as response:
issues = json.load(response)
for issue in issues:
_id = issue['id']
retval[_id] = issue
retval[_id]['categories'] = [kind]
return retval
def process(retrieved):
def process_combined(combined):
cal = Calendar()
for _id, issue in retrieved.items():
for _id, issue in combined.items():
todo = Todo()
todo['uid'] = _id
todo['dtstamp'] = issue['created_at']
@ -55,36 +56,20 @@ def get_token(input_token):
return token
class Root:
def _authenticate(self, kind):
@cherrypy.expose
def todo(self):
cherrypy.response.headers['WWW-Authenticate'] = \
'Basic realm="ICS access"'
cherrypy.response.headers['Content-Type'] = \
'text/calendar'
cherrypy.response.headers['Content-Disposition'] = \
f'attachment; filename="Gitea {kind}.ics"'
authorization = cherrypy.request.headers.get('Authorization', ':')
if not authorization:
raise cherrypy.HTTPError(401, 'Unauthorized')
token = get_token(authorization)
if not token:
raise cherrypy.HTTPError(401, 'Unauthorized')
return token
@cherrypy.expose
def assigned(self):
token = self._authenticate("assigned")
retrieved = retrieve(token, "assigned")
if not retrieved:
combined = get_combined(token)
if not combined:
raise cherrypy.HTTPError(401, 'Unauthorized')
return(process(retrieved))
@cherrypy.expose
def review_requested(self):
token = self._authenticate("review_requested")
retrieved = retrieve(token, "review_requested")
if not retrieved:
raise cherrypy.HTTPError(401, 'Unauthorized')
return(process(retrieved))
return(process_combined(combined))
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',