//public async Task<byte[]> ReadBinaryFileAsync(string url, string path) //{ // long repositoryId = await _idLookup.GetIdAsync(url); // var content = await _client.GetFileContentAsync(repositoryId, path); // return Convert.FromBase64String(content.EncodedContent); //} public async Task <string?> ReadStringFileAsync(string url, string path) { string branchName = url.ExtractBranchName(); url = url.StripBranchName(branchName); long repositoryId = await _idLookup.GetIdAsync(url); string?content = await _client.GetStringContentAsync(repositoryId, path, branchName); return(content); }
public async Task <string> GetInstallUrlAsync(string url) { string appName = "GitScribe-Develop"; // https://github.com/apps/YOUR_APP_NAME/installations/new/permissions?suggested_target_id=ID_OF_USER_OR_ORG&repository_ids[]=REPO_A_ID&repository_ids[]=REPO_B_ID var currentUser = await _client.User.Current(); int userId = currentUser.Id; long repoId = await _idLookup.GetIdAsync(url); string installUrl = $"https://github.com/apps/{appName}/installations/new/permissions?suggested_target_id={userId}&repository_ids[]={repoId}"; //foreach (string repoId in repositoryIds) //{ // url += $"&repository_ids[]={repoId}"; //} return(installUrl); }
public async Task <IEnumerable <string> > SearchAsync(string url, string query) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException(nameof(url)); } if (string.IsNullOrEmpty(query)) { throw new ArgumentNullException(nameof(query)); } string branchName = url.ExtractBranchName(); url = url.StripBranchName(branchName); var values = GitHubUrlExtractor.ExtractFromUrl(url); string username = values.Item1; string reponame = values.Item2; //var req = new SearchCodeRequest(query, username, reponame); //if (string.IsNullOrEmpty(branchName)) //{ // var results = await _client.Search.SearchCode(req); // // Github is searching the contents of the files too // // Github seems generous with the match, so do a secondary filter // var filteredResults = results.Items.Select(i => i.Path); // return filteredResults; //} //else { if (string.IsNullOrEmpty(branchName)) { branchName = "master"; } long repositoryId = await _idLookup.GetIdAsync(url); // Search in a specific branch TreeResponse?tree; try { tree = await _client.Git.Tree.GetRecursive(repositoryId, branchName); } catch (Octokit.NotFoundException) { _log.LogWarning($"Repository not found : {url}"); return(Enumerable.Empty <string>()); } var filePaths = tree.Tree.Select(item => item.Path); if (!string.IsNullOrEmpty(query)) { if (query.StartsWith("extension:")) { string extension = query.Replace("extension:", ""); filePaths = filePaths.Where(path => Path.GetExtension(path) == extension); } else { var splits = query.Split('+'); filePaths = filePaths.Where(path => path.Contains(splits[0])); } } return(filePaths); } }