Exemple #1
0
        private async Task DownloadCommits(
            IExecutionContext executionContext,
            Guid teamProjectId,
            IList <AgentArtifactDefinition> agentArtifactDefinitions)
        {
            Trace.Entering();

            Trace.Info("Creating commit work folder");
            string commitsWorkFolder = GetCommitsWorkFolder(executionContext);

            // Note: We are having an explicit type here. For other artifact types we are planning to go with tasks
            // Only for jenkins we are making the agent to download
            var             extensionManager = HostContext.GetService <IExtensionManager>();
            JenkinsArtifact jenkinsExtension = (extensionManager.GetExtensions <IArtifactExtension>()).FirstOrDefault(x => x.ArtifactType == AgentArtifactType.Jenkins) as JenkinsArtifact;

            foreach (AgentArtifactDefinition agentArtifactDefinition in agentArtifactDefinitions)
            {
                if (agentArtifactDefinition.ArtifactType == AgentArtifactType.Jenkins)
                {
                    Trace.Info($"Found supported artifact {agentArtifactDefinition.Alias} for downloading commits");
                    ArtifactDefinition artifactDefinition = ConvertToArtifactDefinition(agentArtifactDefinition, executionContext, jenkinsExtension);
                    await jenkinsExtension.DownloadCommitsAsync(executionContext, artifactDefinition, commitsWorkFolder);
                }
            }
        }
Exemple #2
0
        public async void CommitsShouldBeUploadedAsAttachment()
        {
            using (TestHostContext tc = Setup())
            {
                string commitRootDirectory = Path.Combine(tc.GetDirectory(WellKnownDirectory.Work), Guid.NewGuid().ToString("D"));
                Directory.CreateDirectory(commitRootDirectory);

                try
                {
                    JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                    details.StartCommitArtifactVersion = "10";
                    details.EndCommitArtifactVersion   = "20";

                    var artifact = new JenkinsArtifact();
                    artifact.Initialize(tc);

                    SetupBuildRangeQuery(details, "{ \"allBuilds\": [{ \"number\": 20 }, { \"number\": 10 }, { \"number\": 2 } ] }");
                    string commitResult = " {\"builds\": [{ \"number\":9, \"result\":\"SUCCESS\", \"changeSet\": { \"items\": [{ \"commitId\" : \"2869c7ccd0b1b649ba6765e89ee5ff36ef6d4805\", \"author\": { \"fullName\" : \"testuser\" }, \"msg\":\"test\" }]}}]}";
                    string commitsUrl   = $"{details.Url}/job/{details.JobName}/api/json?tree=builds[number,result,changeSet[items[commitId,date,msg,author[fullName]]]]{{0,1}}";
                    _httpClient.Setup(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(commitsUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
                    .Returns(Task.FromResult(commitResult));

                    string commitFilePath = Path.Combine(commitRootDirectory, $"commits_{details.Alias}_1.json");

                    await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, commitRootDirectory);

                    _ec.Verify(x => x.QueueAttachFile(It.Is <string>(y => y.Equals(CoreAttachmentType.FileAttachment)), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
                }
                finally
                {
                    IOUtil.DeleteDirectory(commitRootDirectory, CancellationToken.None);
                }
            }
        }
Exemple #3
0
        public async void IfNoCommitVersionExistsInArtifactDetailsNoIssueShouldBeAdded()
        {
            using (TestHostContext tc = Setup())
            {
                var trace = tc.GetTrace();

                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);
                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, "test");

                _ec.Verify(x => x.AddIssue(It.Is <Issue>(y => y.Type == IssueType.Warning)), Times.Never);
            }
        }
Exemple #4
0
        public async void MissingStartVersionShouldDownloadCommitsFromSingleBuild()
        {
            using (TestHostContext tc = Setup())
            {
                JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                details.EndCommitArtifactVersion = "10";

                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);
                string expectedUrl = $"{details.Url}/job/{details.JobName}/{details.EndCommitArtifactVersion}/api/json?tree=number,result,changeSet[items[commitId,date,msg,author[fullName]]]";
                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, tc.GetDirectory(WellKnownDirectory.Root));

                _httpClient.Verify(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(expectedUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
            }
        }
Exemple #5
0
        public async void ShouldLogAnIssueIfEndVersionIsInvalidInArtifactDetail()
        {
            using (TestHostContext tc = Setup())
            {
                var trace = tc.GetTrace();

                JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                details.EndCommitArtifactVersion = "xx";
                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);
                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, "test");

                _ec.Verify(x => x.AddIssue(It.Is <Issue>(y => y.Type == IssueType.Warning)), Times.Once);
            }
        }
