Action Specification


This document specifies how actions exposed by the decision task are to be presented and triggered from Treeherder, or similar user interfaces.

The decision task creates an artifact public/actions.json to be consumed by a user interface such as Treeherder. The public/actions.json file specifies the available actions.

Through the public/actions.json file it is possible expose actions defined in-tree such that the actions can be conveniently triggered in a user interface tool such as Treeherder. This has two purposes:

  1. Facilitate development of utility actions/tools in-tree, and,
  2. Strongly decouple build/test configuration from Treeherder.

For details on how define custom actions in-tree, refer to the in-tree actions section. This document merely specifies how actions.json shall be interpreted.

actions.json

The content of actions.json is a list of actions and some variables, to be described later.

Each action has a kind describing how a user interface should trigger it. The kind-specific properties are described later.

Action Metadata

Each action entry must define a name, title and description. furthermore, the list of actions should be sorted by the order in which actions should appear in a menu.

The name is used by user interfaces to identify the action. For example, a retrigger button might look for an action with name = "retrigger".

Action names must be unique for a given task, or for a taskgroup, but the same name may be used for actions applying to disjoint sets of tasks. For example, it may be helpful to define different "retrigger" actions for build tasks [{jobKind: 'build'}] and test tasks [{jobKind: 'test'}], and in this case only one such action would apply to any given task.

The title is a human readable string intended to be used as label on the button, link or menu entry that triggers the action. This should be short and concise. Ideally, you'll want to avoid duplicates.

The description property contains a human readable string describing the action, such as what it does, how it does it, what it is useful for. This string is to be rendered as markdown, allowing for bullet points, links and other simple formatting to explain what the action does.

Action Context

Few actions are relevant in all contexts. For this reason each action specifies a context property. This property specifies when an action is relevant. Actions relevant for a task should be displayed in a context menu for the given task. Similarly actions not relevant for a given task should not be displayed in the context menu for the given task.

As a special case we say that actions for which no relevant contexts can exist, are relevant for the task-group. This could for example be an action to create tasks that was optimized away.

The context property is specified as a list of tag-sets. A tag-set is a set of key-value pairs. A task is said to match a tag-set if task.tags is a super-set of the tag-set. An action is said to be relevant for a given task, if task.tags match one of the tag-sets given in the context property for the action.

Naturally, it follows that an action with an empty list of tag-sets in its context property cannot possibly be relevant for any task. Hence, by previously declared special case such an action is relevant for the task-group.

Examples:

// Example task definitions (everything but tags eclipsed)
TaskA = {..., tags: {kind: 'test', platform: 'linux'}}
TaskB = {..., tags: {kind: 'test', platform: 'windows'}}
TaskC = {..., tags: {kind: 'build', platform: 'linux'}}

Action1 = {..., context: [{kind: 'test'}]}
// Action1 is relevant to: TaskA, TaskB

Action2 = {..., context: [{kind: 'test', platform: 'linux'}]}
// Action2 is relevant to: TaskA

Action3 = {..., context: [{platform: 'linux'}]}
// Action3 is relevant to: TaskA, TaskC

Action4 = {..., context: [{kind: 'test'}, {kind: 'build'}]}
// Action4 is relevant to: TaskA, TaskB, TaskC

Action5 = {..., context: [{}]}
// Action5 is relevant to: TaskA, TaskB, TaskC (all tasks in fact)

Action6 = {..., context: []}
// Action6 is relevant to the task-group

Action Input

An action can take JSON input, the input format accepted by an action is specified using a JSON schema. This schema is specified with by the action's schema property. For example:

{
  "version": 1,
  "actions": [
    {
      "kind": "task",
      "name": "thing",
      "title": "Do A Thing",
      "description": "Do something",
      "schema": {
        "description": "The thing to do",
        "title": "Thing",
        "default": "something",
        "type": "string",
        "maxLength": 255
      },
      "task": {
        "payload": {
          "env": {
            "INPUT_JSON": {"$json": {"$eval": "input"}}
          },
          ...
        },
        ...
      }
    }
  ],
  "variables": {}
}

