我正在调用两个单独的端点以将带有评论的附件发送给 Jira。
我先发送附件,并获取附件Id。然后我调用post请求评论,并将附件Id绑定到评论正文并发送评论。但它不是这样工作的。
这是我上传的附件,以及.net应用程序的发表评论功能。
[HttpPost("postcommentwithattachmentonjira")]
public async Task<IActionResult> AddCommentWithAttachmentOnJiraIssue(string issueKey, string commentBody, List<IFormFile> files)
{
try
{
// Step 1: Upload the file as an attachment
var attachmentId = await UploadAttachment(issueKey, files);
// Step 2: Add a comment with a link to the attachment
commentBody += $" [^{attachmentId}]";
return await AddCommentOnJiraIssue(issueKey, commentBody);
}
catch (Exception ex)
{
// Handle any exception that occurred during the process
return StatusCode(500, $"An error occurred: {ex.Message}");
}
}
// add attachmend endpoint
private async Task<string> UploadAttachment(string issueKey, List<IFormFile> files)
{
try
{
string url = $"{JiraBaseUrl}/rest/api/3/issue/{issueKey}/attachments";
using (var httpClient = new HttpClient())
{
string authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{JiraUsername}:{JiraApiToken}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
httpClient.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check"); // Add the header
// Create a multipart form data content
using (var multipartContent = new MultipartFormDataContent())
{
foreach (var file in files)
{
using (var memoryStream = new MemoryStream())
{
await file.CopyToAsync(memoryStream);
byte[] fileBytes = memoryStream.ToArray();
ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
multipartContent.Add(fileContent, "file", file.FileName);
}
}
HttpResponseMessage response = await httpClient.PostAsync(url, multipartContent);
response.EnsureSuccessStatusCode();
string responseContent = await response.Content.ReadAsStringAsync();
var responseObject = System.Text.Json.JsonSerializer.Deserialize<object>(responseContent);
string jsonString = System.Text.Json.JsonSerializer.Serialize(responseObject);
JsonDocument jsonDocument = JsonDocument.Parse(jsonString);
if (jsonDocument.RootElement.ValueKind == JsonValueKind.Array && jsonDocument.RootElement.GetArrayLength() > 0)
{
JsonElement firstElement = jsonDocument.RootElement[0];
if (firstElement.TryGetProperty("id", out JsonElement idElement) && idElement.ValueKind == JsonValueKind.String)
{
return idElement.GetString();
}
}
throw new Exception("Failed to extract attachment ID from the response.");
}
}
}
catch (Exception ex)
{
throw new Exception($"Failed to upload attachment to Jira issue. Error: {ex.Message}");
}
}
//add comment end point
private async Task<IActionResult> AddCommentOnJiraIssue(string issueKey, string commentBody)
{
string url = $"{JiraBaseUrl}/rest/api/2/issue/{issueKey}/comment";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent($"{{ \"body\": \"{commentBody}\" }}", Encoding.UTF8, "application/json");
string authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{JiraUsername}:{JiraApiToken}"));
request.Headers.Add("Authorization", $"Basic {authHeaderValue}");
HttpResponseMessage response = await _httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
var jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var responseObject = System.Text.Json.JsonSerializer.Deserialize<object>(jsonResponse, jsonOptions);
return Ok(responseObject);
}
else
{
return StatusCode((int)response.StatusCode, "Failed to post comment on Jira issue.");
}
}
是否有其他方法可以使用 jira Rest api 将附件发送到 Jira。或者说用这种方式发送附件有什么错误吗?