Exemple #6
0
        public async void JenkinsCommitsShouldLogAnIssueIfBuildIsDeleted()
        {
            using (TestHostContext tc = Setup())
            {
                JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                details.StartCommitArtifactVersion = "10";
                details.EndCommitArtifactVersion   = "20";

                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);
                SetupBuildRangeQuery(details, "{ \"allBuilds\": [{ \"number\": 30 }, { \"number\": 29 }, { \"number\": 28 } ] }");

                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, tc.GetDirectory(WellKnownDirectory.Root));

                _ec.Verify(x => x.AddIssue(It.Is <Issue>(y => y.Type == IssueType.Warning)), Times.Once);
            }
        }
Exemple #7
0
        public async void JenkinsCommitsShouldBeFetchedBetweenBuildRange()
        {
            using (TestHostContext tc = Setup())
            {
                JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                details.StartCommitArtifactVersion = "10";
                details.EndCommitArtifactVersion   = "20";

                var artifact = new JenkinsArtifact();
                artifact.Initialize(tc);

                SetupBuildRangeQuery(details, "{ \"allBuilds\": [{ \"number\": 20 }, { \"number\": 10 }, { \"number\": 2 } ] }");
                string expectedUrl = $"{details.Url}/job/{details.JobName}/api/json?tree=builds[number,result,changeSet[items[commitId,date,msg,author[fullName]]]]{{0,1}}";

                await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, tc.GetDirectory(WellKnownDirectory.Root));

                _httpClient.Verify(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(expectedUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once);
            }
        }
        public async void CommitsShoulHaveUrlIfItsGitRepo()
        {
            using (TestHostContext tc = Setup())
            {
                string commitRootDirectory = tc.GetDirectory(WellKnownDirectory.Root);

                try
                {
                    JenkinsArtifactDetails details = _artifactDefinition.Details as JenkinsArtifactDetails;
                    details.StartCommitArtifactVersion = "10";
                    details.EndCommitArtifactVersion   = "20";

                    var artifact = new JenkinsArtifact();
                    artifact.Initialize(tc);

                    SetupBuildRangeQuery(details, "{ \"allBuilds\": [{ \"number\": 20 }, { \"number\": 10 }, { \"number\": 2 } ] }");
                    string commitResult = " {\"builds\": [{ \"number\":9, \"result\":\"SUCCESS\", \"changeSet\": { \"items\": [{ \"commitId\" : \"2869c7ccd0b1b649ba6765e89ee5ff36ef6d4805\", \"author\": { \"fullName\" : \"testuser\" }, \"msg\":\"test\" }]}}]}";
                    string commitsUrl   = $"{details.Url}/job/{details.JobName}/api/json?tree=builds[number,result,changeSet[items[commitId,date,msg,author[fullName]]]]{{0,1}}";
                    _httpClient.Setup(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(commitsUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
                    .Returns(Task.FromResult(commitResult));

                    string repoUrl    = $"{details.Url}/job/{details.JobName}/{details.EndCommitArtifactVersion}/api/json?tree=actions[remoteUrls],changeSet[kind]";
                    string repoResult = "{ \"actions\": [ { \"remoteUrls\": [ \"https://github.com/TestUser/TestRepo\" ] }, ], \"changeSet\": { \"kind\": \"git\" } }";
                    _httpClient.Setup(x => x.GetStringAsync(It.Is <string>(y => y.StartsWith(repoUrl)), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
                    .Returns(Task.FromResult(repoResult));

                    string commitFilePath = Path.Combine(commitRootDirectory, $"commits_{details.Alias}_1.json");
                    Directory.CreateDirectory(commitRootDirectory);

                    string expectedCommitUrl = "https://github.com/TestUser/TestRepo/commit/2869c7ccd0b1b649ba6765e89ee5ff36ef6d4805";
                    await artifact.DownloadCommitsAsync(_ec.Object, _artifactDefinition, commitRootDirectory);

                    _ec.Verify(x => x.QueueAttachFile(It.Is <string>(y => y.Equals(CoreAttachmentType.FileAttachment)), It.IsAny <string>(), It.Is <string>(z => string.Join("", File.ReadAllLines(z)).Contains(expectedCommitUrl))), Times.Once);
                }
                finally
                {
                    IOUtil.DeleteDirectory(commitRootDirectory, CancellationToken.None);
                }
            }
        }