Esempio n. 1
1
        // { 
        //   'format':'basic'
        //   'url':'http://host/repository',
        //   'is_hg':true // optional
        // }
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (!String.Equals(payload.Value<string>("format"), "basic", StringComparison.OrdinalIgnoreCase))
            {
                return DeployAction.UnknownPayload;
            }

            string url = payload.Value<string>("url");
            if (String.IsNullOrEmpty(url))
            {
                return DeployAction.UnknownPayload;
            }

            string scm = payload.Value<string>("scm");
            bool is_hg;
            if (String.IsNullOrEmpty(scm))
            {
                // SSH hg@... vs git@...
                is_hg = url.StartsWith("hg@", StringComparison.OrdinalIgnoreCase);
            }
            else
            {
                is_hg = String.Equals(scm, "hg", StringComparison.OrdinalIgnoreCase);
            }

            deploymentInfo = new DeploymentInfo();
            deploymentInfo.RepositoryUrl = url;
            deploymentInfo.RepositoryType = is_hg ? RepositoryType.Mercurial : RepositoryType.Git;
            deploymentInfo.Deployer = GetDeployerFromUrl(url);
            deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(message: "Fetch from " + url);

            return DeployAction.ProcessDeployment;
        }
Esempio n. 2
0
        public async Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
        {
            var oneDriveInfo = (OneDriveInfo)deploymentInfo;

            _oneDriveHelper.Logger       = logger;
            oneDriveInfo.TargetChangeset = await _oneDriveHelper.Sync(oneDriveInfo, repository, tracer);
        }
Esempio n. 3
0
        protected static DeploymentInfo GetDeploymentInfo(JObject payload, string targetBranch)
        {
            var info = new DeploymentInfo
            {
                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 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");
            info.RepositoryType = RepositoryType.Git;   // TODO once bug is fixed https://bitbucket.org/site/master/issues/11665/missing-scm-and-is_private-info-from

            bool isPrivate = false;     // TODO once bug is fixed https://bitbucket.org/site/master/issues/11665/missing-scm-and-is_private-info-from
            // private repo, use SSH
            if (isPrivate)
            {
                info.RepositoryUrl = BitbucketHandler.GetPrivateRepoUrl(info.RepositoryUrl, info.RepositoryType);
            }

            return info;
        }
Esempio n. 4
0
        private DeploymentInfoBase GetDeploymentInfo(JObject payload, string targetBranch)
        {
            var repository = payload.Value <JObject>("repository");
            var commits    = payload.Value <JArray>("commits");

            // Identify the last commit for the target branch.
            JObject targetCommit = (from commit in commits
                                    where targetBranch.Equals(commit.Value <string>("branch"), StringComparison.OrdinalIgnoreCase)
                                    orderby commit.Value <int>("revision") descending
                                    select(JObject) commit).FirstOrDefault();

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

            string accessToken = _settings.GetValue("kiln.accesstoken");

            var author = targetCommit.Value <string>("author");

            // if the commit was via an access token we don't want to show that for security reasons
            Guid authorGuid;

            if (Guid.TryParse(author, out authorGuid))
            {
                author = "System Account";
            }

            var info = new DeploymentInfo(RepositoryFactory)
            {
                Deployer        = "Kiln",
                RepositoryUrl   = repository.Value <string>("url"),
                RepositoryType  = RepositoryType.Mercurial,
                IsContinuous    = true,
                TargetChangeset = new ChangeSet(
                    id: targetCommit.Value <string>("id"),
                    authorName: ParseNameFromAuthor(author),
                    authorEmail: ParseEmailFromAuthor(author),
                    message: (targetCommit.Value <string>("message") ?? String.Empty).Trim(),
                    timestamp: new DateTimeOffset(DateTime.Parse(targetCommit.Value <string>("timestamp"), CultureInfo.InvariantCulture), TimeSpan.Zero)
                    )
            };

            bool isPrivate = !String.IsNullOrWhiteSpace(accessToken); // assume a private repo if an access token is provided

            if (isPrivate)
            {
                var uri = new UriBuilder(info.RepositoryUrl)
                {
                    Scheme   = "https",
                    Port     = -1,
                    UserName = accessToken,
                    Password = "******" // kiln doesn't use the password when using an access token
                };

                info.RepositoryUrl = uri.ToString();
            }

            return(info);
        }
Esempio n. 5
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string deployer = payload.Value <string>("deployer");

            if (!"CodePlex".Equals(deployer, StringComparison.OrdinalIgnoreCase))
            {
                return(DeployAction.UnknownPayload);
            }

            // { url: "", branch: "", deployer: "", oldRef: "", newRef: "", scmType: "Git" }
            string newRef = payload.Value <string>("newRef");
            string scm    = payload.Value <string>("scmType");
            string branch = payload.Value <string>("branch");

            deploymentInfo = new DeploymentInfo
            {
                RepositoryUrl   = payload.Value <string>("url"),
                Deployer        = "CodePlex",
                IsContinuous    = true,
                TargetChangeset = new ChangeSet(newRef, authorName: null, authorEmail: null, message: null, timestamp: DateTimeOffset.Now)
            };

            deploymentInfo.RepositoryType = ("Mercurial".Equals(scm, StringComparison.OrdinalIgnoreCase)) ? RepositoryType.Mercurial : RepositoryType.Git;

            // Ignore the deployment request
            // (a) if the target branch does not exist (null or empty string). This happens during test hook
            // (b) if the target branch does not have the same name as the deployed branch
            // (c) if the newRef is all zero (deleted changesets)
            return((String.IsNullOrEmpty(branch) || targetBranch.Equals(branch, StringComparison.OrdinalIgnoreCase)) && newRef.Any(c => c != '0') ?
                   DeployAction.ProcessDeployment : DeployAction.NoOp);
        }
Esempio n. 6
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string deployer = payload.Value<string>("deployer");
            if (!"CodePlex".Equals(deployer, StringComparison.OrdinalIgnoreCase))
            {
                return DeployAction.UnknownPayload;
            }

            // { url: "", branch: "", deployer: "", oldRef: "", newRef: "", scmType: "Git" } 
            string newRef = payload.Value<string>("newRef");
            string scm = payload.Value<string>("scmType");
            string branch = payload.Value<string>("branch");

            deploymentInfo = new DeploymentInfo
            {
                RepositoryUrl = payload.Value<string>("url"),
                Deployer = "CodePlex",
                IsContinuous = true,
                TargetChangeset = new ChangeSet(newRef, authorName: null, authorEmail: null, message: null, timestamp: DateTimeOffset.Now)
            };

            deploymentInfo.RepositoryType = ("Mercurial".Equals(scm, StringComparison.OrdinalIgnoreCase)) ? RepositoryType.Mercurial : RepositoryType.Git;

            // Ignore the deployment request 
            // (a) if the target branch does not exist (null or empty string). This happens during test hook
            // (b) if the target branch does not have the same name as the deployed branch
            // (c) if the newRef is all zero (deleted changesets)
            return (String.IsNullOrEmpty(branch) || targetBranch.Equals(branch, StringComparison.OrdinalIgnoreCase)) && newRef.Any(c => c != '0') ?
                   DeployAction.ProcessDeployment : DeployAction.NoOp;
        }
