Metadata-Version: 2.5
Name: trytond_ticket_hub_heptapod
Version: 8.0.0.3
Summary: Ticket Hub Heptapod
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: gitlab,helpdesk,heptapod,ticket,tryton
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Classifier: Topic :: Office/Business :: Groupware
Requires-Python: >=3.10
Requires-Dist: requests
Requires-Dist: trytond-company<8.1,>=8.0
Requires-Dist: trytond-ticket-hub<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Description-Content-Type: text/x-rst

Ticket Hub Heptapod
===================

The ``ticket_hub_heptapod`` module is the Heptapod leg of the Ticket
Hub flywheel: it connects the platform-neutral ``ticket_hub`` base to a
Heptapod (GitLab) instance.

It provides the Heptapod/GitLab REST API client — ``HeptapodClient``, an
authenticated ``requests`` session with ``PRIVATE-TOKEN``
authentication, timeouts, pagination and failures typed for the retry
framework of the base module — the issue import of the projects mapped
to a connection, the bidirectional synchronization of their issues and a
webhook receiver for the GitLab issue and note events.

Project mappings
----------------

A Heptapod connection spans a whole instance, while the issues live in
individual projects.  The ``ticket_hub.project.mapping`` model binds one
project to the Ticket Hub:

- the ``Connection`` the project is read through;
- the ``Project`` reference the GitLab API accepts: the project path
  (``group/project``) or its numeric identifier;
- an optional ``Name`` label;
- the ``Default Assignee`` the imported issues are assigned to (a GitLab
  assignee is not a Tryton user, so it can not be translated);
- the ``Default Tag`` appended to every imported issue, so a project
  keeps a recognizable origin in the neutral ``Tags`` field;
- ``Import on Sync``, which decides whether the scheduled import reads
  the project.

Issue import
------------

*Import Issues* is offered on a Heptapod connection: it walks every
project mapped to the connection and creates or updates the matching
``ticket_hub.ticket``.  The state is translated through the state
mappings of the connection — a status label of the issue wins over the
plain GitLab ``opened``/``closed`` state — and the labels through the tag
mappings, with the default tag of the mapping appended.  The notes of an
issue become messages of the ``ticket_hub.message`` journal, ordered
chronologically and deduplicated by their GitLab identifier; the system
notes GitLab records for its own activity are left out.

Every run records its outcome in the ``ticket_hub.sync.log`` — one entry
per issue and per failure, typed transient or permanent — and stores the
identifier of the last issue it reached on the mapping, so an
interrupted or repeated import resumes where it left off.

Bidirectional synchronization
-----------------------------

Beyond the initial import the module keeps a Heptapod issue and its
Tryton ticket aligned in both directions.  A local state transition of a
synchronized ticket is pushed back to Heptapod as it happens: the state
is translated through the ``sync_back`` mappings of the connection and
written as a GitLab issue ``state_event``, and a divergence the platform
reports on the next import is journaled on the ticket timeline under a
documented conflict policy (the newer platform revision wins, a
local-only change keeps its pending write-back).  A local change of the
tags is pushed as the labels of the issue through the export direction
of the tag mappings, so the label mapping is applied both ways.

A comment marked *publish externally* becomes a new GitLab note, and the
identifier Heptapod assigns is stamped on the message so it is never
published twice; a failed write-back is recorded in the sync log without
rolling the local change back.

The *Synchronize* button of a Heptapod connection runs the
synchronization on demand through the sync adapter of the base module:
an incremental inbound leg reads the issues of every project flagged
``Import on Sync`` that changed since the stored cursor, and an outbound
leg publishes the pending comments.  The wizard reports the counters of
both legs, or the error that prevented the run.  The same adapter is
driven periodically by the ``ticket_hub.sync.run`` cron of the base
module, which also retries the failed operations with an exponential
backoff.

.. note::

   The module no longer registers a scheduler hook of its own: the
   ``ticket_hub.sync.run`` cron of the base module drives both legs, so
   the synchronization interval is the ``Sync Interval`` of the
   connection.

Webhook receiver
----------------

Heptapod can push its issue and note events to Tryton instead of waiting
for the periodic synchronization.  The receiver registers a Heptapod
handler with the webhook framework of the base module: a delivery is
parsed, the project mapping is resolved from the project references the
payload carries, and the issue is imported through the same engine as
the initial import — a repeated delivery never duplicates a ticket or a
journal message.  A payload that identifies no issue, or whose project
is not mapped, is queued: the connection is marked due and the periodic
incremental synchronization imports the changed issues on its next run.

A delivery that fails while the issue is fetched or imported is recorded
in the sync log as a retryable failure and retried by the cron with the
exponential backoff of the framework, so a temporary Heptapod outage
does not lose the event.  Every raw payload is stored on its log entry,
and the *Replay* button of a webhook entry hands it to the handler
again, so a delivery can be replayed after the cause of a failure was
fixed.

Heptapod-side setup
-------------------

1. In Heptapod, open *User Settings > Access Tokens* and create a
   personal access token with the ``api`` scope (or, on a newer
   instance, a project access token with the ``api`` scope for each
   mapped project).
2. Create a ``ticket_hub.connection`` with platform *Heptapod*, the
   instance URL (e.g. ``https://heptapod.example.com``) and the token,
   and use *Test Connection* to verify it.
3. Create a ``ticket_hub.project.mapping`` per project to import, with
   the project path (e.g. ``tryton/tryton-env-mono``) or its numeric
   identifier.
4. Optionally configure the state and tag mappings of the connection, so
   the GitLab ``opened``/``closed`` state and the project labels land as
   the intended ticket states and internal tags.

Webhook setup
~~~~~~~~~~~~~

1. In Heptapod, open the project, then *Settings > Webhooks* and add a
   webhook.
2. Set the URL to the webhook URL of the connection::

       https://<tryton-host>/<database_name>/ticket_hub/webhook/<token>

   The ``<token>`` is the *Webhook Token* of a Heptapod
   ``ticket_hub.connection``, visible to the Ticket Hub administration
   on the connection form.  The ``<database_name>`` is the name of the
   Tryton database the connection lives in.
3. Set the *Secret token* to the same ``<token>``: the receiver validates
   the ``X-Gitlab-Token`` header the GitLab webhook sends, accepting the
   generic ``X-Webhook-Token`` header as well.  An unknown token, an
   inactive connection and a wrong header all answer the same bare
   ``404``, so the endpoint never reveals whether a token exists.
4. Select the *Issues events* and *Comments* (notes) triggers.  The
   receiver understands the ``issue`` and ``note`` object kinds of the
   GitLab payload; the event name feeds the idempotency key of a
   delivery but is not required for processing.
5. Make sure the project of the webhook is mapped to the connection: a
   delivery of an unmapped project is queued for the periodic
   synchronization instead of being imported.

The webhook URL contains a secret: rotating the token with the
*Regenerate Webhook Token* button of the connection revokes the previous
URL.  Rate limiting is delegated to the reverse proxy in front of the
Tryton server, like for the base webhook endpoint.

The token is a secret: its field access is denied by default, so only
the ``Ticket Hub Administration`` group may read or write it.

Dependencies
------------

- Tryton modules: ``ir``, ``res``, ``company``, ``ticket_hub``.
- Python packages: ``requests``.
