Esempio n. 1
0
        public TrackingConfig(
            IExecutionContext executionContext,
            IList <RepositoryResource> repositories,
            int buildDirectory)
            : this()
        {
            ArgUtil.NotNull(executionContext, nameof(executionContext));
            ArgUtil.NotNull(repositories, nameof(repositories));

            // Get the repo that we are going to checkout first to create the tracking info from.
            var primaryRepository = RepositoryUtil.GetPrimaryRepository(repositories);

            // Set the directories.
            BuildDirectory       = buildDirectory.ToString(CultureInfo.InvariantCulture);
            ArtifactsDirectory   = Path.Combine(BuildDirectory, Constants.Build.Path.ArtifactsDirectory);
            SourcesDirectory     = Path.Combine(BuildDirectory, Constants.Build.Path.SourcesDirectory);
            TestResultsDirectory = Path.Combine(BuildDirectory, Constants.Build.Path.TestResultsDirectory);

            // Set the other properties.
            CollectionId   = executionContext.Variables.System_CollectionId;
            DefinitionId   = executionContext.Variables.System_DefinitionId;
            RepositoryUrl  = primaryRepository?.Url.AbsoluteUri;
            RepositoryType = primaryRepository?.Type;
            System         = BuildSystem;
            UpdateJobRunProperties(executionContext);

            foreach (var repo in repositories)
            {
                RepositoryTrackingInfo.Add(new Build.RepositoryTrackingInfo(repo, SourcesDirectory));
            }

            // Now that we have all the repositories set up, we can compute the config hash
            HashKey = TrackingConfigHashAlgorithm.ComputeHash(CollectionId, DefinitionId, RepositoryTrackingInfo);
        }
        private bool TryGetRepositoryInfo(IExecutionContext executionContext, out RepositoryInfo repoInfo)
        {
            // Return the matching repository resource and its source provider.
            Trace.Entering();
            var repo = RepositoryUtil.GetPrimaryRepository(executionContext.Repositories);

            repoInfo = new RepositoryInfo
            {
                Repository     = repo,
                SourceProvider = GetSourceProvider(executionContext, repo),
            };

            return(repoInfo.SourceProvider != null);
        }
Esempio n. 3
0
        public void GetPrimaryRepository_should_return_correct_value_when_called()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                var repo1 = new RepositoryResource
                {
                    Alias = "repo1",
                    Id    = "repo1",
                    Type  = "git",
                };

                var repo2 = new RepositoryResource
                {
                    Alias = "repo2",
                    Id    = "repo2",
                    Type  = "git",
                };

                var repoSelf = new RepositoryResource
                {
                    Alias = "self",
                    Id    = "repo3",
                    Type  = "git",
                };

                // No properties set
                Assert.Equal(null, RepositoryUtil.GetPrimaryRepository(null));
                Assert.Equal(repo1, RepositoryUtil.GetPrimaryRepository(new[] { repo1 }));
                Assert.Equal(repo2, RepositoryUtil.GetPrimaryRepository(new[] { repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repoSelf }));
                Assert.Equal(null, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repoSelf, repo1, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repoSelf, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2, repoSelf }));

                // With IsPrimaryRepository set
                repo2.Properties.Set(RepositoryUtil.IsPrimaryRepository, Boolean.TrueString);
                Assert.Equal(repo2, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2, repoSelf }));
                repo2.Properties.Set(RepositoryUtil.IsPrimaryRepository, Boolean.FalseString);
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2, repoSelf }));
            }
        }
