in src/YouTrackSharp/Issues/IssuesService.Attachments.cs [15:81]
public async Task AttachFileToIssue(string issueId, string attachmentName, Stream attachmentStream, string group = null, string author = null, string attachmentContentType = null)
{
if (string.IsNullOrEmpty(issueId))
{
throw new ArgumentNullException(nameof(issueId));
}
if (string.IsNullOrEmpty(attachmentName))
{
throw new ArgumentNullException(nameof(attachmentName));
}
if (attachmentStream == null)
{
throw new ArgumentNullException(nameof(attachmentStream));
}
var client = await _connection.GetAuthenticatedApiClient();
var attachment = new IssueAttachment();
if (!string.IsNullOrEmpty(attachmentName))
{
attachment.Name = attachmentName;
}
if (!string.IsNullOrEmpty(group))
{
var visibilityGroups = await client.VisibilitygroupsAsync(
"groupsWithoutRecommended(id,name),recommendedGroups(id,name)", 0, -1,
new VisibilityGroupsRequest()
{
Prefix = group.ToLower(),
Top = 1000000000,
Issues = new List<Generated.Issue>() {new Generated.Issue() {IdReadable = issueId}}
});
var userGroup =
visibilityGroups.RecommendedGroups.FirstOrDefault(g =>
g.Name.Equals(group, StringComparison.InvariantCultureIgnoreCase)) ??
visibilityGroups.GroupsWithoutRecommended.FirstOrDefault(g =>
g.Name.Equals(group, StringComparison.InvariantCultureIgnoreCase));
if (userGroup == null)
{
throw new YouTrackErrorException(Strings.Exception_BadRequest, (int)HttpStatusCode.BadRequest,
$"Could not suggest visibility group with name {group} for issue {issueId}",
null, null);
}
attachment.Visibility = new LimitedVisibility() {PermittedGroups = new List<UserGroup> {userGroup}};
}
if (!string.IsNullOrEmpty(author))
{
attachment.Author = new Me() {Login = author};
}
if (!string.IsNullOrEmpty(attachmentContentType))
{
attachment.MimeType = attachmentContentType;
}
var response = await client.IssuesAttachmentsPostAsync(issueId, false, "id",
new FileParameter(attachmentStream, attachmentName, attachmentContentType));
var apiAttachment = response.FirstOrDefault();
if (apiAttachment == null)
{
throw new YouTrackErrorException(Strings.Exception_BadRequest, (int)HttpStatusCode.OK,
$"Failed to upload attachment: server returned HTTP OK with empty response",
null, null);
}
await client.IssuesAttachmentsPostAsync(issueId, apiAttachment.Id, "id", attachment);
}