Пример #1
0
        public void BitbucketHandlerIgnoresNonBitbucketPayloads()
        {
            // Arrange
            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: null, targetBranch: null, deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.UnknownPayload, result);
        }
Пример #2
0
        public void BitbucketHandlerReturnsNoOpForCommitsThatAreNotTheTargetBranch()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [ { ""author"": ""pranavkm"", ""branch"": ""default"", ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Some more changes"", ""node"": ""0bbefd70c4c4"", ""parents"": [ ""3cb8bf8aec0a"" ], ""raw_author"": ""Pranav <*****@*****.**>"", ""raw_node"": ""0bbefd70c4c4213bba1e91998141f6e861cec24d"", ""revision"": 4, ""size"": -1, ""timestamp"": ""2012-12-17 19:41:28"", ""utctimestamp"": ""2012-12-17 18:41:28+00:00"" } ], ""repository"": { ""absolute_url"": ""/kudutest/hellomercurial/"", ""fork"": false, ""is_private"": false, ""name"": ""HelloMercurial"", ""owner"": ""kudutest"", ""scm"": ""hg"", ""slug"": ""hellomercurial"", ""website"": """" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "not-default", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.NoOp, result);
            Assert.Null(deploymentInfo);
        }
Пример #3
0
        public void BitbucketHandlerParsesBitbucketPayloadsForGitRepositoriesWithMultipleCommits()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [ { ""author"": ""Pranav K"", ""branch"": null, ""branches"": [], ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Changes 1\n"", ""node"": ""176a1c27dde2"", ""parents"": [ ""0b418c5fd473"" ], ""raw_author"": ""Pranav K <*****@*****.**>"", ""raw_node"": ""176a1c27dde2d397a69dd859cb6df8087403a07a"", ""revision"": null, ""size"": -1, ""timestamp"": ""2012-12-17 23:31:14"", ""utctimestamp"": ""2012-12-17 22:31:14+00:00"" }, { ""author"": ""Pranav K"", ""branch"": ""foo"", ""files"": [ { ""file"": ""Foo.txt"", ""type"": ""added"" } ], ""message"": ""Foo commit\n"", ""node"": ""f94996d67d6d"", ""parents"": [ ""e689ee0adcb0"" ], ""raw_author"": ""Pranav K <*****@*****.**>"", ""raw_node"": ""f94996d67d6d5a060aaf2fcb72c333d0899549ab"", ""revision"": null, ""size"": -1, ""timestamp"": ""2012-12-17 23:32:20"", ""utctimestamp"": ""2012-12-17 22:32:20+00:00"" }, { ""author"": ""Pranav K"", ""branch"": ""master"", ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Some changes to Hello\n"", ""node"": ""d3bde12dfe11"", ""parents"": [ ""176a1c27dde2"" ], ""raw_author"": ""Pranav K <*****@*****.**>"", ""raw_node"": ""d3bde12dfe11206173fa940b3e50b135e9ae1677"", ""revision"": null, ""size"": -1, ""timestamp"": ""2012-12-17 23:32:44"", ""utctimestamp"": ""2012-12-17 22:32:44+00:00"" } ], ""repository"": { ""absolute_url"": ""/kudutest/mypublicrepo/"", ""fork"": false, ""is_private"": false, ""name"": ""MyPublicRepo"", ""owner"": ""kudutest"", ""scm"": ""git"", ""slug"": ""mypublicrepo"", ""website"": """" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "master", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.ProcessDeployment, result);
            Assert.Equal("Bitbucket.org", deploymentInfo.Deployer);
            Assert.Equal(RepositoryType.Git, deploymentInfo.RepositoryType);
            Assert.Equal("https://bitbucket.org/kudutest/mypublicrepo/", deploymentInfo.RepositoryUrl);
            Assert.Equal("Pranav K", deploymentInfo.TargetChangeset.AuthorName);
            Assert.Equal("d3bde12dfe11206173fa940b3e50b135e9ae1677", deploymentInfo.TargetChangeset.Id);
            Assert.Equal("Some changes to Hello", deploymentInfo.TargetChangeset.Message);
        }