Esempio n. 4
0
        public void GetPrimaryRepository_should_return_correct_value_when_called()
        {
            using (TestHostContext hc = new TestHostContext(this))
            {
                Tracing trace = hc.GetTrace();

                var repo1 = new RepositoryResource
                {
                    Alias = "repo1",
                    Id    = "repo1",
                    Type  = "git",
                };

                var repo2 = new RepositoryResource
                {
                    Alias = "repo2",
                    Id    = "repo2",
                    Type  = "git",
                };

                var repoSelf = new RepositoryResource
                {
                    Alias = "self",
                    Id    = "repo3",
                    Type  = "git",
                };

                Assert.Equal(null, RepositoryUtil.GetPrimaryRepository(null));
                Assert.Equal(repo1, RepositoryUtil.GetPrimaryRepository(new[] { repo1 }));
                Assert.Equal(repo2, RepositoryUtil.GetPrimaryRepository(new[] { repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repoSelf }));
                Assert.Equal(null, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repoSelf, repo1, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repoSelf, repo2 }));
                Assert.Equal(repoSelf, RepositoryUtil.GetPrimaryRepository(new[] { repo1, repo2, repoSelf }));
            }
        }
        private bool TryGetRepositoryInfo(IExecutionContext executionContext, out RepositoryInfo repoInfo)
        {
            // Return the matching repository resource and its source provider.
            Trace.Entering();
            repoInfo = new RepositoryInfo();
            var extensionManager = HostContext.GetService <IExtensionManager>();
            List <ISourceProvider> sourceProviders = extensionManager.GetExtensions <ISourceProvider>();

            var primaryRepository = RepositoryUtil.GetPrimaryRepository(executionContext.Repositories);

            if (primaryRepository != null)
            {
                var sourceProvider = sourceProviders.FirstOrDefault(x => string.Equals(x.RepositoryType, primaryRepository.Type, StringComparison.OrdinalIgnoreCase));

                if (sourceProvider != null)
                {
                    repoInfo.Repository     = primaryRepository;
                    repoInfo.SourceProvider = sourceProvider;
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 6
0
        public TrackingConfig PrepareDirectory(
            IExecutionContext executionContext,
            IList <RepositoryResource> repositories,
            WorkspaceOptions workspace)
        {
            // Validate parameters.
            Trace.Entering();
            ArgUtil.NotNull(executionContext, nameof(executionContext));
            ArgUtil.NotNull(executionContext.Variables, nameof(executionContext.Variables));
            ArgUtil.NotNull(repositories, nameof(repositories));

            // Get the primary repository (self)
            var primaryRepository = RepositoryUtil.GetPrimaryRepository(repositories);

            ArgUtil.NotNull(primaryRepository, nameof(primaryRepository));

            // TODO (next PR): We need to modify the Tracking file to handle multiple repositories (currently we are only tracking the self repo)
            var trackingManager = HostContext.GetService <ITrackingManager>();

            // Defer to the source provider to calculate the hash key.
            Trace.Verbose("Calculating build directory hash key.");
            string hashKey = primaryRepository.GetSourceDirectoryHashKey(executionContext);

            Trace.Verbose($"Hash key: {hashKey}");

            // Load the existing tracking file if one already exists.
            string trackingFile = Path.Combine(
                HostContext.GetDirectory(WellKnownDirectory.Work),
                Constants.Build.Path.SourceRootMappingDirectory,
                executionContext.Variables.System_CollectionId,
                executionContext.Variables.System_DefinitionId,
                Constants.Build.Path.TrackingConfigFile);

            Trace.Verbose($"Loading tracking config if exists: {trackingFile}");
            TrackingConfigBase existingConfig = trackingManager.LoadIfExists(executionContext, trackingFile);

            // Check if the build needs to be garbage collected. If the hash key
            // has changed, then the existing build directory cannot be reused.
            TrackingConfigBase garbageConfig = null;

            if (existingConfig != null &&
                !string.Equals(existingConfig.HashKey, hashKey, StringComparison.OrdinalIgnoreCase))
            {
                // Just store a reference to the config for now. It can safely be
                // marked for garbage collection only after the new build directory
                // config has been created.
                Trace.Verbose($"Hash key from existing tracking config does not match. Existing key: {existingConfig.HashKey}");
                garbageConfig  = existingConfig;
                existingConfig = null;
            }

            // Create a new tracking config if required.
            TrackingConfig newConfig;

            if (existingConfig == null)
            {
                Trace.Verbose("Creating a new tracking config file.");
                var agentSetting = HostContext.GetService <IConfigurationStore>().GetSettings();
                newConfig = trackingManager.Create(
                    executionContext,
                    primaryRepository,
                    hashKey,
                    trackingFile,
                    primaryRepository.TestOverrideBuildDirectory(agentSetting));
                ArgUtil.NotNull(newConfig, nameof(newConfig));
            }
            else
            {
                // Convert legacy format to the new format if required.
                newConfig = ConvertToNewFormat(executionContext, primaryRepository, existingConfig);

                // Fill out repository type if it's not there.
                // repository type is a new property introduced for maintenance job
                if (string.IsNullOrEmpty(newConfig.RepositoryType))
                {
                    newConfig.RepositoryType = primaryRepository.Type;
                }

                // For existing tracking config files, update the job run properties.
                Trace.Verbose("Updating job run properties.");
                trackingManager.UpdateJobRunProperties(executionContext, newConfig, trackingFile);
            }

            // Mark the old configuration for garbage collection.
            if (garbageConfig != null)
            {
                Trace.Verbose("Marking existing config for garbage collection.");
                trackingManager.MarkForGarbageCollection(executionContext, garbageConfig);
            }

            // Prepare the build directory.
            // There are 2 ways to provide build directory clean policy.
            //     1> set definition variable build.clean or agent.clean.buildDirectory. (on-prem user need to use this, since there is no Web UI in TFS 2016)
            //     2> select source clean option in definition repository tab. (VSTS will have this option in definition designer UI)
            BuildCleanOption cleanOption = GetBuildDirectoryCleanOption(executionContext, workspace);

            CreateDirectory(
                executionContext,
                description: "build directory",
                path: Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), newConfig.BuildDirectory),
                deleteExisting: cleanOption == BuildCleanOption.All);
            CreateDirectory(
                executionContext,
                description: "artifacts directory",
                path: Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), newConfig.ArtifactsDirectory),
                deleteExisting: true);
            CreateDirectory(
                executionContext,
                description: "test results directory",
                path: Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), newConfig.TestResultsDirectory),
                deleteExisting: true);
            CreateDirectory(
                executionContext,
                description: "binaries directory",
                path: Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), newConfig.BuildDirectory, Constants.Build.Path.BinariesDirectory),
                deleteExisting: cleanOption == BuildCleanOption.Binary);
            CreateDirectory(
                executionContext,
                description: "source directory",
                path: Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), newConfig.SourcesDirectory),
                deleteExisting: cleanOption == BuildCleanOption.Source);

            // Set the default clone path for each repository (the Checkout task may override this later)
            foreach (var repository in repositories)
            {
                var repoPath = GetDefaultRepositoryPath(executionContext, repository, newConfig.SourcesDirectory);
                Trace.Info($"Set repository path for repository {repository.Alias} to '{repoPath}'");
                repository.Properties.Set <string>(RepositoryPropertyNames.Path, repoPath);
            }

            return(newConfig);
        }