microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
523 stars 517 forks source link

CreateAnnotatedTagAsync sample #295

Closed TalalYousif closed 4 years ago

TalalYousif commented 4 years ago

Can you please provide a sample for CreateAnnotatedTagAsync method?

I keep getting "VS30063: You are not authorized to access https://dev.azure.com" even though my credentials are valid.

Please take a look at this:

        var vssConnection = new VssConnection(new Uri("ORG_URI"),  new VssBasicCredential(string.Empty, "PAT"));

        var gitClient = vssConnection.GetClient<GitHttpClient>();

        var tag = new GitAnnotatedTag
        {
            Name = "tagname",
            Message = "A new tag",
            TaggedBy = new GitUserDate
            {
                Name = "Name",
                Email = "Email",
                Date = DateTime.Now,
                ImageUrl = null
            },
            ObjectId = "SHA",
            TaggedObject = new GitObject
            {
                ObjectId = "SHA",
                ObjectType = GitObjectType.Commit
            },
            Url = string.Empty
        };

        var sourceRepo = await gitClient.GetRepositoryAsync("PROJECT", repoName);

        var tags = await gitClient.GetTagRefsAsync(sourceRepo.Id);

        // This works fine and a tag is displayed
        Console.WriteLine(tags.First().Name);
       // This throws "VS30063: You are not authorized to access https://dev.azure.com"
       await gitClient.CreateAnnotatedTagAsync(tag, "PROJECT", sourceRepo.Id);