Esempio n. 7
0
        protected DeploymentInfoBase GetDeploymentInfo(JObject payload, string targetBranch)
        {
            // bitbucket format
            // { repository: { absolute_url: "/a/b", is_private: true }, canon_url: "https//..." }
            var repository = payload.Value <JObject>("repository");

            var info = new DeploymentInfo(RepositoryFactory)
            {
                Deployer     = "Bitbucket",
                IsContinuous = true
            };

            // Per bug #623, Bitbucket sends an empty commits array when a commit has a large number of files.
            // In this case, we'll attempt to fetch regardless.
            var commits = payload.Value <JArray>("commits");

            if (commits != null && commits.Count > 0)
            {
                // Identify the last commit for the target branch.
                JObject targetCommit = (from commit in commits
                                        where targetBranch.Equals(commit.Value <string>("branch") ?? targetBranch, StringComparison.OrdinalIgnoreCase)
                                        orderby TryParseCommitStamp(commit.Value <string>("utctimestamp")) descending
                                        select(JObject) commit).FirstOrDefault();


                if (targetCommit == null)
                {
                    return(null);
                }
                info.TargetChangeset = new ChangeSet(
                    id: targetCommit.Value <string>("raw_node"),
                    authorName: targetCommit.Value <string>("author"), // The Bitbucket id for the user.
                    authorEmail: null,                                 // TODO: Bitbucket gives us the raw_author field which is the user field set in the repository, maybe we should parse it.
                    message: (targetCommit.Value <string>("message") ?? String.Empty).TrimEnd(),
                    timestamp: TryParseCommitStamp(targetCommit.Value <string>("utctimestamp"))
                    );
            }
            else
            {
                info.TargetChangeset = new ChangeSet(id: String.Empty, authorName: null,
                                                     authorEmail: null, message: null, timestamp: DateTime.UtcNow);
            }

            string server    = payload.Value <string>("canon_url");       // e.g. https://bitbucket.org
            string path      = repository.Value <string>("absolute_url"); // e.g. /davidebbo/testrepo/
            string scm       = repository.Value <string>("scm");          // e.g. hg
            bool   isPrivate = repository.Value <bool>("is_private");

            // Combine them to get the full URL
            info.RepositoryUrl  = server + path;
            info.RepositoryType = scm.Equals("hg", StringComparison.OrdinalIgnoreCase) ? RepositoryType.Mercurial : RepositoryType.Git;

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

            return(info);
        }
Esempio n. 8
0
        public virtual async Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger)
        {
            // (A)sync with dropbox
            var dropboxInfo = (DropboxInfo)deploymentInfo;

            deploymentInfo.TargetChangeset = await _dropBoxHelper.Sync(dropboxInfo, targetBranch, logger, repository);
        }
Esempio n. 9
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            // check if this parser can be used to interprete request
            if (ParserMatches(request, payload, targetBranch))
            {
                // recognize request, decide whether it want to process it
                if (IsNoop(request, payload, targetBranch))
                {
                    // Noop, deployment info is never used
                    deploymentInfo = null;
                    return(DeployAction.NoOp);
                }

                // fill deploymentinfo body
                deploymentInfo = new DeploymentInfo {
                    RepositoryType = RepositoryType.Git, IsContinuous = true
                };
                var    commits = payload.Value <JArray>("commits");
                string newRef  = payload.Value <string>("after");
                deploymentInfo.TargetChangeset = ParseChangeSet(newRef, commits);
                deploymentInfo.RepositoryUrl   = DetermineSecurityProtocol(payload);
                deploymentInfo.Deployer        = GetDeployer();

                return(DeployAction.ProcessDeployment);
            }

            deploymentInfo = null;
            return(DeployAction.UnknownPayload);
        }
Esempio n. 10
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            // check if this parser can be used to interprete request
            if (ParserMatches(request, payload, targetBranch))
            { 
                // recognize request, decide whether it want to process it
                if (IsNoop(request, payload, targetBranch))
                {
                    // Noop, deployment info is never used
                    deploymentInfo = null;
                    return DeployAction.NoOp;
                }

                // fill deploymentinfo body
                deploymentInfo = new DeploymentInfo { RepositoryType = RepositoryType.Git, IsContinuous = true };
                var commits = payload.Value<JArray>("commits");
                string newRef = payload.Value<string>("after");
                deploymentInfo.TargetChangeset = ParseChangeSet(newRef, commits);
                deploymentInfo.RepositoryUrl = DetermineSecurityProtocol(payload);
                deploymentInfo.Deployer = GetDeployer();

                return DeployAction.ProcessDeployment;
            }

            deploymentInfo = null;
            return DeployAction.UnknownPayload;
        } 
