Eugene Podskal
Eugene Podskal

Reputation: 10401

Is there a way to create git push/commit with linked workitems through TFS client libraries/API in a single operation?

I want to create a push using TFS git client libraries that contains the specified workitems.

The code is basically:

public static async Task<GitPush> CreatePush(
    this GitHttpClient git,
    GitRepository repo,
    GitRefUpdate branchUpdateRef,
    GitChange change,
    String comment,  
    IEnumerable<Int32> workitems = null)
{
    workitems = (workitems ?? Enumerable.Empty<Int32>()).ToList();
    var commit = new GitCommitRef
    {
        Comment = comment,
        Changes = new[]
        {
            change
        },
        WorkItems = workitems
            .Select(wi => new ResourceRef
            {
                Id = wi.ToString() 
                // Perhaps one should also set an URL of some kind
            })
            .ToList()
    };
    var push = new GitPush
    {
        Commits = new[]
        {
            commit
        },
        RefUpdates = new[]
        {
            branchUpdateRef
        },
        Repository = repo
    };
    return await git.CreatePushAsync(
        push, 
        project: repo.ProjectReference.Id,
        repositoryId: repo.Id);
}

The push is created successfully, though there are no workitem links on the created commit.

I have tried

  1. Setting the URL to the one from https://learn.microsoft.com/en-us/rest/api/vsts/wit/work%20items/get%20work%20item?view=vsts-rest-4.1#workitem .
  2. Settings it to the slightly different workitem URL that is actually returned in GitCommitRef.WorkItems ResourceRef for commits that have related workitems.

But to no avail.

I actually can achieve the desired final result by

  1. Either appending $"#{workitemId}" to the commit comment string
  2. Or by adding commit link to the workitem in question

Still, both of those solutions have minor drawbacks(1 - changing commit message, 2 - possible race condition with CI and etc.) that I'd prefer to avoid.

Basically, is there a way to way to create GitPush with associated workitems in a single operation, or do I have to use the aforementioned workarounds?

Upvotes: 0

Views: 470

Answers (1)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

No, there isn't any way to achieve this. Even when you create a commit/push from TFS Web UI, TFS still create the commit/push first and then update the work item to link to the push.

Upvotes: 1

Related Questions