Exemplo n.º 1
0
        /// <summary>
        /// Downloads a file from a repository.
        /// Any subsequent attempt to download the same file just return content from the local cache.
        /// See <see cref="ClearCache"/>.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="filePath">The file path from the root of the repository.</param>
        /// <returns>The file content, null if an error occurred.</returns>
        public static async Task <byte[]?> DownloadFile(GitHubRepository repository, string filePath)
        {
            byte[]? Result = null;

            if (await Connect())
            {
                string Login = repository.Source.Owner.Login;
                string Name  = repository.Source.Name;

                string UpdatedFilePath   = filePath.Replace("\\", "/");
                string RepositoryAddress = $"{Login}/{Name}";

                Debug.WriteLine($"Downloading {RepositoryAddress} {UpdatedFilePath}");

                if (DownloadCache.ContainsKey(RepositoryAddress))
                {
                    Dictionary <string, byte[]> RepositoryCache = DownloadCache[RepositoryAddress];
                    if (RepositoryCache.ContainsKey(UpdatedFilePath))
                    {
                        Debug.WriteLine($"  (Already downloaded)");
                        return(RepositoryCache[UpdatedFilePath]);
                    }
                }

                try
                {
                    Result = await Client.Repository.Content.GetRawContent(Login, Name, UpdatedFilePath);
                }
                catch (Exception e) when(e is NotFoundException)
                {
                    Debug.WriteLine("(not found)");
                }

                if (Result != null)
                {
                    if (!DownloadCache.ContainsKey(RepositoryAddress))
                    {
                        DownloadCache.Add(RepositoryAddress, new Dictionary <string, byte[]>());
                    }

                    Dictionary <string, byte[]> RepositoryCache = DownloadCache[RepositoryAddress];
                    if (!RepositoryCache.ContainsKey(UpdatedFilePath))
                    {
                        RepositoryCache.Add(UpdatedFilePath, Result);
                    }
                }

                return(Result);
            }

            return(Result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Enumerate files in a repository.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="path">The path where to start looking.</param>
        /// <param name="searchPattern">The search pattern.</param>
        /// <returns>A table of file names and associated stream content.</returns>
        public static async Task <Dictionary <string, Stream?> > EnumerateFiles(GitHubRepository repository, string path, string searchPattern)
        {
            Dictionary <string, Stream?> Result = new();

            if (await Connect())
            {
                string Login = repository.Source.Owner.Login;
                string Name  = repository.Source.Name;

                SearchCodeRequest Request = new SearchCodeRequest()
                {
                    Path = path, FileName = searchPattern
                };
                Request.Repos.Add(Login, Name);
                SearchCodeResult SearchResult = await Client.Search.SearchCode(Request);

                await GetFileStreams(Login, Name, SearchResult, Result);
            }

            return(Result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Enumerates branches of a repository.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <returns>A list of branches.</returns>
        public static async Task <List <GitHubBranch> > EnumerateBranches(GitHubRepository repository)
        {
            List <GitHubBranch> Result = new();

            if (await Connect())
            {
                IReadOnlyList <Branch> BranchList = await Client.Repository.Branch.GetAll(repository.Source.Id);

                foreach (Branch Branch in BranchList)
                {
                    GitHubBranch NewBranch = new(repository, Branch);
                    Result.Add(NewBranch);

                    if (Branch.Name == "master")
                    {
                        repository.SetMasterCommit(new GitHubCommit(NewBranch, Branch.Commit));
                        break;
                    }
                }
            }

            return(Result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GitHubBranch"/> class.
 /// </summary>
 /// <param name="repository">The repository with this branch.</param>
 /// <param name="source">The Octokit branch.</param>
 internal GitHubBranch(GitHubRepository repository, Branch source)
 {
     Repository = repository;
     Source     = source;
 }