github.com/blend/go-sdk@v1.20240719.1/pagerduty/create_incident.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package pagerduty
     9  
    10  import (
    11  	"context"
    12  	"encoding/json"
    13  	"net/http"
    14  
    15  	"github.com/blend/go-sdk/ex"
    16  	"github.com/blend/go-sdk/r2"
    17  )
    18  
    19  // CreateIncident creates an incident using the incident events api.
    20  func (hc HTTPClient) CreateIncident(ctx context.Context, incident CreateIncidentInput) (output Incident, err error) {
    21  	var res *http.Response
    22  	res, err = hc.Request(ctx,
    23  		r2.OptPost(),
    24  		r2.OptPath("/incidents"),
    25  		r2.OptJSONBody(createIncidentInputWrapper{Incident: incident}),
    26  	).Do()
    27  	if err != nil {
    28  		return
    29  	}
    30  	if statusCode := res.StatusCode; statusCode < 200 || statusCode > 299 {
    31  		err = ex.New(ErrNon200Status, ex.OptMessagef("method: post, path: /incidents, status: %d", statusCode))
    32  		return
    33  	}
    34  	defer res.Body.Close()
    35  	var body createIncidentOutputWrapper
    36  	if err = json.NewDecoder(res.Body).Decode(&body); err != nil {
    37  		err = ex.New(err)
    38  		return
    39  	}
    40  	output = body.Incident
    41  	return
    42  }
    43  
    44  // CreateIncidentInput is the input to create|update incident.
    45  type CreateIncidentInput struct {
    46  	Type             string            `json:"type"`    // required
    47  	Title            string            `json:"title"`   // required
    48  	Service          APIObject         `json:"service"` /// required
    49  	Priority         *APIObject        `json:"priority,omitempty"`
    50  	Body             *Body             `json:"body,omitempty"`
    51  	IncidentKey      string            `json:"incident_key,omitempty"`
    52  	Assignments      []Assignment      `json:"assignments,omitempty"`
    53  	EscalationPolicy *APIObject        `json:"escalation_policy,omitempty"`
    54  	Urgency          Urgency           `json:"urgency,omitempty"`
    55  	ConferenceBridge *ConferenceBridge `json:"conference_bridge,omitempty"`
    56  }
    57  
    58  // createIncidentInputWrapper wraps the input to satisfy the input schema.
    59  type createIncidentInputWrapper struct {
    60  	Incident CreateIncidentInput `json:"incident"`
    61  }
    62  
    63  // CreateIncidentOutput is the response to create incident.
    64  type createIncidentOutputWrapper struct {
    65  	Incident Incident `json:"incident"`
    66  }