コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: BuildHelper.cs プロジェクト: vzabrodin/RocketBuild
        public async Task <List <DisplayBuild> > GetBuildsAsync()
        {
            var client = VssClientHelper.GetClient <BuildHttpClient>(
                Settings.GlobalSettings.Current.AccountUrl,
                Settings.GlobalSettings.Current.ApiKey,
                Settings.GlobalSettings.Current.UseSsl);

            var tfsClient = VssClientHelper.GetClient <TfvcHttpClient>(
                Settings.GlobalSettings.Current.AccountUrl,
                Settings.GlobalSettings.Current.ApiKey,
                Settings.GlobalSettings.Current.UseSsl);

            List <BuildDefinitionReference> definitions = await client.GetDefinitionsAsync(Settings.GlobalSettings.Current.Project, name : null);

            var tfsBuilds = await client.GetBuildsAsync(Settings.GlobalSettings.Current.Project, definitions : definitions.Select(d => d.Id), maxBuildsPerDefinition : 1, queryOrder : BuildQueryOrder.FinishTimeDescending);

            var builds = new List <DisplayBuild>();

            TfvcChangesetRef changeset = (await tfsClient.GetChangesetsAsync(Settings.GlobalSettings.Current.Project, top: 1)).FirstOrDefault();

            foreach (BuildDefinitionReference definition in definitions)
            {
                var    lastTfsBuild     = tfsBuilds.FirstOrDefault(b => b.Definition.Id == definition.Id);
                string lastTfsBuildLink = (lastTfsBuild?.Links.Links["web"] as ReferenceLink)?.Href;

                builds.Add(new DisplayBuild
                {
                    DefinitionId    = definition.Id,
                    Path            = definition.Path.TrimStart('\\'),
                    Name            = definition.Name,
                    LastBuild       = lastTfsBuild != null ? Regex.Match(lastTfsBuild.BuildNumber, Settings.GlobalSettings.Current.BuildNameExtractVersionRegex).Value : String.Empty,
                    LastBuildStatus = (DisplayBuildStatus?)lastTfsBuild?.Status,
                    LastBuildResult = (BuildResult?)lastTfsBuild?.Result,
                    LastBuildLink   = lastTfsBuildLink,
                    LastCheckin     = changeset?.ChangesetId.ToString()
                });
            }

            return(builds);
        }
コード例 #3
0
        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);
        }