CreateIssue creates a new JIRA issue and returns it
(projectIDOrKey, title, body string,
extra map[string]interface{})
| 949 | |
| 950 | // CreateIssue creates a new JIRA issue and returns it |
| 951 | func (client *Client) CreateIssue(projectIDOrKey, title, body string, |
| 952 | extra map[string]interface{}) (*IssueCreateResult, error) { |
| 953 | |
| 954 | url := fmt.Sprintf("%s/rest/api/2/issue", client.serverURL) |
| 955 | |
| 956 | fields := make(map[string]interface{}) |
| 957 | fields["summary"] = title |
| 958 | fields["description"] = body |
| 959 | for key, value := range extra { |
| 960 | fields[key] = value |
| 961 | } |
| 962 | |
| 963 | // If the project string is an integer than assume it is an ID. Otherwise it |
| 964 | // is a key. |
| 965 | _, err := strconv.Atoi(projectIDOrKey) |
| 966 | if err == nil { |
| 967 | fields["project"] = map[string]string{"id": projectIDOrKey} |
| 968 | } else { |
| 969 | fields["project"] = map[string]string{"key": projectIDOrKey} |
| 970 | } |
| 971 | |
| 972 | message := make(map[string]interface{}) |
| 973 | message["fields"] = fields |
| 974 | |
| 975 | data, err := json.Marshal(message) |
| 976 | if err != nil { |
| 977 | return nil, err |
| 978 | } |
| 979 | |
| 980 | request, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) |
| 981 | if err != nil { |
| 982 | return nil, err |
| 983 | } |
| 984 | |
| 985 | if client.ctx != nil { |
| 986 | ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout) |
| 987 | defer cancel() |
| 988 | request = request.WithContext(ctx) |
| 989 | } |
| 990 | |
| 991 | response, err := client.Do(request) |
| 992 | if err != nil { |
| 993 | err := fmt.Errorf("Performing request %v", err) |
| 994 | return nil, err |
| 995 | } |
| 996 | defer response.Body.Close() |
| 997 | |
| 998 | if response.StatusCode != http.StatusCreated { |
| 999 | content, _ := io.ReadAll(response.Body) |
| 1000 | err := fmt.Errorf( |
| 1001 | "HTTP response %d, query was %s\n data: %s\n response: %s", |
| 1002 | response.StatusCode, request.URL.String(), data, content) |
| 1003 | return nil, err |
| 1004 | } |
| 1005 | |
| 1006 | var result IssueCreateResult |
| 1007 | |
| 1008 | data, _ = io.ReadAll(response.Body) |