# Jira Automation REST API - Endpoints for AImpress Tasks

**Based on:** https://developer.atlassian.com/cloud/automation/rest/intro/#about

**Status:** ✅ Full API available for creating automation rules

---

## WEEK 3: Create Automation Rules (15+ rules)

### Overview

The Jira Automation REST API allows you to:
- Create automation rules programmatically
- Enable/disable rules
- Update rule configurations
- Search existing rules
- Apply rules to different projects and scopes

**All endpoints support both `/rest/v1/` and `/rest/latest/` versions.**

---

## Core Endpoints

### 1. Create Automation Rule
```bash
POST /rest/v1/rule
```

**Request Body Structure:**
```json
{
  "name": "Auto-assign new issues to component lead",
  "description": "Automatically assigns new issues to the component lead",
  "enabled": true,
  "trigger": {
    "ruleType": "TriggerType",
    "parameters": {
      "eventType": "issue_created"
    }
  },
  "conditions": [
    {
      "ruleType": "ConditionType",
      "parameters": {
        "field": "project",
        "compareType": "equals",
        "value": "PROD"
      }
    }
  ],
  "actions": [
    {
      "ruleType": "ActionType",
      "parameters": {
        "assignee": "{{component.lead}}"
      }
    }
  ]
}
```

**Returns:** Rule object with `ruleUuid`

---

### 2. Get All Rules
```bash
GET /rest/v1/rule/summary
```

**Query Parameters:**
- `trigger` - Filter by trigger type
- `state` - Filter by enabled/disabled
- `scope` - Filter by scope (project, global)

**Response:** List of rule summaries

---

### 3. Get Specific Rule
```bash
GET /rest/v1/rule/{ruleUuid}
```

**Returns:** Complete rule configuration

---

### 4. Update Rule
```bash
PUT /rest/v1/rule/{ruleUuid}
```

**Request Body:** Same structure as create, with updates

---

### 5. Enable/Disable Rule
```bash
PUT /rest/v1/rule/{ruleUuid}/state
```

**Request Body:**
```json
{
  "enabled": true
}
```

---

### 6. Change Rule Scope
```bash
PUT /rest/v1/rule/{ruleUuid}/rule-scope
```

**Request Body:**
```json
{
  "scope": {
    "resources": ["ari:cloud:jira:cloud:project/PROD"]
  }
}
```

---

## Trigger Types

### Issue Created
```json
{
  "ruleType": "Issue_Created_Trigger",
  "parameters": {}
}
```

### Issue Updated
```json
{
  "ruleType": "Issue_Updated_Trigger",
  "parameters": {
    "field": "status"
  }
}
```

### Custom Field Changed
```json
{
  "ruleType": "Issue_Updated_Trigger",
  "parameters": {
    "field": "customfield_10111"
  }
}
```

### Sprint Events
```json
{
  "ruleType": "Sprint_Started_Trigger",
  "parameters": {}
}
```

### Scheduled Events
```json
{
  "ruleType": "Scheduled_Trigger",
  "parameters": {
    "scheduleType": "DAILY",
    "time": "09:00"
  }
}
```

---

## Condition Types

### Project Equals
```json
{
  "ruleType": "Project_Condition",
  "parameters": {
    "field": "project",
    "value": "PROD"
  }
}
```

### Issue Type Equals
```json
{
  "ruleType": "Issue_Type_Condition",
  "parameters": {
    "field": "issuetype",
    "value": "Bug"
  }
}
```

### Priority Equals
```json
{
  "ruleType": "Priority_Condition",
  "parameters": {
    "field": "priority",
    "compareType": "in",
    "value": ["Highest", "High"]
  }
}
```

### Custom Field Matches
```json
{
  "ruleType": "Custom_Field_Condition",
  "parameters": {
    "field": "customfield_10111",
    "compareType": "equals",
    "value": "New"
  }
}
```

### Status Changed To
```json
{
  "ruleType": "Status_Changed_Condition",
  "parameters": {
    "statusAfter": "In Progress"
  }
}
```