Пример #4
0
        public void BitbucketHandlerParsesBitbucketPayloadsForGitRepositories()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [ { ""author"": ""Pranav K"", ""branch"": ""master"", ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Some changes to Hello.txt\n"", ""node"": ""0b418c5fd473"", ""parents"": [ ""ec8a72695042"" ], ""raw_author"": ""Pranav K <*****@*****.**>"", ""raw_node"": ""0b418c5fd473474f197071ec75cd664937d6565d"", ""revision"": null, ""size"": -1, ""timestamp"": ""2012-12-17 22:01:22"", ""utctimestamp"": ""2012-12-17 21:01:22+00:00"" } ], ""repository"": { ""absolute_url"": ""/kudutest/mypublicrepo/"", ""fork"": false, ""is_private"": false, ""name"": ""MyPublicRepo"", ""owner"": ""kudutest"", ""scm"": ""git"", ""slug"": ""mypublicrepo"", ""website"": """" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "master", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.ProcessDeployment, result);
            Assert.Equal("Bitbucket.org", deploymentInfo.Deployer);
            Assert.Equal(RepositoryType.Git, deploymentInfo.RepositoryType);
            Assert.Equal("https://bitbucket.org/kudutest/mypublicrepo/", deploymentInfo.RepositoryUrl);
            Assert.Equal("Pranav K", deploymentInfo.TargetChangeset.AuthorName);
            Assert.Equal("0b418c5fd473474f197071ec75cd664937d6565d", deploymentInfo.TargetChangeset.Id);
            Assert.Equal("Some changes to Hello.txt", deploymentInfo.TargetChangeset.Message);
        }
Пример #5
0
        public void BitbucketHandlerAllowsPayloadsWithNullBranch()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", 
                    ""commits"": [ { ""author"": ""pranavkm"", ""branch"": null, ""raw_node"": ""0bbefd70c4c4213bba1e91998141f6e861cec24d"", ""message"": ""Some file changes"" }],
                    ""repository"": { ""absolute_url"": ""/kudutest/hellomercurial/"", ""is_private"": false, ""name"": ""HelloMercurial"", ""owner"": ""kudutest"", ""scm"": ""hg"" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "not-default", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.ProcessDeployment, result);
            Assert.Equal("Bitbucket", deploymentInfo.Deployer);
            Assert.Equal(RepositoryType.Mercurial, deploymentInfo.RepositoryType);
            Assert.Equal("https://bitbucket.org/kudutest/hellomercurial/", deploymentInfo.RepositoryUrl);
            Assert.Equal("pranavkm", deploymentInfo.TargetChangeset.AuthorName);
            Assert.Equal("0bbefd70c4c4213bba1e91998141f6e861cec24d", deploymentInfo.TargetChangeset.Id);
            Assert.Equal("Some file changes", deploymentInfo.TargetChangeset.Message);
        }
        protected DeploymentInfoBase GetDeploymentInfo(JObject payload, string targetBranch)
        {
            var info = new DeploymentInfo(RepositoryFactory)
            {
                Deployer     = "Bitbucket",
                IsContinuous = true
            };

            // get changes
            // do not handle NullRef, let if fail if payload is not in the right format
            //{
            //    "push": {
            //        "changes": [
            //            {
            //                "old": { ... },
            //                "new": { ... }
            //            }
            //        ]
            //    }
            //}
            var changes = payload.Value <JObject>("push").Value <JArray>("changes");

            if (changes != null && changes.Count > 0)
            {
                JObject latestCommit = (from change in changes
                                        where change.Value <JObject>("new") != null && string.Equals(targetBranch, change.Value <JObject>("new").Value <string>("name") ?? targetBranch, StringComparison.OrdinalIgnoreCase)
                                        orderby BitbucketHandler.TryParseCommitStamp(change.Value <JObject>("new").Value <JObject>("target").Value <string>("date")) descending
                                        select change.Value <JObject>("new")).FirstOrDefault();

                if (latestCommit == null)
                {
                    return(null);
                }

                string   authorRaw    = WebUtility.HtmlDecode(latestCommit.Value <JObject>("target").Value <JObject>("author").Value <string>("raw"));
                string[] nameAndEmail = authorRaw.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries);
                info.TargetChangeset = new ChangeSet(
                    id: latestCommit.Value <JObject>("target").Value <string>("hash"),
                    authorName: nameAndEmail.Length == 2 ? nameAndEmail[0].Trim() : authorRaw,
                    authorEmail: nameAndEmail.Length == 2 ? nameAndEmail[1].Trim() : null,
                    message: latestCommit.Value <JObject>("target").Value <string>("message"),
                    timestamp: BitbucketHandler.TryParseCommitStamp(latestCommit.Value <JObject>("target").Value <string>("date")));
            }
            else
            {
                info.TargetChangeset = new ChangeSet(id: String.Empty, authorName: null,
                                                     authorEmail: null, message: null, timestamp: DateTime.UtcNow);
            }

            info.RepositoryUrl = payload.Value <JObject>("repository").Value <JObject>("links").Value <JObject>("html").Value <string>("href");
            bool isGitRepo = string.Equals("git", payload.Value <JObject>("repository").Value <string>("scm"), StringComparison.OrdinalIgnoreCase);

            info.RepositoryType = isGitRepo ? RepositoryType.Git : RepositoryType.Mercurial;

            bool isPrivate = bool.Parse(payload.Value <JObject>("repository").Value <string>("is_private"));

            // private repo, use SSH
            if (isPrivate)
            {
                info.RepositoryUrl = BitbucketHandler.GetPrivateRepoUrl(info.RepositoryUrl, info.RepositoryType);
            }

            return(info);
        }
Пример #7
0
        public void BitbucketHandlerParsesBitbucketPayloadsForMercurialRepositoriesWithMultipleCommitsAcrossBranches()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [ { ""author"": ""pranavkm"", ""branch"": ""default"", ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Fix Hello.txt"", ""node"": ""dabec27eeec9"", ""parents"": [ ""478b0d4d794c"" ], ""raw_author"": ""Pranav <*****@*****.**>"", ""raw_node"": ""dabec27eeec9d85175a4dcbeb83b65189c929b68"", ""revision"": 7, ""size"": -1, ""timestamp"": ""2012-12-17 21:47:21"", ""utctimestamp"": ""2012-12-17 20:47:21+00:00"" }, { ""author"": ""pranavkm"", ""branch"": ""default"", ""files"": [ { ""file"": ""HelloWorld.txt"", ""type"": ""added"" } ], ""message"": ""Adding hello world"", ""node"": ""42c0d799763d"", ""parents"": [ ""dabec27eeec9"" ], ""raw_author"": ""Pranav <*****@*****.**>"", ""raw_node"": ""42c0d799763d7acbe4312d000f771ec0afa0d6ab"", ""revision"": 8, ""size"": -1, ""timestamp"": ""2012-12-17 21:47:46"", ""utctimestamp"": ""2012-12-17 20:47:46+00:00"" }, { ""author"": ""pranavkm"", ""branch"": ""Test-Branch"", ""files"": [ { ""file"": ""HelloWorld.txt"", ""type"": ""modified"" } ], ""message"": ""Fixing hello world"", ""node"": ""16ea3237dbcd"", ""parents"": [ ""42c0d799763d"" ], ""raw_author"": ""Pranav <*****@*****.**>"", ""raw_node"": ""16ea3237dbcd492b170c28ae0060791a1c170c0c"", ""revision"": 9, ""size"": -1, ""timestamp"": ""2012-12-17 21:48:13"", ""utctimestamp"": ""2012-12-17 20:48:13+00:00"" } ], ""repository"": { ""absolute_url"": ""/kudutest/hellomercurial/"", ""fork"": false, ""is_private"": false, ""name"": ""HelloMercurial"", ""owner"": ""kudutest"", ""scm"": ""hg"", ""slug"": ""hellomercurial"", ""website"": """" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "default", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.ProcessDeployment, result);
            Assert.Equal("Bitbucket", deploymentInfo.Deployer);
            Assert.Equal(RepositoryType.Mercurial, deploymentInfo.RepositoryType);
            Assert.Equal("https://bitbucket.org/kudutest/hellomercurial/", deploymentInfo.RepositoryUrl);
            Assert.Equal("pranavkm", deploymentInfo.TargetChangeset.AuthorName);
            Assert.Equal("42c0d799763d7acbe4312d000f771ec0afa0d6ab", deploymentInfo.TargetChangeset.Id);
            Assert.Equal("Adding hello world", deploymentInfo.TargetChangeset.Message);
        }
