Esempio n. 1
0
        public async Task <List <VSTSChange> > GetChangesetDetails(int changesetId)
        {
            TfvcChangeset changeset = null;

            TfvcHttpClient vsClient = Conn.GetClient <TfvcHttpClient>();

            changeset = await vsClient.GetChangesetAsync(changesetId, null, true, true, null, true, null, null, null, null, null);

            List <TfvcChange> tfvchanges = await vsClient.GetChangesetChangesAsync(changesetId);

            List <VSTSChange> changes = new List <VSTSChange>();

            foreach (var tfvchange in tfvchanges)
            {
                VSTSChange change = new VSTSChange()
                {
                    ChangeType = tfvchange.ChangeType.ToString(),
                    FilePath   = tfvchange.Item.Path
                };

                changes.Add(change);
            }

            return(changes);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public void ChangeItemContent(SourceInformation sourceInformation, TfvcItem existingItem, string content)
        {
            Validators.AssertIsNotNull(sourceInformation, nameof(sourceInformation));
            Validators.AssertIsNotNull(existingItem, nameof(existingItem));
            Validators.AssertIsNotNullOrEmpty(content, nameof(content));

            Logger.Trace("Entering");

            sourceInformation.AssertIsValid();
            sourceInformation.AssertIsSourceType(SourceType.TfsVc);

            var isAdd = existingItem == null;

            var item = new TfvcItem
            {
                Path            = sourceInformation.SourcePath,
                ContentMetadata = new FileContentMetadata {
                    Encoding = 65001
                }
            };

            if (!isAdd)
            {
                item.ChangesetVersion = existingItem.ChangesetVersion;
            }

            var change = new TfvcChange
            {
                ChangeType = isAdd
                    ? VersionControlChangeType.Add
                    : VersionControlChangeType.Edit,
                NewContent = new ItemContent
                {
                    Content     = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)),
                    ContentType = ItemContentType.Base64Encoded
                },
                Item = item
            };

            var changeset = new TfvcChangeset
            {
                Comment = "Automatically "
                          + (isAdd
                              ? "Added"
                              : "Updated")
                          + " from API",
                Changes = new List <TfvcChange> {
                    change
                }
                //PolicyOverride = new TfvcPolicyOverrideInfo("API", null),
            };

            // submit the changeset
            var result = _client.Value.CreateChangesetAsync(
                changeset,
                VsTsTool.GetProjectNameFromPath(sourceInformation.SourcePath))
                         .Result;

            Console.WriteLine($"Changeset created for Add/Update. Id: {result.ChangesetId}.");
        }
        public TfvcChangesetRef CreateChange()
        {
            VssConnection  connection = this.Context.Connection;
            TfvcHttpClient tfvcClient = connection.GetClient <TfvcHttpClient>();

            string   projectName             = ClientSampleHelpers.FindAnyProject(this.Context).Name;
            DateTime time                    = DateTime.UtcNow;
            string   destinationFilePath     = string.Format("$/{0}/example-file-{1}.txt", projectName, time.ToString("yyyy-MM-dd-HH-mm-ss-ff"));
            string   destinationFileContents = string.Format("File contents as of {0}", time);

            TfvcChangeset changeset = new TfvcChangeset()
            {
                Changes = new[]
                {
                    new TfvcChange()
                    {
                        ChangeType = VersionControlChangeType.Add,
                        Item       = new TfvcItem()
                        {
                            Path            = destinationFilePath,
                            ContentMetadata = new FileContentMetadata()
                            {
                                Encoding    = Encoding.UTF8.WindowsCodePage,
                                ContentType = "text/plain",
                            }
                        },
                        NewContent = new ItemContent()
                        {
                            Content     = destinationFileContents,
                            ContentType = ItemContentType.RawText,
                        },
                    },
                },
                Comment = "(sample) Adding a new changeset via API",
            };

            try
            {
                TfvcChangesetRef changesetRef = tfvcClient.CreateChangesetAsync(changeset).Result;
                Console.WriteLine("{0} by {1}: {2}", changesetRef.ChangesetId, changesetRef.Author.DisplayName, changesetRef.Comment ?? "<no comment>");
                return(changesetRef);
            }
            catch (AggregateException e)
            {
                Console.WriteLine("Something went wrong, could not create TFVC changeset.");
                if (e.InnerException.Message.Contains(projectName))
                {
                    Console.WriteLine("This may mean project \"{0}\" isn't configured for TFVC.", projectName);
                    Console.WriteLine("Add a TFVC repo to the project, then try this sample again.");
                }
                else
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }

            return(null);
        }
