Skip to content

Generate the Gmail template from Google's API description - #319

Merged
tnkuehne merged 7 commits into
feat/template-generatorfrom
feat/gmail-template
Nov 23, 2026
Merged

tnkuehne merged 7 commits into
feat/template-generatorfrom
feat/gmail-template

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

The Gmail template offers a few hand-written methods: search messages, read them, download attachments. Anything else, like sending, drafts, labels or settings, needs a template change. Linear already exposes its whole API as typed methods with approvals. Gmail gets the same from Google's published discovery document, and so can later Google templates (Calendar, Drive, Sheets).

What changes

Generator

  • Discovery generator: turns a Google discovery document into typed methods.
    • Methods are named after their id without the shared resource prefix: users.messages.get becomes messagesGet.
    • Each method takes its path and query parameters plus a typed body. Path parameters with a default, like userId: "me", are optional.
    • Schemas become types. Every field is optional, int64 values are strings, enums are unions.
    • src/schema.ts lists each method's HTTP route and parameters, and the OAuth scopes that cover all offered methods.
  • Repeated parameters: a repeated parameter with choices takes a list of them, such as historyTypes?: ("messageAdded" | ...)[].
  • Rules: every method that is not a GET must be classified in scripts/operations.ts as a change or as not offered. New methods and stale rules stop the generator, like for GraphQL.
  • Field selection: methods with a response take select. The runtime turns it into Google's fields parameter (id,payload(headers,parts(mimeType))), so Google returns only the selected fields. The selection types now handle REST schemas, where every field is optional.
  • Shared runtime: request inputs, the approval page form and the approval-level methods move into the shared runtime files. Linear uses them too.
  • Approval level form after a failed save: the form reads the stored level from GET /approval-level and shows it, because a lost reply may follow a successful save. It reports a failure only when the stored level differs from the chosen one.
  • New change category send: for actions that reach other people, like sending a message or an invitation. The default level asks before changes, deletions and sends. In Linear, invitations, invite resends and update reminders are now send.

Gmail template

  • 69 typed methods.

  • 10 methods are not offered because only service accounts can call them: S/MIME, delegates, forwarding addresses and send-as changes.

  • Approvals work like in the Linear template: per-user level on the connection page, plus the approval question in the agent.

  • Scopes: https://mail.google.com/ and gmail.settings.basic, which cover every offered method. Both are restricted scopes. A connection that lacks one answers permission_required with a link to reconnect.

  • Errors:

    Gmail answer Result
    401 reconnect
    429 or a rate-limit reason rate_limited with retryAfter
    insufficientPermissions permission_required
    any other error gmail_error with Google's message, status and reasons
  • Large responses: larger than 8 MiB, declared or counted while streaming, give too_large with maximumBytes. Nothing is read into memory past the limit, as in the hand-written template.

  • Arguments: optional arguments passed as undefined are left out, so defaults such as userId: "me" apply.

  • Missing scopes: connectionStatus reports permission_required for an account with fewer scopes. Before, a change asked for approval first and then failed.

  • OAuth: Gmail connects only with the organization's own OAuth client (GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET). The managed login and its test case in template-oauth.test.ts are removed.

  • The hand-written message helpers (messages.ts, bytes.ts) are removed. Apps decode raw and body data themselves.

  • template.md describes method names, selections, approvals, results, scopes and how to regenerate.

Connection page

Connected state, with the same approval form as Linear.

Before After

Not shown: the disconnected and setup-required states. They only change their description text.

Testing

  • pnpm check
  • Generator tests: discovery output, rules, fields expressions, request building.
  • Gmail tests against a mocked Google: typed requests and fields URLs, approvals for send, delete and every level, error mapping, missing scopes.
  • Type tests: selections narrow results, unknown fields and arguments don't compile.
  • Regenerating Gmail and Linear from saved sources reproduces the committed files.
  • Not tested against a live Google account.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the forwarding approval gap is closed.

Summary

Generates Gmail's typed API and adds shared request and approval code.

  • Gmail calls can now use typed methods across messages, drafts, labels, threads, and settings.
  • The template generator can now turn Google Discovery documents into typed API methods.
  • Approval rules now ask before sends and share the same approval page and methods.
  • Gmail connections request the scopes needed for the expanded API.

Reviews (6) · Last reviewed commit: "Ask before Gmail filters, which can forw..." · Reviewed by Greptile

@tnkuehne
tnkuehne added this pull request to stack #320 November 22, 2026 23:38
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 24a5c98 Nov 23 2026, 10:17 AM

@tnkuehne
tnkuehne force-pushed the feat/gmail-template branch from a5b3910 to 55836a5 Compare November 22, 2026 23:52
Comment thread templates/gmail/src/run.ts Resolved
@tnkuehne
tnkuehne force-pushed the feat/gmail-template branch from 55836a5 to 20e20d6 Compare November 22, 2026 23:53
Comment thread templates/gmail/src/run.ts Resolved
The Gmail template wrapped a handful of hand-written methods. It now offers
Gmail's whole API as typed methods, generated from Google's discovery
document: pnpm --filter @platform/template-generator generate gmail.

Each method takes its path and query parameters and a typed body, and
returns only the fields its selection names; the selection becomes Google's
fields parameter. Methods that only service accounts can call are not
offered. Changes ask for approval like in the Linear template, with a new
"send" category for messages and other actions that reach other people.