### Date/Time Condition
```json
{
  "ruleType": "Date_Condition",
  "parameters": {
    "field": "duedate",
    "compareType": "before",
    "value": "now()"
  }
}
```

---

## Action Types

### Assign Issue
```json
{
  "ruleType": "Assign_Action",
  "parameters": {
    "assignee": "{{component.lead}}"
  }
}
```

### Transition Issue
```json
{
  "ruleType": "Transition_Action",
  "parameters": {
    "status": "In Progress"
  }
}
```

### Update Custom Field
```json
{
  "ruleType": "Edit_Custom_Field_Action",
  "parameters": {
    "field": "customfield_10111",
    "value": "Contacted"
  }
}
```

### Send Email Notification
```json
{
  "ruleType": "Send_Email_Action",
  "parameters": {
    "recipients": ["{{issue.assignee.email}}", "team@example.com"],
    "subject": "Issue {{issue.key}} updated",
    "body": "Status changed to {{issue.status}}"
  }
}
```

### Create Issue
```json
{
  "ruleType": "Create_Issue_Action",
  "parameters": {
    "project": "OPS",
    "issueType": "Contract",
    "summary": "Contract for {{issue.summary}}",
    "description": "Created from {{issue.key}}"
  }
}
```

### Add Comment
```json
{
  "ruleType": "Add_Comment_Action",
  "parameters": {
    "comment": "Automatically assigned to {{assignee.name}}"
  }
}
```

### Change Label
```json
{
  "ruleType": "Edit_Labels_Action",
  "parameters": {
    "add": ["automated"],
    "remove": []
  }
}
```

### Webhook
```json
{
  "ruleType": "Webhook_Action",
  "parameters": {
    "url": "https://api.example.com/webhook",
    "headers": {
      "Authorization": "Bearer token"
    },
    "body": "{\"issue\": \"{{issue.key}}\"}"
  }
}
```

---

## Example Rules for AImpress

### Rule 1: Auto-assign new issues to component lead
```bash
POST /rest/v1/rule
```

```json
{
  "name": "PROD: Auto-assign to component lead",
  "description": "Automatically assigns new issues to the component lead",
  "enabled": true,
  "trigger": {
    "ruleType": "Issue_Created_Trigger"
  },
  "conditions": [
    {
      "ruleType": "Project_Condition",
      "parameters": {
        "value": "PROD"
      }
    }
  ],
  "actions": [
    {
      "ruleType": "Assign_Action",
      "parameters": {
        "assignee": "{{component.lead}}"
      }
    }
  ]
}
```

---

### Rule 2: Auto-assign hot leads to sales team
```bash
POST /rest/v1/rule
```

```json
{
  "name": "MARK: Auto-assign hot leads",
  "description": "Assign high-priority leads to sales team",
  "enabled": true,
  "trigger": {
    "ruleType": "Issue_Created_Trigger"
  },
  "conditions": [
    {
      "ruleType": "Project_Condition",
      "parameters": {
        "value": "MARK"
      }
    },
    {
      "ruleType": "Priority_Condition",
      "parameters": {
        "compareType": "in",
        "value": ["Highest", "High"]
      }
    }
  ],
  "actions": [
    {
      "ruleType": "Assign_Action",
      "parameters": {
        "assignee": "p.gulyk@ai-impress.com"
      }
    },
    {
      "ruleType": "Send_Email_Action",
      "parameters": {
        "recipients": ["p.gulyk@ai-impress.com"],
        "subject": "Hot lead assigned: {{issue.summary}}",
        "body": "A high-priority lead has been assigned to you"
      }
    }
  ]
}
```

---

### Rule 3: Email on deal won
```bash
POST /rest/v1/rule
```