User interfaces for triggering actions, like Treeherder, are expected to provide JSON input that satisfies this schema. These interfaces are also expected to validate the input against the schema before attempting to trigger the action.

It is perfectly legal to reference external schemas using constructs like {"$ref": "https://example.com/my-schema.json"}, in this case it however strongly recommended that the external resource is available over HTTPS and allows CORS requests from any source.

When writing schemas it is strongly encouraged that the JSON schema description properties are used to provide detailed descriptions. It is assumed that consumers will render these description properties as markdown.

In practice it's encouraged that consumers employ a facility that can generate HTML forms from JSON schemas. However, if certain schemas are particularly complicated or common, consumers may also hand-write a user-interface for collecting the input. In this case the consumer must do a deep comparison between the schema given in the action, and the schema for which a custom user-interface have been written, and fall-back to an auto-generated form if the schema doesn't match.

Variables

The public/actions.json artifact has a variables property that is a mapping from variable names to JSON values to be used as constants. These variables can be referenced from task templates, but beware that they may overshadow builtin variables. This is mainly useful to deduplicate commonly used values, in order to reduce template size. This feature does not introduce further expressiveness.

JSON-e Context

Most kinds use JSON-e to render templates. The context for that rendering contains:

taskGroupId: the taskGroupId of task-group this is triggered from

taskId: the taskId of the selected task, null if no task is selected (this is the case if the action has context: []).

task: (kind == 'task' only) the task definition of the selected task, or null if no task is selected (this is the case if the action has context: []). For kind == 'hook', the temptation to rely on this unstrusted information would be too great -- fetch the taskId from the Queue instead.

ownTaskId: (kind == 'task' only) the taskId of the action task itself. This is useful for indexing without collisions and other similar needs; for kind == 'hook', use the taskId provided by the hooks service.

input: the input matching the schema property, null if the action doesn't have a schema property. See "Action Input" below.

<key>: Any <key> defined in the variables property may also be referenced. See "Variables" below.

Do not confuse the JSON-e context (used to render a template) with the action context (used to control which actions apply to which tasks).

The following example demonstrates how a task template can specify timestamps and dump input JSON into environment variables:

{
  "version": 1,
  "actions": [
    {
      "kind": "task",
      "name": "thing",
      "title": "Do A Thing",
      "description": "Do something",
      "schema": {
        "type": "object",
        "properties": {
          "thing": {
            "description": "The thing to do",
            "type": "string",
            "maxLength": 255,
            "default": "something"
          }
        }
      },
      "task": {
        "workerType": "my-worker",
        "payload": {
          "created": {"$fromNow": ""},
          "deadline": {"$fromNow": "1 hour 15 minutes"},
          "expiration": {"$fromNow": "14 days"},
          "image": "my-docker-image",
          "env": {
            "TASKID_TRIGGERED_FOR": "${taskId}",
            "REVISION": "${revision}",
            "THING_TO_DO": {"$eval": "input.thing"}
          },
          ...
        },
        ...
      }
    }
  ],
  "variables": {
    "revision": "123abc"
  }
}

Kinds

"task" kind

An action with kind: 'task' specifies a task that the user interface should create. That is, when the action is triggered, the user interface calls the Taskcluster API to create a new task, with the content of that task determined from actions.json.

The task created by the action may be useful in its own right (for example, running a test with additional debugging), or it may simplify trigger in-tree scripts that create new tasks. The latter form is called an action task, and is similar to a decision task. This allows in-tree scripts to execute complicated actions such as backfilling.

Actions of the 'task' kind must have a task property. This property specifies the task template to be parameterized and created in order to trigger the action.

"hook" kind

An action with kind: 'hook' specifies a hook that should be triggered via the hooks.triggerHook API method when the action is executed.

The hook is specified in the hookId and hookGroupId properties.