Esempio n. 11
0
        protected static DeploymentInfo GetDeploymentInfo(JObject payload, string targetBranch)
        {
            // bitbucket format
            // { repository: { absolute_url: "/a/b", is_private: true }, canon_url: "https//..." } 
            var repository = payload.Value<JObject>("repository");

            var info = new DeploymentInfo
            {
                Deployer = "Bitbucket",
                IsContinuous = true
            };

            // Per bug #623, Bitbucket sends an empty commits array when a commit has a large number of files. 
            // In this case, we'll attempt to fetch regardless.
            var commits = payload.Value<JArray>("commits");
            if (commits != null && commits.Count > 0)
            {
                // Identify the last commit for the target branch. 
                JObject targetCommit = (from commit in commits
                                        where targetBranch.Equals(commit.Value<string>("branch") ?? targetBranch, StringComparison.OrdinalIgnoreCase)
                                        orderby TryParseCommitStamp(commit.Value<string>("utctimestamp")) descending
                                        select (JObject)commit).FirstOrDefault();


                if (targetCommit == null)
                {
                    return null;
                }
                info.TargetChangeset = new ChangeSet(
                    id: targetCommit.Value<string>("raw_node"),
                    authorName: targetCommit.Value<string>("author"),  // The Bitbucket id for the user.
                    authorEmail: null,                                 // TODO: Bitbucket gives us the raw_author field which is the user field set in the repository, maybe we should parse it.
                    message: (targetCommit.Value<string>("message") ?? String.Empty).TrimEnd(),
                    timestamp: TryParseCommitStamp(targetCommit.Value<string>("utctimestamp"))
                );
            }
            else
            {
                info.TargetChangeset = new ChangeSet(id: String.Empty, authorName: null,
                                        authorEmail: null, message: null, timestamp: DateTime.UtcNow);
            }

            string server = payload.Value<string>("canon_url");     // e.g. https://bitbucket.org
            string path = repository.Value<string>("absolute_url"); // e.g. /davidebbo/testrepo/
            string scm = repository.Value<string>("scm"); // e.g. hg
            bool isPrivate = repository.Value<bool>("is_private");

            // Combine them to get the full URL
            info.RepositoryUrl = server + path;
            info.RepositoryType = scm.Equals("hg", StringComparison.OrdinalIgnoreCase) ? RepositoryType.Mercurial : RepositoryType.Git;

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

            return info;
        }
Esempio n. 12
0
        protected static DeploymentInfo GetDeploymentInfo(JObject payload, string targetBranch)
        {
            // bitbucket format
            // { repository: { absolute_url: "/a/b", is_private: true }, canon_url: "https//..." }
            var repository = payload.Value <JObject>("repository");
            var commits    = payload.Value <JArray>("commits");


            // Identify the last commit for the target branch.
            JObject targetCommit = (from commit in commits
                                    where targetBranch.Equals(commit.Value <string>("branch"), StringComparison.OrdinalIgnoreCase)
                                    orderby DateTimeOffset.Parse(commit.Value <string>("utctimestamp")) descending
                                    select(JObject) commit).FirstOrDefault();

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

            var info = new DeploymentInfo {
                IsContinuous = true
            };
            string server    = payload.Value <string>("canon_url");       // e.g. https://bitbucket.org
            string path      = repository.Value <string>("absolute_url"); // e.g. /davidebbo/testrepo/
            string scm       = repository.Value <string>("scm");          // e.g. hg
            bool   isPrivate = repository.Value <bool>("is_private");

            // Combine them to get the full URL
            info.RepositoryUrl  = server + path;
            info.RepositoryType = scm.Equals("hg", StringComparison.OrdinalIgnoreCase) ? RepositoryType.Mercurial : RepositoryType.Git;

            info.TargetChangeset = new ChangeSet(
                id: targetCommit.Value <string>("raw_node"),
                authorName: targetCommit.Value <string>("author"), // The Bitbucket id for the user.
                authorEmail: null,                                 // TODO: Bitbucket gives us the raw_author field which is the user field set in the repository, maybe we should parse it.
                message: (targetCommit.Value <string>("message") ?? String.Empty).TrimEnd(),
                timestamp: DateTimeOffset.Parse(targetCommit.Value <string>("utctimestamp"))
                );

            info.Deployer = "Bitbucket";

            // private repo, use SSH
            if (isPrivate)
            {
                var uri = new UriBuilder(info.RepositoryUrl);
                if (uri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    uri.Scheme = "ssh";
                    uri.Port   = -1;
                    uri.Host   = (info.RepositoryType == RepositoryType.Mercurial ? "hg@" : "git@") + uri.Host;

                    // Private repo paths are of the format ssh://[email protected]/accountname/reponame.git
                    info.RepositoryUrl = uri.ToString();
                }
            }

            return(info);
        }
Esempio n. 13
0
        protected DeploymentInfo GetDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch)
        {
            // bitbucket format
            // { repository: { absolute_url: "/a/b", is_private: true }, canon_url: "https//..." }
            var repository = payload.Value<JObject>("repository");
            var commits = payload.Value<JArray>("commits");

            // Identify the last commit for the target branch.
            JObject targetCommit = (from commit in commits
                                    where targetBranch.Equals(commit.Value<string>("branch"), StringComparison.OrdinalIgnoreCase)
                                    orderby DateTimeOffset.Parse(commit.Value<string>("utctimestamp")) descending
                                    select (JObject)commit).FirstOrDefault();

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

            var info = new DeploymentInfo();
            string server = payload.Value<string>("canon_url");     // e.g. https://bitbucket.org
            string path = repository.Value<string>("absolute_url"); // e.g. /davidebbo/testrepo/
            string scm = repository.Value<string>("scm"); // e.g. hg
            bool isPrivate = repository.Value<bool>("is_private");

            // Combine them to get the full URL
            info.RepositoryUrl = server + path;
            info.RepositoryType = scm.Equals("hg", StringComparison.OrdinalIgnoreCase) ? RepositoryType.Mercurial : RepositoryType.Git;

            info.TargetChangeset = new ChangeSet(
                id: targetCommit.Value<string>("raw_node"),
                authorName: targetCommit.Value<string>("author"),  // The Bitbucket id for the user.
                authorEmail: null,                                 // TODO: Bitbucket gives us the raw_author field which is the user field set in the repository, maybe we should parse it.
                message: (targetCommit.Value<string>("message") ?? String.Empty).TrimEnd(),
                timestamp: DateTimeOffset.Parse(targetCommit.Value<string>("utctimestamp"))
            );

            info.Deployer = "Bitbucket";

            // private repo, use SSH
            if (isPrivate)
            {
                var uri = new UriBuilder(info.RepositoryUrl);
                if (uri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    uri.Scheme = "ssh";
                    uri.Port = -1;
                    uri.Host = (info.RepositoryType == RepositoryType.Mercurial ? "hg@" : "git@") + uri.Host;

                    // Private repo paths are of the format ssh://[email protected]/accountname/reponame.git
                    info.RepositoryUrl = uri.ToString();
                }
            }

            // Identity the latest commit

            return info;
        }
Esempio n. 14
0
 public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
 {
     deploymentInfo = null;
     if (request.Headers["X-Github-Event"] != null)
     {
         GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
         deploymentInfo = gitDeploymentInfo;
         return deploymentInfo == null || IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment;
     }
     return DeployAction.UnknownPayload;
 }
Esempio n. 15
0
 public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
 {
     GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
     if (gitDeploymentInfo != null && gitDeploymentInfo.IsValid())
     {
         deploymentInfo = gitDeploymentInfo;
         return IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment;
     }
     deploymentInfo = null;
     return DeployAction.UnknownPayload;
 }
Esempio n. 16
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (request.UserAgent != null &&
                request.UserAgent.StartsWith("Bitbucket.org", StringComparison.OrdinalIgnoreCase))
            {
                deploymentInfo = GetDeploymentInfo(payload, targetBranch);
                return deploymentInfo == null ? DeployAction.NoOp : DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 17
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;

            if (IsKilnRequest(payload))
            {
                deploymentInfo = GetDeploymentInfo(payload, targetBranch);
                return deploymentInfo == null ? DeployAction.NoOp : DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 18
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (request.UserAgent != null &&
                request.UserAgent.StartsWith("Codebasehq", StringComparison.OrdinalIgnoreCase))
            {
                GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
                deploymentInfo = gitDeploymentInfo;
                return deploymentInfo == null || IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 19
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;

            var publisherId = payload.Value<string>("publisherId");
            if (String.Equals(publisherId, "tfs", StringComparison.OrdinalIgnoreCase))
            {
                deploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
                return DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 20
0
        public static void SetRepositoryUrl(DeploymentInfo deploymentInfo, string url)
        {
            string commitId = null;
            int index = url.LastIndexOf('#');
            if (index >= 0)
            {
                if (index + 1 < url.Length)
                {
                    commitId = url.Substring(index + 1);
                }
                url = url.Substring(0, index);
            }

            deploymentInfo.RepositoryUrl = url;
            deploymentInfo.CommitId = commitId;
        }
Esempio n. 21
0
        public static void SetRepositoryUrl(DeploymentInfo deploymentInfo, string url)
        {
            string commitId = null;
            int    index    = url.LastIndexOf('#');

            if (index >= 0)
            {
                if (index + 1 < url.Length)
                {
                    commitId = url.Substring(index + 1);
                }
                url = url.Substring(0, index);
            }

            deploymentInfo.RepositoryUrl = url;
            deploymentInfo.CommitId      = commitId;
        }
Esempio n. 22
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (!String.IsNullOrEmpty(payload.Value<string>("NewCursor")))
            {
                var dropboxInfo = new DropboxInfo(payload);
                deploymentInfo = dropboxInfo;

                // Temporary deployment
                string authorName = dropboxInfo.DeployInfo.UserName;
                string authorEmail = dropboxInfo.DeployInfo.Email;
                string message = "Syncing with dropbox at " + DateTime.UtcNow.ToString("g");
                deploymentInfo.TargetChangeset = new ChangeSet("InProgress", authorName, authorEmail, message, DateTimeOffset.MinValue);

                return DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 23
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (!String.IsNullOrEmpty(payload.Value<string>("NewCursor")))
            {
                var dropboxInfo = new DropboxInfo(payload, _settings.IsNullRepository() ? RepositoryType.None : RepositoryType.Git); 
                deploymentInfo = dropboxInfo; 

                // Temporary deployment
                deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                    authorName: dropboxInfo.DeployInfo.UserName,
                    authorEmail: dropboxInfo.DeployInfo.Email,
                    message: String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_Synchronizing, dropboxInfo.DeployInfo.Deltas.Count())
                );

                return DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 24
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload,
            string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string branch = payload.Value<string>("ref");

            if (String.IsNullOrEmpty(branch))
            {
                return DeployAction.UnknownPayload;
            }

            GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload);
            if (gitDeploymentInfo != null && gitDeploymentInfo.IsValid())
            {
                deploymentInfo = gitDeploymentInfo;
                return IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment;
            }

            return DeployAction.UnknownPayload;
        }
Esempio n. 25
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            DropboxInfo dropboxInfo = null;
            string message = null;

            if (!String.IsNullOrEmpty(payload.Value<string>("NewCursor")))
            {
                dropboxInfo = DropboxInfo.CreateV1Info(payload, GetRepositoryType());
                message = String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_SynchronizingNChanges, dropboxInfo.DeployInfo.Deltas.Count);
            }
            else if (String.Equals(payload.Value<string>("scmType"), "DropboxV2", StringComparison.OrdinalIgnoreCase))
            {
                string oauthToken = GetValue(payload, DropboxTokenKey),
                       path = GetValue(payload, DropboxPathKey),
                       userName = payload.Value<string>("dropbox_username") ?? "Dropbox",
                       email = payload.Value<string>("dropbox_email");
                
                dropboxInfo = DropboxInfo.CreateV2Info(path, oauthToken, GetRepositoryType());
                dropboxInfo.DeployInfo.UserName = userName;
                dropboxInfo.DeployInfo.Email = email;
                message = String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_Synchronizing);
            }

            if (dropboxInfo != null)
            {
                deploymentInfo = dropboxInfo;

                // Temporary deployment
                deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                    authorName: dropboxInfo.DeployInfo.UserName,
                    authorEmail: dropboxInfo.DeployInfo.Email,
                    message: message
                );
                
                return DeployAction.ProcessDeployment;
            }

            deploymentInfo = null;
            return DeployAction.UnknownPayload;
        }
Esempio n. 26
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string deployer = payload.Value<string>("deployer");
            if (!"CodePlex".Equals(deployer, StringComparison.OrdinalIgnoreCase))
            {
                return DeployAction.UnknownPayload;
            }

            string newRef = payload.Value<string>("newRef");
            string scm = payload.Value<string>("scmType");
            string branch = payload.Value<string>("branch");

            deploymentInfo = new DeploymentInfo
            {
                RepositoryUrl = payload.Value<string>("url"),
                Deployer = "CodePlex",
                TargetChangeset = new ChangeSet(newRef, authorName: null, authorEmail: null, message: null, timestamp: DateTimeOffset.Now)
            };

            if ("Mercurial".Equals(scm, StringComparison.OrdinalIgnoreCase))
            {
                // { url: "", branch: "", deployer: "", scmType: "Mercurial" }
                deploymentInfo.RepositoryType = RepositoryType.Mercurial;
                // TODO: Figure out what happens when you delete the branch and handle that.
                return targetBranch.Equals(branch, StringComparison.OrdinalIgnoreCase) ? DeployAction.ProcessDeployment : DeployAction.NoOp;
            }
            else
            {
                // Look for the generic format
                // { url: "", branch: "", deployer: "", oldRef: "", newRef: "", scmType: "Git" }

                deploymentInfo.RepositoryType = RepositoryType.Git;

                // Ignore the deployment request (a) if the target branch does not match the deployed branch or (b) if the newRef is all zero (deleted changesets)
                return (deploymentInfo.IsValid() && (!targetBranch.Equals(branch, StringComparison.OrdinalIgnoreCase) || newRef.All(c => c == '0'))) ?
                        DeployAction.NoOp : DeployAction.ProcessDeployment;
            }
        }
