Beispiel #1
0
        /// <summary>
        /// Downloads a single file from VSTS and saves it to the specified location.
        /// </summary>
        /// <param name="authentication">The user's authentication credentials.</param>
        /// <param name="project">The VSTS (team) Project.</param>
        /// <param name="repo">The name of the Git Repo containing the file.</param>
        /// <param name="branch"></param>
        /// <param name="fileToDownload">The Path to the file (within the Repo) to be downloaded.</param>
        /// <returns><c>true</c> if the file is downloaded successfully; otherwise, <c>false</c>.</returns>
        public byte[] DownloadBinaryFile(BasicAuthentication authentication, string project, string repo, string branch, string fileToDownload)
        {
            var restHttpClient = new RestHttpClient();
            var url            = $"{authentication.AccountUrl}/{project}/_apis/git/repositories/{repo}/items?api-version=1.0&versionType=branch&version={branch}&scopePath={fileToDownload}";
            var buffer         = new byte[0x4000];

            byte[] data = null;

            using (var response = restHttpClient.RequestFile(authentication, url))
            {
                if (response != null)
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            int read;

                            while (responseStream != null && (read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                ms.Write(buffer, 0, read);
                            }

                            data = ms.ToArray();
                        }
                    }
                }
            }

            return(data);
        }
Beispiel #2
0
        /// <summary>
        /// Downloads a single file from VSTS and saves it to the specified location.
        /// </summary>
        /// <param name="authentication">The user's authentication credentials.</param>
        /// <param name="project">The VSTS (team) Project.</param>
        /// <param name="repo">The name of the Git Repo containing the file.</param>
        /// <param name="branch"></param>
        /// <param name="fileToDownload">The Path to the file (within the Repo) to be downloaded.</param>
        public string GetFileContents(BasicAuthentication authentication, string project, string repo, string branch, string fileToDownload)
        {
            Logger.LogMessage($"Retrieving file contents for '{fileToDownload}'");

            var fileContents   = string.Empty;
            var restHttpClient = new RestHttpClient();
            var url            = $"{authentication.AccountUrl}/{project}/_apis/git/repositories/{repo}/items?api-version=1.0&versionType=branch&version={branch}&scopePath={fileToDownload}";

            using (var response = restHttpClient.RequestFile(authentication, url))
            {
                if (response != null)
                {
                    fileContents = GetResponseText(response);
                }
            }

            return(fileContents);
        }