Пример #8
0
        public void BitbucketDoesNotReturnNoOpForDeleteOperations()
        {
            // Arrange
            string payloadContent = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [], ""repository"": { ""absolute_url"": ""/kudutest/myprivaterepo/"", ""fork"": false, ""is_private"": false, ""name"": ""MyprivateRepo"", ""owner"": ""kudutest"", ""scm"": ""git"", ""slug"": ""myprivaterepo"", ""website"": """" }, ""user"": ""kudutest"" }";

            var httpRequest = new Mock<HttpRequestBase>();
            httpRequest.SetupGet(r => r.UserAgent).Returns("Bitbucket.org");
            var bitbucketHandler = new BitbucketHandler();

            // Act
            DeploymentInfo deploymentInfo;
            DeployAction result = bitbucketHandler.TryParseDeploymentInfo(httpRequest.Object, payload: JObject.Parse(payloadContent), targetBranch: "master", deploymentInfo: out deploymentInfo);

            // Assert
            Assert.Equal(DeployAction.ProcessDeployment, result);
            Assert.Equal("Bitbucket", deploymentInfo.Deployer);
            Assert.Equal(RepositoryType.Git, deploymentInfo.RepositoryType);
            Assert.Equal("https://bitbucket.org/kudutest/myprivaterepo/", deploymentInfo.RepositoryUrl);
            Assert.Empty(deploymentInfo.TargetChangeset.Id);
            Assert.Null(deploymentInfo.TargetChangeset.AuthorName);
            Assert.Null(deploymentInfo.TargetChangeset.Message);
        }