Esempio n. 27
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string url = payload.Value<string>("RepositoryUrl");
            if (string.IsNullOrWhiteSpace(url) || !url.ToLowerInvariant().Contains("api.onedrive.com"))
            {
                return DeployAction.UnknownPayload;
            }

            /*
                 Expecting payload to be:
                 {
                    "RepositoryUrl": "xxx",
                    "AccessToken": "xxx"
                 }
             */
            string accessToken = payload.Value<string>("AccessToken");

            // keep email and name, so that can be re-used in later commit
            OneDriveInfo oneDriveInfo = new OneDriveInfo()
            {
                Deployer = "OneDrive",
                RepositoryUrl = url,
                AccessToken = accessToken,
                AuthorName = _settings.GetValue("authorName"),
                AuthorEmail = _settings.GetValue("authorEmail")
            };

            deploymentInfo = oneDriveInfo;

            deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                authorName: oneDriveInfo.AuthorName,
                authorEmail: oneDriveInfo.AuthorEmail,
                message: String.Format(CultureInfo.CurrentUICulture, Resources.OneDrive_Synchronizing)
            );

            return DeployAction.ProcessDeployment;
        }
Esempio n. 28
0
        // {
        //   'format':'basic'
        //   'url':'http://host/repository',
        //   'is_hg':true // optional
        // }
        public override DeployAction TryParseDeploymentInfo(HttpRequest request, JObject payload, string targetBranch, out DeploymentInfoBase deploymentInfo)
        {
            deploymentInfo = null;
            if (!String.Equals(payload.Value <string>("format"), "basic", StringComparison.OrdinalIgnoreCase))
            {
                return(DeployAction.UnknownPayload);
            }

            string url = payload.Value <string>("url");

            if (String.IsNullOrEmpty(url))
            {
                return(DeployAction.UnknownPayload);
            }

            string scm = payload.Value <string>("scm");
            bool   is_hg;

            if (String.IsNullOrEmpty(scm))
            {
                // SSH hg@... vs git@...
                is_hg = url.StartsWith("hg@", StringComparison.OrdinalIgnoreCase);
            }
            else
            {
                is_hg = String.Equals(scm, "hg", StringComparison.OrdinalIgnoreCase);
            }

            deploymentInfo = new DeploymentInfo(RepositoryFactory);
            SetRepositoryUrl(deploymentInfo, url);
            deploymentInfo.RepositoryType  = is_hg ? RepositoryType.Mercurial : RepositoryType.Git;
            deploymentInfo.Deployer        = GetDeployerFromUrl(url);
            deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(message: "Fetch from " + url);
            deploymentInfo.AllowDeploymentWhileScmDisabled = true;

            return(DeployAction.ProcessDeployment);
        }
Esempio n. 29
0
        protected virtual DeploymentInfo GetDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch)
        {
            var sessionToken = payload.Value <JObject>("sessionToken");

            var info = new DeploymentInfo
            {
                RepositoryType = RepositoryType.Git,
                Deployer       = GetDeployer(),
                IsContinuous   = true
            };

            // even it is empty password we need to explicitly say so (with colon).
            // without colon, LibGit2Sharp is not working
            Uri remoteUrl = GetRemoteUrl(payload);

            if (sessionToken == null)
            {
                // if there is no session token, fallback to use raw remoteUrl from setting.xml
                info.RepositoryUrl = remoteUrl.AbsoluteUri;
            }
            else
            {
                info.RepositoryUrl = string.Format(CultureInfo.InvariantCulture, "{0}://{1}:@{2}{3}",
                                                   remoteUrl.Scheme,
                                                   sessionToken.Value <string>("token"),
                                                   remoteUrl.Authority,
                                                   remoteUrl.PathAndQuery);
            }

            info.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                authorName: info.Deployer,
                authorEmail: info.Deployer,
                message: String.Format(CultureInfo.CurrentUICulture, Resources.Vso_Synchronizing)
                );

            return(info);
        }
Esempio n. 30
0
        protected virtual DeploymentInfo GetDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch)
        {
            var sessionToken = payload.Value<JObject>("sessionToken");

            var info = new DeploymentInfo
            {
                RepositoryType = RepositoryType.Git,
                Deployer = GetDeployer(),
                IsContinuous = true
            };

            // even it is empty password we need to explicitly say so (with colon).
            // without colon, LibGit2Sharp is not working
            Uri remoteUrl = GetRemoteUrl(payload);
            if (sessionToken == null)
            {
                // if there is no session token, fallback to use raw remoteUrl from setting.xml
                info.RepositoryUrl = remoteUrl.AbsoluteUri;
            }
            else
            {
                info.RepositoryUrl = string.Format(CultureInfo.InvariantCulture, "{0}://{1}:@{2}{3}",
                    remoteUrl.Scheme,
                    sessionToken.Value<string>("token"),
                    remoteUrl.Authority,
                    remoteUrl.PathAndQuery);
            }

            info.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                authorName: info.Deployer,
                authorEmail: info.Deployer,
                message: String.Format(CultureInfo.CurrentUICulture, Resources.Vso_Synchronizing)
            );

            return info;
        }
Esempio n. 31
0
        private DeployAction GetRepositoryInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo info)
        {
            foreach (var handler in _serviceHookHandlers)
            {
                DeployAction result = handler.TryParseDeploymentInfo(request, payload, targetBranch, out info);
                if (result != DeployAction.UnknownPayload)
                {
                    if (_tracer.TraceLevel >= TraceLevel.Verbose)
                    {
                        TraceHandler(handler);
                    }

                    if (result == DeployAction.ProcessDeployment)
                    {
                        // Although a payload may be intended for a handler, it might not need to fetch.
                        // For instance, if a different branch was pushed than the one the repository is deploying, we can no-op it.
                        Debug.Assert(info != null);
                        info.Handler = handler;
                    }

                    return result;
                }
            }

            throw new FormatException(Resources.Error_UnsupportedFormat);
        }
Esempio n. 32
0
        // For continuous integration, we will only build/deploy if fetch new changes
        // The immediate goal is to address duplicated /deploy requests from Bitbucket (retry if taken > 20s)
        private bool ShouldDeploy(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch)
        {
            if (deploymentInfo.IsContinuous)
            {
                ChangeSet changeSet = repository.GetChangeSet(targetBranch);
                return !String.Equals(_status.ActiveDeploymentId, changeSet.Id, StringComparison.OrdinalIgnoreCase);
            }

            return true;
        }