Gmail connects with the organization's own OAuth client, and the template
requests the scopes the offered methods need.
A change checks the connection status before it asks for approval. An
account that granted fewer scopes was reported as connected there, so the
user was asked first and the change then failed with permission_required.
Responses larger than 8 MiB return too_large instead of being read into
memory, as the hand-written template did. Optional arguments passed as
undefined are left out, and repeated parameters with choices take a list of
them.
When a save's reply is lost, the level may have been stored anyway. The page
now reads the stored level and shows it, and reports a failure only when the
stored level differs from the chosen one.
When a save fails while the user has already chosen another level, the
page keeps that choice and saves it next instead of showing the stored
level.
@tnkuehne
tnkuehne force-pushed the feat/gmail-template branch from e3f354b to dbc93a4 Compare November 23, 2026 10:05
Comment thread templates/gmail/scripts/operations.ts Resolved
@tnkuehne
tnkuehne merged commit c192260 into main Nov 23, 2026
17 checks passed
@tnkuehne
tnkuehne deleted the feat/gmail-template branch November 23, 2026 10:49
tnkuehne added a commit that referenced this pull request Nov 23, 2026
#324)

## Why

Google Calendar is one of the most requested integrations, and none of
the templates covers it yet. Google publishes a Discovery document for
it, which the generator from #319 already reads. A calendar template
therefore needs only its rules and the template code around the
generated API.

## What changes

**Google Calendar template** (`templates/google-calendar`)

- **Methods:** all 38 methods of the Google Calendar API as typed
methods with field selections: events and recurring instances,
calendars, the calendar list, sharing (ACL), settings, colors, free/busy
times, and push notification channels. None is left out.
- **Approvals:** each user's approval level decides when a method asks,
as in the other templates.
- `eventsInsert` is `send`: it adds the event to its guests' calendars
and, with `sendUpdates`, emails them.
  - Quick-add, imported private copies and new calendars are `create`.
- Changing, moving, sharing, clearing and deleting are updates or
deletions.
- **Scope:** `https://www.googleapis.com/auth/calendar`, which covers
every method. An account connected with fewer scopes gets
`permission_required`.
- **OAuth:** the organization's own Google OAuth client only
(`GOOGLE_CLIENT_ID`/`GOOGLE_CLIENT_SECRET`), like Gmail. The scope is
sensitive, so a managed client would need Google's verification.
- **Results:** `calendar_error` carries Google's message, status and
reasons. Rate limits, missing scopes, revoked tokens and responses over
8 MiB are handled as in Gmail.
- **Icon:** a plain calendar icon, not Google's logo.
- `template.md` describes setup, method names, selections, approvals,
results and how to regenerate.

**Generator**

- **`read` rule:** discovery rules accept `read` for methods that only
read through a POST, such as `freebusy.query`. Those methods never ask.

**Catalog tests**

- **Template list:** the catalog tests compare against the template
directories in the repository instead of a fixed list of four, so adding
a template doesn't change them.

## Connection page

New page, connected state, with the same approval form as the other
templates.

| 1280 px | 390 px |
| --- | --- |
| <img
src="https://gist.githubusercontent.com/tnkuehne/2e857c8b70cfc0a62168609f2702355e/raw/90e74caf0ea6221c430b0094bd0ff5df4120a8f7/calendar-after-1280.png"
width="400"> | <img
src="https://gist.githubusercontent.com/tnkuehne/2e857c8b70cfc0a62168609f2702355e/raw/90e74caf0ea6221c430b0094bd0ff5df4120a8f7/calendar-after-390.png"
width="200"> |

## Testing

- `pnpm check`
- **Generator:** a test for the `read` rule.
- **Calendar template against a mocked Google:**
- typed requests with `fields` URLs and `undefined` arguments left out;
  - invitations asking as `send`;
  - quick-add running at once by default and asking at `all_changes`;
  - `freebusyQuery` never asking;
  - each approval level;
  - Google errors, rate limits and missing scopes;
  - oversized responses;
  - token refresh, outages and callback checks.
- **Type tests:** the README example compiles; unknown fields, missing
selections, a missing calendar and wrong choices don't.
- **Catalog and template install tests:** pass with the new template.
- Not tested against a live Google account.






























<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=116075280&amp;sig=ae468be2d6b8b06c9cb9abfa21e52632275c8828c174e4745950025fe3243aad"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F324&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier:
apex" title="Review tier: apex"
src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-apex.svg?v=3"
height="12"></sup></h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge with the
required query-argument checks from dependent PR #325.

<details open><summary><h3>Summary</h3></summary>

Adds a personal Google Calendar template with 38 typed methods, field
selections and per-user approvals.

- Apps can connect a user's Google account and call typed Calendar
methods.
- The generator can include read-only methods that use POST.
</details>

<details open><summary><h3>Diagram</h3></summary>

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Calendar method"] --> B["Build request and field selection"]
  B --> C{"Changes data?"}
  C -- No --> F["Send with the user's Google token"]
  C -- Yes --> D{"Approval needed?"}
  D -- No --> F
  D -- Yes --> E["Ask the user"]
  E -- Accepted --> F
  E -- Declined --> G["Return declined"]
  F --> H["Return selected data or an error"]
```
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (15) · Last reviewed commit: ["Add a Google Calendar
template
generated..."](615a3e6)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant