Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.5 KB
Raw Blame
import { HogFunctionTemplate } from '~/cdp/types'

import { ERROR_TRACKING_TICKET_DESCRIPTION, ERROR_TRACKING_TICKET_TITLE } from '../../error-tracking-ticket'
import { hogErrorTrackingStacktraceFn } from '../../hog-helpers'

export const template: HogFunctionTemplate = {
    status: 'stable',
    free: false,
    type: 'destination',
    id: 'template-linear',
    name: 'Linear',
    description: 'Creates an issue for a Linear team',
    icon_url: '/static/services/linear.png',
    category: ['Error tracking'],
    code_language: 'hog',
    code: `${hogErrorTrackingStacktraceFn}

fun query(mutation) {
    return fetch('https://api.linear.app/graphql', {
        'body': {
            'query': mutation,
        },
        'method': 'POST',
        'headers': {
            'Authorization': f'Bearer {inputs.linear_workspace.access_token}',
            'Content-Type': 'application/json'
        }
    })
}

let description := concat(inputs.description, errorTrackingStacktrace(event.properties.$exception_list));

let issue_mutation := f'mutation IssueCreate \\{ issueCreate(input: \\{ title: {jsonStringify(inputs.title)} description: {jsonStringify(description)} teamId: "{inputs.team}" }) \\{ success issue \\{ identifier } } }';

let issue_response := query(issue_mutation);

if (issue_response.status != 200) {
  throw Error(f'Failed to post create issue in Linear: {issue_response.status}: {issue_response.body}');
}

let linear_issue_id := issue_response.body.data.issueCreate.issue.identifier;

let attachment_url := inputs.posthog_issue_url;
if (empty(attachment_url)) {
  attachment_url := f'{project.url}/error-tracking/{inputs.posthog_issue_id}';
}
let attachment_mutation := f'mutation AttachmentCreate \\{ attachmentCreate(input: \\{ issueId: "{linear_issue_id}", title: "PostHog issue", url: "{attachment_url}" }) \\{ success } }';

query(attachment_mutation);`,
    inputs_schema: [
        {
            key: 'linear_workspace',
            type: 'integration',
            integration: 'linear',
            label: 'Linear workspace',
            secret: false,
            hidden: false,
            required: true,
        },
        {
            key: 'team',
            type: 'integration_field',
            integration_key: 'linear_workspace',
            integration_field: 'linear_team',
            label: 'Team',
            secret: false,
            hidden: false,
            required: true,
        },
        {
            key: 'title',
            type: 'string',
            label: 'Title',
            secret: false,
            hidden: false,
            required: true,
            default: ERROR_TRACKING_TICKET_TITLE,
        },
        {
            key: 'description',
            type: 'string',
            label: 'Description',
            secret: false,
            hidden: false,
            required: true,
            default: ERROR_TRACKING_TICKET_DESCRIPTION,
        },
        {
            key: 'posthog_issue_id',
            type: 'string',
            label: 'PostHog issue ID',
            secret: false,
            hidden: true,
            required: true,
            default: '{event.properties.$exception_issue_id}',
        },
        {
            key: 'posthog_issue_url',
            type: 'string',
            label: 'PostHog issue URL',
            description:
                'Link back to the PostHog issue. When empty, a link is built from the PostHog issue ID instead.',
            secret: false,
            hidden: true,
            required: false,
        },
    ],
}