Esempio n. 33
0
        public async Task PerformDeployment(DeploymentInfo deploymentInfo, IDisposable tempDeployment = null, ChangeSet tempChangeSet = null)
        {
            DateTime currentMarkerFileUTC;
            DateTime nextMarkerFileUTC = FileSystemHelpers.GetLastWriteTimeUtc(_markerFilePath);

            do
            {
                // save the current marker
                currentMarkerFileUTC = nextMarkerFileUTC;

                string targetBranch = _settings.GetBranch();

                using (_tracer.Step("Performing fetch based deployment"))
                {
                    // create temporary deployment before the actual deployment item started
                    // this allows portal ui to readily display on-going deployment (not having to wait for fetch to complete).
                    // in addition, it captures any failure that may occur before the actual deployment item started
                    tempDeployment = tempDeployment ?? _deploymentManager.CreateTemporaryDeployment(
                                                    Resources.ReceivingChanges,
                                                    out tempChangeSet,
                                                    deploymentInfo.TargetChangeset,
                                                    deploymentInfo.Deployer);

                    ILogger innerLogger = null;
                    try
                    {
                        ILogger logger = _deploymentManager.GetLogger(tempChangeSet.Id);

                        // Fetch changes from the repository
                        innerLogger = logger.Log(Resources.FetchingChanges);

                        IRepository repository = _repositoryFactory.EnsureRepository(deploymentInfo.RepositoryType);

                        try
                        {
                            await deploymentInfo.Handler.Fetch(repository, deploymentInfo, targetBranch, innerLogger);
                        }
                        catch (BranchNotFoundException)
                        {
                            // mark no deployment is needed
                            deploymentInfo.TargetChangeset = null;
                        }

                        // set to null as Deploy() below takes over logging
                        innerLogger = null;

                        // The branch or commit id to deploy
                        string deployBranch = !String.IsNullOrEmpty(deploymentInfo.CommitId) ? deploymentInfo.CommitId : targetBranch;

                        // In case the commit or perhaps fetch do no-op.
                        if (deploymentInfo.TargetChangeset != null && ShouldDeploy(repository, deploymentInfo, deployBranch))
                        {
                            // Perform the actual deployment
                            var changeSet = repository.GetChangeSet(deployBranch);

                            if (changeSet == null && !String.IsNullOrEmpty(deploymentInfo.CommitId))
                            {
                                throw new InvalidOperationException(String.Format("Invalid revision '{0}'!", deploymentInfo.CommitId));
                            }

                            // Here, we don't need to update the working files, since we know Fetch left them in the correct state
                            // unless for GenericHandler where specific commitId is specified
                            bool deploySpecificCommitId = !String.IsNullOrEmpty(deploymentInfo.CommitId);

                            await _deploymentManager.DeployAsync(repository, changeSet, deploymentInfo.Deployer, clean: false, needFileUpdate: deploySpecificCommitId);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (innerLogger != null)
                        {
                            innerLogger.Log(ex);
                        }

                        // In case the commit or perhaps fetch do no-op.
                        if (deploymentInfo.TargetChangeset != null)
                        {
                            IDeploymentStatusFile statusFile = _status.Open(deploymentInfo.TargetChangeset.Id);
                            if (statusFile != null)
                            {
                                statusFile.MarkFailed();
                            }
                        }

                        throw;
                    }

                    // only clean up temp deployment if successful
                    tempDeployment.Dispose();
                }

                // check marker file and, if changed (meaning new /deploy request), redeploy.
                nextMarkerFileUTC = FileSystemHelpers.GetLastWriteTimeUtc(_markerFilePath);
            } while (deploymentInfo.IsReusable && currentMarkerFileUTC != nextMarkerFileUTC);
        }
 public abstract DeployAction TryParseDeploymentInfo(System.Web.HttpRequestBase request, Newtonsoft.Json.Linq.JObject payload, string targetBranch, out DeploymentInfo deploymentInfo);
Esempio n. 35
0
 public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
 {
     deploymentInfo = null;
     if (request.Headers["X-Github-Event"] != null)
     {
         GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
         deploymentInfo = gitDeploymentInfo;
         return(deploymentInfo == null || IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment);
     }
     return(DeployAction.UnknownPayload);
 }
        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);
        }
Esempio n. 37
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;

            if (IsKilnRequest(payload))
            {
                deploymentInfo = GetDeploymentInfo(payload, targetBranch);
                return(deploymentInfo == null ? DeployAction.NoOp : DeployAction.ProcessDeployment);
            }

            return(DeployAction.UnknownPayload);
        }
Esempio n. 38
0
 public Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger)
 {
     repository.ClearLock();
     repository.FetchWithoutConflict(deploymentInfo.RepositoryUrl, targetBranch);
     return TaskHelpers.Completed();
 }
Esempio n. 39
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (request.UserAgent != null &&
                request.UserAgent.StartsWith("Bitbucket-Webhooks/2.0", StringComparison.OrdinalIgnoreCase))
            {
                deploymentInfo = GetDeploymentInfo(payload, targetBranch);
                return(deploymentInfo == null ? DeployAction.NoOp : DeployAction.ProcessDeployment);
            }

            return(DeployAction.UnknownPayload);
        }
Esempio n. 40
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            DropboxInfo dropboxInfo = null;
            string      message     = null;

            if (!String.IsNullOrEmpty(payload.Value <string>("NewCursor")))
            {
                dropboxInfo = DropboxInfo.CreateV1Info(payload, GetRepositoryType());
                message     = String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_SynchronizingNChanges, dropboxInfo.DeployInfo.Deltas.Count);
            }
            else if (String.Equals(payload.Value <string>("scmType"), "DropboxV2", StringComparison.OrdinalIgnoreCase))
            {
                string oauthToken = GetValue(payload, DropboxTokenKey),
                       path       = GetValue(payload, DropboxPathKey),
                       userName   = payload.Value <string>("dropbox_username") ?? "Dropbox",
                       email      = payload.Value <string>("dropbox_email");

                dropboxInfo = DropboxInfo.CreateV2Info(path, oauthToken, GetRepositoryType());
                dropboxInfo.DeployInfo.UserName = userName;
                dropboxInfo.DeployInfo.Email    = email;
                message = String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_Synchronizing);
            }

            if (dropboxInfo != null)
            {
                deploymentInfo = dropboxInfo;

                // Temporary deployment
                deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                    authorName: dropboxInfo.DeployInfo.UserName,
                    authorEmail: dropboxInfo.DeployInfo.Email,
                    message: message
                    );

                return(DeployAction.ProcessDeployment);
            }

            deploymentInfo = null;
            return(DeployAction.UnknownPayload);
        }