```json
{
  "name": "MARK: Email on deal won",
  "description": "Notify team when deal is won",
  "enabled": true,
  "trigger": {
    "ruleType": "Issue_Updated_Trigger",
    "parameters": {
      "field": "customfield_10111"
    }
  },
  "conditions": [
    {
      "ruleType": "Custom_Field_Condition",
      "parameters": {
        "field": "customfield_10111",
        "compareType": "equals",
        "value": "Won"
      }
    }
  ],
  "actions": [
    {
      "ruleType": "Send_Email_Action",
      "parameters": {
        "recipients": ["team@ai-impress.com"],
        "subject": "Deal Won: {{issue.summary}}",
        "body": "Opportunity {{issue.key}} status changed to Won"
      }
    }
  ]
}
```

---

### Rule 4: Auto-escalate critical tickets
```bash
POST /rest/v1/rule
```

```json
{
  "name": "SUPP: Auto-escalate unresolved critical tickets",
  "description": "Escalate critical tickets after 1 hour unresolved",
  "enabled": true,
  "trigger": {
    "ruleType": "Scheduled_Trigger",
    "parameters": {
      "scheduleType": "HOURLY"
    }
  },
  "conditions": [
    {
      "ruleType": "Project_Condition",
      "parameters": {
        "value": "SUPP"
      }
    },
    {
      "ruleType": "Priority_Condition",
      "parameters": {
        "compareType": "equals",
        "value": "Highest"
      }
    },
    {
      "ruleType": "Status_Changed_Condition",
      "parameters": {
        "statusAfter": "Not_Resolved"
      }
    }
  ],
  "actions": [
    {
      "ruleType": "Send_Email_Action",
      "parameters": {
        "recipients": ["o.maslovets@ai-impress.com"],
        "subject": "URGENT: Escalation needed on {{issue.key}}",
        "body": "Critical ticket unresolved for 1+ hour"
      }
    }
  ]
}
```

---

### Rule 5: Auto-update invoice status to overdue
```bash
POST /rest/v1/rule
```

```json
{
  "name": "OPS: Mark invoices overdue",
  "description": "Auto-update invoice status when due date passed",
  "enabled": true,
  "trigger": {
    "ruleType": "Scheduled_Trigger",
    "parameters": {
      "scheduleType": "DAILY",
      "time": "08:00"
    }
  },
  "conditions": [
    {
      "ruleType": "Project_Condition",
      "parameters": {
        "value": "OPS"
      }
    },
    {
      "ruleType": "Issue_Type_Condition",
      "parameters": {
        "value": "Invoice"
      }
    },
    {
      "ruleType": "Date_Condition",
      "parameters": {
        "field": "customfield_10132",
        "compareType": "before",
        "value": "now()"
      }
    }
  ],
  "actions": [
    {
      "ruleType": "Edit_Custom_Field_Action",
      "parameters": {
        "field": "customfield_10113",
        "value": "Overdue"
      }
    },
    {
      "ruleType": "Send_Email_Action",
      "parameters": {
        "recipients": ["team@ai-impress.com"],
        "subject": "Invoice {{issue.key}} is overdue",
        "body": "Invoice {{issue.summary}} is now overdue"
      }
    }
  ]
}
```

---

## Search & Filter Rules

### Search Rules by Trigger
```bash
GET /rest/v1/rule/summary?trigger=Issue_Created_Trigger
```

### Search Rules by State
```bash
GET /rest/v1/rule/summary?state=enabled
```

### Search Rules by Scope (Project)
```bash
GET /rest/v1/rule/summary?scope=ari:cloud:jira:cloud:project/PROD
```

---

## Rate Limits

- 100 requests per 10 seconds per user
- Respect Retry-After headers

---

## Authentication

All endpoints require Basic Auth:

```bash
Authorization: Basic $(echo -n "email:api_token" | base64)
```

---

## Testing Rules

1. Create rule with `"enabled": false`
2. Test by manually triggering conditions
3. Review audit logs
4. Enable with `PUT /rest/v1/rule/{ruleUuid}/state`

---

**Documentation:** https://developer.atlassian.com/cloud/automation/rest/intro/#about

**Key Limitations:**
- Some advanced conditions may require UI configuration
- Webhook payloads need proper escaping
- Test with limited scope before global rollout
