JetBrains / YouTrackSharp

.NET Standard 2.0 Library to access YouTrack API.
https://www.jetbrains.com/youtrack
Apache License 2.0
135 stars 105 forks source link

CreateWorkItemForIssue fails with Bad Request error #120

Open labsilva opened 2 years ago

labsilva commented 2 years ago

Expected behavior

Using the YouTrackSharp Nuget package (version 2021.3.6) to create a work item for an existing issue should correctly create the work item in the issue.

YouTrack server on-prem, Build 2022.2.53793, Fri, Aug 19, 2022.

Actual behavior

The request fails with the following error:

YouTrackSharp.Generated.YouTrackErrorException : The HTTP status code of the response was not expected (400).

Status: 400
Response: 
{"error":"Bad Request","error_description":"YouTrack is unable to locate an Me-type entity unless its ID is also provided","error_developer_message":"YouTrack is unable to locate an Me-type entity unless its ID is also provided"}

Could not find any reference to this error on my Google searches.

I've triple-checked that the data supplied on the method call is valid and everything "exists" on our YouTrack instance. The bearer token used is valid and has the necessary service scopes for the operation.

Steps to reproduce the behavior

The following Xunit test triggers the error, you'll just have to supply valid values to the configuration options:

public class YouTrackWorkItemTests
    {
        private readonly BearerTokenConnection connection;

        public YouTrackWorkItemTests()
        {
            connection = new BearerTokenConnection(
                "<youtrack instance url>",
                "<bearer token>"
            );
        }

        [Fact]
        public async Task ShouldCreateTimeTrackingWorkItem()
        {
            // Arrange
            var issueService = connection.CreateIssuesService();
            var timeTrackingService = connection.CreateTimeTrackingService();

            var issueId = "<an existing issue id from a project with time tracking enabled>";
            var description = "<any description will do>";
            var duration = TimeSpan.FromHours(1);
            var author = "<a valid user name with access to the project>";

            // Act
            var issue = await issueService.GetIssue(issueId).ConfigureAwait(false);
            var workItemTypes = await timeTrackingService.GetWorkTypesForProject(issue.GetField("projectShortName").AsString()).ConfigureAwait(false);

            var workItem = new WorkItem(
                    DateTime.UtcNow,
                    duration,
                    description,
                    workItemTypes.First(),
                    new Author { Login = author });
            var workItemId = await timeTrackingService.CreateWorkItemForIssue(issue.Id, workItem);

            // Assert
            workItemId
                .Should()
                .NotBeNullOrEmpty()
                .And
                .NotBeNullOrWhiteSpace();

        }
    }

Any help is appreciated! Thanks!