/// <summary>
        /// Attaches a file to an issue on the server.
        /// </summary>
        /// <remarks>Uses the REST API <a href="https://www.jetbrains.com/help/youtrack/standalone/Attach-File-to-an-Issue.html">Attach File to an Issue</a>.</remarks>
        /// <param name="issueId">Id of the issue to attach the file to.</param>
        /// <param name="attachmentName">Filename for the attachment.</param>
        /// <param name="attachmentStream">The <see cref="T:System.IO.Stream"/> to attach.</param>
        /// <param name="group">Attachment visibility group.</param>
        /// <param name="author">Creator of the attachment. Note to define author the 'Low-Level Administration' permission is required.</param>
        /// <exception cref="T:System.ArgumentNullException">When the <paramref name="issueId"/>, <paramref name="attachmentName"/> or <paramref name="attachmentStream"/> is null or empty.</exception>
        /// <exception cref="T:System.Net.HttpRequestException">When the call to the remote YouTrack server instance failed.</exception>
        public async Task AttachFileToIssue(string issueId, IAttachFileQueryBuilder builder, Stream attachmentStream)
        {
            if (string.IsNullOrEmpty(issueId))
            {
                throw new ArgumentNullException(nameof(issueId));
            }

            if (attachmentStream == null)
            {
                throw new ArgumentNullException(nameof(attachmentStream));
            }

            var url     = IssuesUrls.AttachFileToIssue(issueId);
            var request = new YouTrackRestRequest(url, Method.POST);

            if (builder != null)
            {
                foreach (var param in builder.GetQueryParameters())
                {
                    request.AddQueryParameter(param.Key, param.Value);
                }
            }

            request.AddHeader("Content-Type", "multipart/form-data");


            var streamContent = new StreamContent(attachmentStream);

            streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = builder.GetAttachmentName(),
                Name     = builder.GetAttachmentName()
            };

            var content = new MultipartFormDataContent
            {
                streamContent
            };

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(content),
                                 ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync(request);

            //var response = await client.PostAsync($"rest/issue/{issueId}/attachment?{query}", content);

            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                //TODO THE
                // Try reading the error message
                //var responseJson = JObject.Parse(await response.Content.ReadAsStringAsync());
                //if (responseJson["value"] != null)
                //{
                //    throw new YouTrackErrorException(responseJson["value"].Value<string>());
                //}
                //else
                //{
                //    throw new YouTrackErrorException(Strings.Exception_UnknownError);
                //}
            }
        }
        public async Task <Comment> AddPullRequestComment(string repositoryName, string ownerName, long id, string content, long?lineFrom = null, long?lineTo = null, string fileName = null, long?parentId = null)
        {
            var url     = EnterpriseApiUrls.PullRequestComments(ownerName, repositoryName, id);
            var request = new YouTrackRestRequest(url, Method.POST);


            var body = new EnterpriseComment()
            {
                Text   = content,
                Parent = parentId.HasValue ? new EnterpriseParent()
                {
                    Id = parentId.Value
                } : null,
                Anchor = fileName != null
                    ? new EnterpriseAnchor()
                {
                    Line       = lineFrom ?? lineTo,
                    FileType   = lineFrom != null && lineTo != null ? (FileDiffType?)null : lineFrom != null ? FileDiffType.From : FileDiffType.To,
                    Path       = fileName,
                    SourcePath = fileName,
                    LineType   = lineFrom != null && lineTo != null ? "CONTEXT" : lineFrom != null ? "REMOVED" : "ADDED",
                } : null
            };

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(body), ParameterType.RequestBody);

            var response = await RestClient.ExecuteTaskAsync <EnterpriseComment>(request);

            return(response.Data.MapTo <Comment>());
        }
        public async Task <Comment> AddPullRequestComment(string repositoryName, string ownerName, long id, string content, long?lineFrom = null, long?lineTo = null, string fileName = null, long?parentId = null)
        {
            var url     = ApiUrls.PullRequestCommentsV1(ownerName, repositoryName, id);
            var request = new YouTrackRestRequest(url, Method.POST);

            var body = new CommentV1()
            {
                Content  = content,
                LineFrom = parentId != null ? null : lineFrom,
                LineTo   = parentId != null ? null : lineTo,
                FileName = fileName,
                ParentId = parentId
            };

            if (body.LineFrom != null)//we can't set both
            {
                body.LineTo = null;
            }

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(body), ParameterType.RequestBody);

            var response = await _versionOneClient.ExecuteTaskAsync <CommentV1>(request);

            return(response.Data.MapTo <Comment>());
        }
        /// <summary>
        /// Updates a comment for an issue on the server.
        /// </summary>
        /// <remarks>Uses the REST API <a href="https://www.jetbrains.com/help/youtrack/standalone/Update-a-Comment.html">Update a Comment</a>.</remarks>
        /// <param name="issueId">Id of the issue to which the comment belongs.</param>
        /// <param name="commentId">Id of the comment to update.</param>
        /// <param name="text">The new text of the comment.</param>
        /// <exception cref="T:System.ArgumentNullException">When the <paramref name="issueId"/>, <paramref name="commentId"/> or <paramref name="text"/> is null or empty.</exception>
        /// <exception cref="T:System.Net.HttpRequestException">When the call to the remote YouTrack server instance failed.</exception>
        public async Task UpdateCommentForIssue(string issueId, string commentId, string text)
        {
            if (string.IsNullOrEmpty(issueId))
            {
                throw new ArgumentNullException(nameof(issueId));
            }

            if (string.IsNullOrEmpty(commentId))
            {
                throw new ArgumentNullException(nameof(commentId));
            }

            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException(nameof(text));
            }

            var myText  = new SimpleText(text);
            var url     = IssuesUrls.UpdateCommentForIssue(issueId, commentId);
            var request = new YouTrackRestRequest(url, Method.PUT);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(myText),
                                 ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync <PullRequest>(request);
        }
        /// <summary>
        /// Updates a user.
        /// </summary>
        /// <remarks>Uses the REST API <a href="https://www.jetbrains.com/help/youtrack/standalone/POST-User.html">Update a user</a>.</remarks>
        /// <param name="username">Login name of the user to be updated.</param>
        /// <param name="fullName">Full name of a user.</param>
        /// <param name="email">E-mail address of the user.</param>
        /// <param name="jabber">Jabber address for the user.</param>
        /// <param name="password">Password for the user.</param>
        /// <exception cref="T:System.Net.HttpRequestException">When the call to the remote YouTrack server instance failed.</exception>
        public async Task UpdateUser(string username, string fullName = null, string email = null, string jabber = null, string password = null)
        {
            var queryString = new Dictionary <string, string>(4);

            if (!string.IsNullOrEmpty(fullName))
            {
                queryString.Add("fullName", WebUtility.UrlEncode(fullName));
            }
            if (!string.IsNullOrEmpty(email))
            {
                queryString.Add("email", WebUtility.UrlEncode(email));
            }
            if (!string.IsNullOrEmpty(jabber))
            {
                queryString.Add("jabber", WebUtility.UrlEncode(jabber));
            }
            if (!string.IsNullOrEmpty(password))
            {
                queryString.Add("password", WebUtility.UrlEncode(password));
            }


            var url     = UsersUrls.GetUser(username);
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(new FormUrlEncodedContent(queryString)), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync(request);
        }
        /// <summary>
        ///
        /// </summary>
        /// <remarks>
        /// <a href="https://www.jetbrains.com/help/youtrack/standalone/Log-in-to-YouTrack.html">Login to YouTrack</a>
        ///  </remarks>
        /// <returns></returns>
        public async Task <User> Login()
        {
            var url     = UserUrls.Login();
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("login", Connection.Credentials.Login);
            request.AddParameter("password", Connection.Credentials.Password);

            var response = await RestClient.ExecuteTaskAsync(request);

            if (!string.Equals(response.Content, "<login>ok</login>", StringComparison.OrdinalIgnoreCase))
            {
                throw new UnauthorizedConnectionException("Could not authenticate. Server did not return expected authentication response. Check the Response property for more details.", response.StatusCode, response.Content);
            }

            return(await GetCurrentUserInfo());
        }
        public async Task MergePullRequest(string repositoryName, string ownerName, MergeRequest mergeRequest)
        {
            var url     = ApiUrls.PullRequestMerge(ownerName, repositoryName, mergeRequest.Id);
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(mergeRequest), ParameterType.RequestBody);
            await RestClient.ExecuteTaskAsync(request);
        }
        /// <summary>
        /// Add user to group.
        /// </summary>
        /// <remarks>Uses the REST API <a href="https://www.jetbrains.com/help/youtrack/standalone/POST-User-Group.html">Add user account to a group</a>.</remarks>
        /// <param name="username">Login name of the user to be updated.</param>
        /// <param name="group">Name of the group to add the user to.</param>
        /// <exception cref="T:System.Net.HttpRequestException">When the call to the remote YouTrack server instance failed.</exception>
        public async Task AddUserToGroup(string username, string group)
        {
            var url     = $"rest/admin/user/{username}/group/{WebUtility.UrlEncode(group)}";
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(new StringContent(string.Empty)), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync(request);
        }
        /// <summary>
        /// Merge users.
        /// </summary>
        /// <remarks>Uses the REST API <a href="https://www.jetbrains.com/help/youtrack/standalone/Merge-Users.html">Delete a user</a>.</remarks>
        /// <param name="usernameToMerge">Login name of the user to be merged.</param>
        /// <param name="targetUser">Login name of the user to merge <paramref name="usernameToMerge"/> into.</param>
        /// <exception cref="T:System.Net.HttpRequestException">When the call to the remote YouTrack server instance failed.</exception>
        public async Task MergeUsers(string usernameToMerge, string targetUser)
        {
            var url     = $"/admin/user/{targetUser}/merge/{usernameToMerge}";
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(new StringContent(string.Empty)), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync(request);
        }
        public async Task <Repository> CreateRepository(Repository repository)
        {
            var url     = ApiUrls.Repository(Connection.Credentials.Login, repository.Name);
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(repository), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync <Repository>(request);

            return(response.Data);
        }
        public async Task CreatePullRequest(PullRequest pullRequest, string repositoryName, string owner)
        {
            //pullRequest.Author = new User()
            //{
            //    Username = Connection.Credentials.Login
            //};
            var url     = EnterpriseApiUrls.PullRequests(owner, repositoryName);
            var request = new YouTrackRestRequest(url, Method.POST);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(pullRequest.MapTo <EnterprisePullRequest>()), ParameterType.RequestBody);
            await RestClient.ExecuteTaskAsync(request);
        }
        public async Task UpdatePullRequest(PullRequest pullRequest, string repoName, string owner)
        {
            //pullRequest.Author = new User()
            //{
            //    Username = Connection.Credentials.Login
            //};

            var url     = ApiUrls.PullRequest(owner, repoName, pullRequest.Id);
            var request = new YouTrackRestRequest(url, Method.PUT);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(pullRequest), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync <PullRequest>(request);
        }
        public async Task UpdatePullRequest(PullRequest pullRequest, string repoName, string owner)
        {
            //pullRequest.Author = new User()
            //{
            //    Username = Connection.Credentials.Login
            //};

            pullRequest.Destination = null;//throws exception if the same dest is set. Unless we allow to change it, leave it null

            var url     = EnterpriseApiUrls.PullRequest(owner, repoName, pullRequest.Id);
            var request = new YouTrackRestRequest(url, Method.PUT);

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(pullRequest.MapTo <EnterprisePullRequest>()), ParameterType.RequestBody);
            await RestClient.ExecuteTaskAsync(request);
        }
Beispiel #14
0
        public async Task <Repository> CreateRepository(Repository repository)
        {
            var url            = EnterpriseApiUrls.CreateRepositories(Connection.Credentials.Login);
            var request        = new YouTrackRestRequest(url, Method.POST);
            var enterpriseRepo = new EnterpriseRepository()
            {
                Name     = repository.Name,
                IsPublic = !repository.IsPrivate
            };

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(enterpriseRepo), ParameterType.RequestBody);
            var response = await RestClient.ExecuteTaskAsync <EnterpriseRepository>(request);

            return(response.Data.MapTo <Repository>());
        }
        public async Task <Comment> EditPullRequestComment(string repositoryName, string ownerName, long pullRequestId, long id, string content, long commentVersion)
        {
            var url     = ApiUrls.PullRequestCommentV1(ownerName, repositoryName, pullRequestId, id);
            var request = new YouTrackRestRequest(url, Method.PUT);

            var body = new CommentV1()
            {
                Content = content
            };

            request.AddParameter("application/json; charset=utf-8", request.JsonSerializer.Serialize(body), ParameterType.RequestBody);

            var response = await _versionOneClient.ExecuteTaskAsync <CommentV1>(request);

            return(response.Data.MapTo <Comment>());
        }