Esempio n. 41
0
        private void PerformDeployment(DeploymentInfo deploymentInfo)
        {
            bool hasPendingDeployment;

            do
            {
                string targetBranch = _settings.GetBranch();
                hasPendingDeployment = false;

                using (_tracer.Step("Performing fetch based deployment"))
                {
                    // create temporary deployment before the actual deployment item started
                    // this allows portal ui to readily display on-going deployment (not having to wait for fetch to complete).
                    // in addition, it captures any failure that may occur before the actual deployment item started
                    ChangeSet tempChangeSet;
                    IDisposable tempDeployment = _deploymentManager.CreateTemporaryDeployment(
                                                    Resources.ReceivingChanges,
                                                    out tempChangeSet,
                                                    deploymentInfo.TargetChangeset,
                                                    deploymentInfo.Deployer);

                    ILogger innerLogger = null;
                    try
                    {
                        ILogger logger = _deploymentManager.GetLogger(tempChangeSet.Id);

                        // Fetch changes from the repository
                        innerLogger = logger.Log(Resources.FetchingChanges);

                        IRepository repository = _repositoryFactory.EnsureRepository(deploymentInfo.RepositoryType);

                        deploymentInfo.Handler.Fetch(repository, deploymentInfo, targetBranch, innerLogger);

                        // set to null as Deploy() below takes over logging
                        innerLogger = null;

                        // In case the commit or perhaps fetch do no-op.
                        if (deploymentInfo.TargetChangeset != null && ShouldDeploy(repository, deploymentInfo, targetBranch))
                        {
                            // Perform the actual deployment
                            var changeSet = repository.GetChangeSet(targetBranch);

                            // Here, we don't need to update the working files, since we know Fetch left them in the correct state
                            _deploymentManager.Deploy(repository, changeSet, deploymentInfo.Deployer, clean: false, needFileUpdate: false);
                        }

                        if (MarkerFileExists())
                        {
                            _tracer.Trace("Pending deployment marker file exists");

                            hasPendingDeployment = DeleteMarkerFile();

                            if (hasPendingDeployment)
                            {
                                _tracer.Trace("Deleted marker file");

                                hasPendingDeployment = deploymentInfo.IsReusable;
                            }
                            else
                            {
                                _tracer.TraceError("Failed to delete marker file");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (innerLogger != null)
                        {
                            innerLogger.Log(ex);
                        }

                        // In case the commit or perhaps fetch do no-op.
                        if (deploymentInfo.TargetChangeset != null)
                        {
                            IDeploymentStatusFile statusFile = _status.Open(deploymentInfo.TargetChangeset.Id);
                            if (statusFile != null)
                            {
                                statusFile.MarkFailed();
                            }
                        }

                        throw;
                    }

                    // only clean up temp deployment if successful
                    tempDeployment.Dispose();
                }

            } while (hasPendingDeployment);
        }
Esempio n. 42
0
 public virtual void Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger)
 {
     // Sync with dropbox
     var dropboxInfo = ((DropboxInfo)deploymentInfo);
     deploymentInfo.TargetChangeset = _dropBoxHelper.Sync(dropboxInfo, targetBranch, logger);
 }
 public Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger)
 {
     repository.ClearLock();
     repository.FetchWithoutConflict(deploymentInfo.RepositoryUrl, targetBranch);
     return(_completed);
 }
Esempio n. 44
0
        // key goal is to create background tracer that is independent of request.
        public static void PerformBackgroundDeployment(DeploymentInfo deployInfo, IEnvironment environment, IDeploymentSettingsManager settings, TraceLevel traceLevel, Uri uri, IDisposable tempDeployment, IAutoSwapHandler autoSwapHandler, ChangeSet tempChangeSet)
        {
            var tracer = traceLevel <= TraceLevel.Off ? NullTracer.Instance : new XmlTracer(environment.TracePath, traceLevel);
            var traceFactory = new TracerFactory(() => tracer);

            var backgroundTrace = tracer.Step(XmlTracer.BackgroundTrace, new Dictionary<string, string>
            {
                {"url", uri.AbsolutePath},
                {"method", "POST"}
            });

            Task.Run(() =>
            {
                try
                {
                    // lock related
                    string lockPath = Path.Combine(environment.SiteRootPath, Constants.LockPath);
                    string deploymentLockPath = Path.Combine(lockPath, Constants.DeploymentLockFile);
                    string statusLockPath = Path.Combine(lockPath, Constants.StatusLockFile);
                    string hooksLockPath = Path.Combine(lockPath, Constants.HooksLockFile);
                    var statusLock = new LockFile(statusLockPath, traceFactory);
                    var hooksLock = new LockFile(hooksLockPath, traceFactory);
                    var deploymentLock = new DeploymentLockFile(deploymentLockPath, traceFactory);

                    var analytics = new Analytics(settings, new ServerConfiguration(), traceFactory);
                    var deploymentStatusManager = new DeploymentStatusManager(environment, analytics, statusLock);
                    var repositoryFactory = new RepositoryFactory(environment, settings, traceFactory);
                    var siteBuilderFactory = new SiteBuilderFactory(new BuildPropertyProvider(), environment);
                    var webHooksManager = new WebHooksManager(tracer, environment, hooksLock);
                    var functionManager = new FunctionManager(environment, traceFactory);
                    var deploymentManager = new DeploymentManager(siteBuilderFactory, environment, traceFactory, analytics, settings, deploymentStatusManager, deploymentLock, NullLogger.Instance, webHooksManager, autoSwapHandler, functionManager);
                    var fetchHandler = new FetchHandler(tracer, deploymentManager, settings, deploymentStatusManager, deploymentLock, environment, null, repositoryFactory, null);

                    // Perform deployment
                    var acquired = deploymentLock.TryLockOperation(() =>
                    {
                        fetchHandler.PerformDeployment(deployInfo, tempDeployment, tempChangeSet).Wait();
                    }, TimeSpan.Zero);

                    if (!acquired)
                    {
                        if (tempDeployment != null)
                        {
                            tempDeployment.Dispose();
                        }

                        using (tracer.Step("Update pending deployment marker file"))
                        {
                            // REVIEW: This makes the assumption that the repository url is the same.
                            // If it isn't the result would be buggy either way.
                            FileSystemHelpers.SetLastWriteTimeUtc(fetchHandler._markerFilePath, DateTime.UtcNow);
                        }
                    }
                }
                catch (Exception ex)
                {
                    tracer.TraceError(ex);
                }
                finally
                {
                    backgroundTrace.Dispose();
                }
            });
        }
