private CommitDTO Parse(IEnumerable <string> commitLines) { try { // TODO: this code should be refactored and replaced with womething like // a visitor pattern for the parser. Additionally, it should not fail // when an unknown token occurs. if (commitLines?.Count() > 3) { var commit = new CommitDTO() { Id = this.commitIdParser.Parse(commitLines.ElementAt(0)) }; var authorInfo = this.commitAuthorParser.Parse(commitLines.ElementAt(1)); commit.AuthorName = authorInfo.name; commit.AuthorEmail = authorInfo.email; commit.Date = commitDateParser.Parse(commitLines.ElementAt(2)); commit.Message = commitMessageParser.Parse(commitLines.Skip(3)); return(commit); } else { logger.Error("Insufficient information for a commit: \n" + string.Join("\n", commitLines)); return(null); } } catch (Exception exp) { logger.Error("Commit parsing exception while parsing: \n" + string.Join("\n", commitLines), exp); return(null); } }
public async Task <IEnumerable <CommitDTO> > GetCommits(string url, int page = 0, int per_page = 10) { try { var repoInfo = ParseGithubUrl(url); string path = $"/repos/{repoInfo.owner}/{repoInfo.repo}/commits?page={page}&per_page={per_page}"; var response = await this.githubClient.GetAsync(path).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = JsonConvert.DeserializeObject <JArray>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); return(CommitMapper.Map(json)); } else { logger.Error($"Insuccess status code while getting commits from Github. StatusCode: {response.StatusCode}"); throw new CommitFetchingOperationException($"Insuccess status code while getting commits from Github. StatusCode: {response.StatusCode}"); } } catch (Exception exp) { logger.Error("Exception while getting commits from Github."); throw new CommitFetchingOperationException($"Exception while getting commits from Github.", exp); } }
public async Task <IEnumerable <CommitDTO> > GetCommits(string url, int page = 0, int per_page = 10) { try { // if the project was already cloned let's use pull... it's faster bool gitPull = true; string projectPath = this.GetProjectPath(url); if (!Directory.Exists(projectPath)) { logger.Debug($"Preparing to clone new project {url}"); Directory.CreateDirectory(projectPath); gitPull = false; } else { logger.Debug($"The project {url} was already cloned."); } // formating command with input parameters string command = string.Format(gitPull ? this.gitPull : this.gitClone, projectPath, url, page * per_page + per_page, page * per_page, per_page); string gitLog = await this.ExecuteShellCommand(command); // TODO: remove this HACK... the STDOUT must not contain the command that was executed gitLog = gitLog.Remove(0, gitLog.IndexOf("commit ")); return(string.IsNullOrWhiteSpace(gitLog) ? new List <CommitDTO>() : this.commitParser.Parse(gitLog)); } catch (Exception exp) { logger.Error($"Unexpected error while getting and parsing commits in {nameof(GitCLICommitFetcher)}", exp); throw new CommitFetchingOperationException("Unexpected error while getting and parsing commits. Check inner exception.", exp); } }