Repository: prometheus/alertmanager
File: notify/jira/types.go (Go)

Code before:
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jira

import (
	"encoding/json"
	"maps"
)

type issue struct {
	Key        string       `json:"key,omitempty"`
	Fields     *issueFields `json:"fields,omitempty"`
	Transition *idNameValue `json:"transition,omitempty"`
}

type issueFields struct {
	Description any           `json:"description,omitempty"`
	Issuetype   *idNameValue  `json:"issuetype,omitempty"`
	Labels      []string      `json:"labels,omitempty"`
	Priority    *idNameValue  `json:"priority,omitempty"`
	Project     *issueProject `json:"project,omitempty"`
	Resolution  *idNameValue  `json:"resolution,omitempty"`
	Summary     *string       `json:"summary,omitempty"`
	Status      *issueStatus  `json:"status,omitempty"`

	Fields map[string]any `json:"-"`
}

type idNameValue struct {
	ID   string `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

type issueProject struct {
	Key string `json:"key"`
}

type issueStatus struct {
	Name           string `json:"name"`
	StatusCategory struct {
		Key string `json:"key"`
	} `json:"statusCategory"`

Review comment (suggestion):
Let's at least add a comment here explaining that Description is RawJSON for v3 and string for v2?
Do we prefer this, or something like:
```
...
Description jiraDescription `json:"description,omitempty"`
...

```

And then a custom marshal (and unmarshal?):

```
type jiraDescription struct {
    StringDescription *string
    RawJSONDescription json.RawMessage
}

func (fd jiraDescription) MarshalJSON() ([]byte, error) {
    if len(fd.RawJSONValue) > 0 {
        return fd.RawJSONValue, nil
    }

    if fd.StringValue != nil {
        // json.Marshal will correctly quote the string value.
        return json.Marshal(*fd.StringValue)
    }

    return []byte, nil
}
```

Code after:
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jira

import (
	"bytes"
	"encoding/json"
	"maps"
)

// issue represents a Jira issue wrapper.
type issue struct {
	Key        string       `json:"key,omitempty"`
	Fields     *issueFields `json:"fields,omitempty"`
	Transition *idNameValue `json:"transition,omitempty"`
}

type issueFields struct {
	Description *jiraDescription `json:"description,omitempty"`
	Issuetype   *idNameValue     `json:"issuetype,omitempty"`
	Labels      []string         `json:"labels,omitempty"`
	Priority    *idNameValue     `json:"priority,omitempty"`
	Project     *issueProject    `json:"project,omitempty"`
	Resolution  *idNameValue     `json:"resolution,omitempty"`
	Summary     *string          `json:"summary,omitempty"`
	Status      *issueStatus     `json:"status,omitempty"`

	Fields map[string]any `json:"-"`
}

type idNameValue struct {
	ID   string `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

type issueProject struct {
	Key string `json:"key"`
}

type issueStatus struct {
	Name           string `json:"name"`
	StatusCategory struct {