Esempio n. 4
0
        /// <inheritdoc />
        public void DeleteItem(SourceInformation sourceInformation, TfvcItem existingItem)
        {
            Validators.AssertIsNotNull(sourceInformation, nameof(sourceInformation));
            Validators.AssertIsNotNull(existingItem, nameof(existingItem));

            sourceInformation.AssertIsValid();
            sourceInformation.AssertIsSourceType(SourceType.TfsVc);

            Logger.Trace("Entering");

            var item = new TfvcItem
            {
                Path            = sourceInformation.SourcePath,
                ContentMetadata = new FileContentMetadata {
                    Encoding = 65001
                },
                ChangesetVersion = existingItem.ChangesetVersion
            };

            var change = new TfvcChange
            {
                ChangeType = VersionControlChangeType.Delete,
                Item       = item
            };

            var changeset = new TfvcChangeset
            {
                Comment = "Automatically deleted from API",
                Changes = new List <TfvcChange> {
                    change
                }
                //PolicyOverride = new TfvcPolicyOverrideInfo("API", null),
            };

            // submit the changeset
            var result = _client.Value.CreateChangesetAsync(
                changeset,
                VsTsTool.GetProjectNameFromPath(sourceInformation.SourcePath))
                         .Result;

            Console.WriteLine($"Changeset created for delete. Id: {result.ChangesetId}.");
        }
        public TfvcChangesetRef EditExistingFile()
        {
            VssConnection  connection = this.Context.Connection;
            TfvcHttpClient tfvcClient = connection.GetClient <TfvcHttpClient>();

            // first, create a file we know is safe to edit
            string   projectName          = ClientSampleHelpers.FindAnyProject(this.Context).Name;
            DateTime time                 = DateTime.UtcNow;
            string   destinationFilePath  = string.Format("$/{0}/file-to-edit-{1}.txt", projectName, time.ToString("yyyy-MM-dd-HH-mm-ss-ff"));
            string   originalFileContents = string.Format("Initial contents as of {0}", time);

            TfvcChangeset createFile = new TfvcChangeset()
            {
                Changes = new[]
                {
                    new TfvcChange()
                    {
                        ChangeType = VersionControlChangeType.Add,
                        Item       = new TfvcItem()
                        {
                            Path            = destinationFilePath,
                            ContentMetadata = new FileContentMetadata()
                            {
                                Encoding    = Encoding.UTF8.WindowsCodePage,
                                ContentType = "text/plain",
                            }
                        },
                        NewContent = new ItemContent()
                        {
                            Content     = originalFileContents,
                            ContentType = ItemContentType.RawText,
                        },
                    },
                },
                Comment = "(sample) Adding a file which we'll later edit",
            };

            TfvcChangesetRef createFileRef;

            try
            {
                createFileRef = tfvcClient.CreateChangesetAsync(createFile).Result;
                Console.WriteLine("{0} by {1}: {2}", createFileRef.ChangesetId, createFileRef.Author.DisplayName, createFileRef.Comment ?? "<no comment>");
            }
            catch (AggregateException e)
            {
                Console.WriteLine("Something went wrong, could not create TFVC changeset.");
                if (e.InnerException.Message.Contains(projectName))
                {
                    Console.WriteLine("This may mean project \"{0}\" isn't configured for TFVC.", projectName);
                    Console.WriteLine("Add a TFVC repo to the project, then try this sample again.");
                }
                else
                {
                    Console.WriteLine(e.InnerException.Message);
                }
                return(null);
            }

            // now edit the file contents
            string        editedFileContents = originalFileContents + "\nEdited contents";
            TfvcChangeset changeset          = new TfvcChangeset()
            {
                Changes = new[]
                {
                    new TfvcChange()
                    {
                        ChangeType = VersionControlChangeType.Edit,
                        Item       = new TfvcItem()
                        {
                            Path            = destinationFilePath,
                            ContentMetadata = new FileContentMetadata()
                            {
                                Encoding    = Encoding.UTF8.WindowsCodePage,
                                ContentType = "text/plain",
                            },
                            // must tell the API what version we want to change
                            ChangesetVersion = createFileRef.ChangesetId,
                        },
                        NewContent = new ItemContent()
                        {
                            Content     = editedFileContents,
                            ContentType = ItemContentType.RawText,
                        },
                    },
                },
                Comment = "(sample) Editing the file via API",
            };

            try
            {
                TfvcChangesetRef changesetRef = tfvcClient.CreateChangesetAsync(changeset).Result;
                Console.WriteLine("{0} by {1}: {2}", changesetRef.ChangesetId, changesetRef.Author.DisplayName, changesetRef.Comment ?? "<no comment>");
                return(changesetRef);
            }
            catch (AggregateException e)
            {
                Console.WriteLine("Something went wrong, could not create TFVC changeset.");
                if (e.InnerException.Message.Contains(projectName))
                {
                    Console.WriteLine("This may mean project \"{0}\" isn't configured for TFVC.", projectName);
                    Console.WriteLine("Add a TFVC repo to the project, then try this sample again.");
                }
                else
                {
                    Console.WriteLine(e.InnerException.Message);
                }
            }

            return(null);
        }