The payload of the triggerHook method is given by the hookPayload property, which is rendered using JSON-e with the context described above.

Note that the result of rendering hookPayload becomes the context for another JSON-e rendering performed by the hooks service. Do not confuse the two operations!

Formal Specification

The JSON Schema for actions.json is as follows, also available in raw form here.

$schema: http://json-schema.org/draft-04/schema#
id: https://docs.taskcluster.net/manual/tasks/actions/schema.yml
title: Schema for Exposing Actions
description: |
  This document specifies the schema for the `public/actions.json` used by
  _decision tasks_ to expose actions that can be triggered by end-users.

  For the purpose of this document the _consumer_ is the user-interface that
  displays task results to the end-user and allows end-users to trigger actions
  defined by `public/actions.json`. A _consumer_ might be Treeherder.
  The _end-user_ is a developer who is inspecting the results, and wishes to
  trigger actions.
definitions:
  name:
    type: string
    maxLength: 255
    description: |
      The name of this action.  This is used by user interfaces to
      identify the action.
  title:
    type: string
    maxLength: 255
    description: |
      Title text to be displayed on the button or link triggering the action.
  description:
    type: string
    maxLength: 4096
    description: |
      Human readable description of the action in markdown. This
      can be displayed in a tooltip, popup and/or dialog when triggering
      the action.
  context:
    type: array
    default: []
    items:
      type: object
      additionalProperties:
        type: string
        maxLength: 4096
      title: tag-set
      description: |
        A set of key-value pairs specifying a _tag-set_.
    description: |
      The `context` property determines in what context the action is
      relevant. Thus, what context the action should be presented to the
      end-user.
  schema:
    $ref: http://json-schema.org/schema
    description: |
      JSON schema for user input to the action.  If this property is omitted,
      then the input is `null`.

type: object
properties:
  version:
    enum: [1]
    type: integer
  variables:
    type: object
    description: |
      Additional variables included in JSON-e context.
    additionalProperties: true
  actions:
    type: array
    description: |
      List of actions that can be triggered.
    items:
      anyOf:
      - type: object
        properties:
          kind: {enum: [task]}
          name: {$ref: '#/definitions/name'}
          title: {$ref: '#/definitions/title'}
          description: {$ref: '#/definitions/description'}
          context: {$ref: '#/definitions/context'}
          schema: {$ref: '#/definitions/schema'}
          task:
            type: object
            title: Task Template
            description: |
              Task template for triggering the action.

              When an action have been selected in the appropriate context and
              input satisfying the `schema` (if any) has been collected. The
              action is triggered by parameterizing the task template given in
              this property, and creating the resulting task.

              The template is an object that is parameterized using
              [JSON-e](https://github.com/taskcluster/json-e), with the context
              described in the documentation.
        additionalProperties: false
        required:
          - kind
          - title
          - description
          - task
      - type: object
        properties:
          kind: {enum: [hook]}
          name: {$ref: '#/definitions/name'}
          title: {$ref: '#/definitions/title'}
          description: {$ref: '#/definitions/description'}
          context: {$ref: '#/definitions/context'}
          schema: {$ref: '#/definitions/schema'}
          hookGroupId:
            type: string
            title: Hook Group ID
            description: The `hookGroupId` of the hook to trigger to run this action
          hookId:
            type: string
            title: Hook ID
            description: The `hookId` of the hook to trigger to run this action
          hookPayload:
            type: object
            title: Hook Payload Template
            description: |
              JSON-e template that renders to the payload for `hooks.triggerHook`.

              The template is an object that is parameterized using
              [JSON-e](https://github.com/taskcluster/json-e), with the
              context described in the documentation. The output of this
              rendering is provided as the payload to `hooks.triggerHook`
              (where it becomes the context for another JSON-e rendering
              step - don't get the two confused!)
        additionalProperties: false
        required:
          - kind
          - title
          - description
          - hookGroupId
          - hookId
          - hookPayload

additionalProperties: false
required:
  - version
  - actions
  - variables