Esempio n. 45
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (request.UserAgent != null &&
                request.UserAgent.StartsWith("Codebasehq", StringComparison.OrdinalIgnoreCase))
            {
                GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
                deploymentInfo = gitDeploymentInfo;
                return(deploymentInfo == null || IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment);
            }

            return(DeployAction.UnknownPayload);
        }
Esempio n. 46
0
 public async Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
 {
     var oneDriveInfo = (OneDriveInfo)deploymentInfo;
     _oneDriveHelper.Logger = logger;
     oneDriveInfo.TargetChangeset = await _oneDriveHelper.Sync(oneDriveInfo, repository, tracer);
 }
Esempio n. 47
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            if (!String.IsNullOrEmpty(payload.Value <string>("NewCursor")))
            {
                var dropboxInfo = new DropboxInfo(payload, _settings.IsNullRepository() ? RepositoryType.None : RepositoryType.Git);
                deploymentInfo = dropboxInfo;

                // Temporary deployment
                deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                    authorName: dropboxInfo.DeployInfo.UserName,
                    authorEmail: dropboxInfo.DeployInfo.Email,
                    message: String.Format(CultureInfo.CurrentUICulture, Resources.Dropbox_Synchronizing, dropboxInfo.DeployInfo.Deltas.Count())
                    );

                return(DeployAction.ProcessDeployment);
            }

            return(DeployAction.UnknownPayload);
        }
Esempio n. 48
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;

            var publisherId = payload.Value <string>("publisherId");

            if (String.Equals(publisherId, "tfs", StringComparison.OrdinalIgnoreCase))
            {
                deploymentInfo = GetDeploymentInfo(request, payload, targetBranch);
                return(DeployAction.ProcessDeployment);
            }

            return(DeployAction.UnknownPayload);
        }
Esempio n. 49
0
        public DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            deploymentInfo = null;
            string url = payload.Value <string>("RepositoryUrl");

            if (string.IsNullOrWhiteSpace(url) || !url.ToLowerInvariant().Contains("api.onedrive.com"))
            {
                return(DeployAction.UnknownPayload);
            }

            /*
             *   Expecting payload to be:
             *   {
             *      "RepositoryUrl": "xxx",
             *      "AccessToken": "xxx"
             *   }
             */
            string accessToken = payload.Value <string>("AccessToken");

            // keep email and name, so that can be re-used in later commit
            OneDriveInfo oneDriveInfo = new OneDriveInfo()
            {
                Deployer      = "OneDrive",
                RepositoryUrl = url,
                AccessToken   = accessToken,
                AuthorName    = _settings.GetValue("authorName"),
                AuthorEmail   = _settings.GetValue("authorEmail")
            };

            deploymentInfo = oneDriveInfo;

            deploymentInfo.TargetChangeset = DeploymentManager.CreateTemporaryChangeSet(
                authorName: oneDriveInfo.AuthorName,
                authorEmail: oneDriveInfo.AuthorEmail,
                message: String.Format(CultureInfo.CurrentUICulture, Resources.OneDrive_Synchronizing)
                );

            return(DeployAction.ProcessDeployment);
        }
Esempio n. 50
0
        private DeploymentInfo GetDeploymentInfo(JObject payload, string targetBranch)
        {
            var repository = payload.Value<JObject>("repository");
            var commits = payload.Value<JArray>("commits");

            // Identify the last commit for the target branch.
            JObject targetCommit = (from commit in commits
                                    where targetBranch.Equals(commit.Value<string>("branch"), StringComparison.OrdinalIgnoreCase)
                                    orderby commit.Value<int>("revision") descending
                                    select (JObject)commit).FirstOrDefault();

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

            string accessToken = _settings.GetValue("kiln.accesstoken");

            var author = targetCommit.Value<string>("author");

            // if the commit was via an access token we don't want to show that for security reasons
            Guid authorGuid;
            if (Guid.TryParse(author, out authorGuid))
            {
                author = "System Account";
            }

            var info = new DeploymentInfo
            {
                Deployer = "Kiln",
                RepositoryUrl = repository.Value<string>("url"),
                RepositoryType = RepositoryType.Mercurial,
                IsContinuous = true,
                TargetChangeset = new ChangeSet(
                    id: targetCommit.Value<string>("id"),
                    authorName: ParseNameFromAuthor(author),
                    authorEmail: ParseEmailFromAuthor(author),
                    message: (targetCommit.Value<string>("message") ?? String.Empty).Trim(),
                    timestamp: new DateTimeOffset(DateTime.Parse(targetCommit.Value<string>("timestamp")), TimeSpan.Zero)
                    )
            };

            bool isPrivate = !String.IsNullOrWhiteSpace(accessToken); // assume a private repo if an access token is provided
            if (isPrivate)
            {
                var uri = new UriBuilder(info.RepositoryUrl)
                {
                    Scheme = "https",
                    Port = -1,
                    UserName = accessToken,
                    Password = "******" // kiln doesn't use the password when using an access token
                };

                info.RepositoryUrl = uri.ToString();
            }

            return info;
        }
Esempio n. 51
0
        public override DeployAction TryParseDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch, out DeploymentInfo deploymentInfo)
        {
            GitDeploymentInfo gitDeploymentInfo = GetDeploymentInfo(request, payload, targetBranch);

            if (gitDeploymentInfo != null && gitDeploymentInfo.IsValid())
            {
                deploymentInfo = gitDeploymentInfo;
                return(IsDeleteCommit(gitDeploymentInfo.NewRef) ? DeployAction.NoOp : DeployAction.ProcessDeployment);
            }
            deploymentInfo = null;
            return(DeployAction.UnknownPayload);
        }
Esempio n. 52
-1
 public virtual async Task Fetch(IRepository repository, DeploymentInfo deploymentInfo, string targetBranch, ILogger logger, ITracer tracer)
 {
     // (A)sync with dropbox
     var dropboxInfo = (DropboxInfo)deploymentInfo;
     _dropBoxHelper.Logger = logger;
     deploymentInfo.TargetChangeset = await _dropBoxHelper.Sync(dropboxInfo, targetBranch, repository, tracer);
 }