Exemplo n.º 1
0
        /// <summary>
        /// Creates a repository for the given student, and pushes the non-test files
        /// from the source project to the new repository.
        /// </summary>
        private async Task <CreateAndPushResult> CreateAndPushAsync(
            Project project,
            ClassroomMembership student,
            string webhookUrl,
            bool overwriteIfSafe,
            ICollection <GitHubTeam> teams,
            ICollection <GitHubRepository> repositories,
            IArchive templateContents)
        {
            string orgName  = project.Classroom.GitHubOrganization;
            string repoName = $"{project.Name}_{student.GitHubTeam}";

            try
            {
                var  repository = repositories.SingleOrDefault(repo => repo.Name == repoName);
                var  team       = teams.First(teamCandidate => teamCandidate.Name == student.GitHubTeam);
                bool repositoryAlreadyExisted = (repository != null);

                if (repositoryAlreadyExisted)
                {
                    if (!overwriteIfSafe)
                    {
                        return(CreateAndPushResult.Exists);
                    }

                    var commits = await _repoClient.GetAllCommitsAsync(orgName, repoName);

                    if (commits.Count > c_numInitialCommits)
                    {
                        return(CreateAndPushResult.Exists);
                    }
                }
                else
                {
                    repository = await _repoClient.CreateRepositoryAsync
                                 (
                        orgName,
                        repoName,
                        team,
                        overwrite : false
                                 );

                    var staffTeam = GetStaffTeam(project.Classroom, teams);
                    if (staffTeam != null)
                    {
                        await _teamClient.AddRepositoryAsync(orgName, repoName, staffTeam);
                    }
                }

                await _repoClient.EnsurePushWebhookAsync(repository, webhookUrl);

                await _repoClient.OverwriteRepositoryAsync
                (
                    repository,
                    c_starterCommitMessage,
                    templateContents,
                    entry => project.GetFileType(entry) != FileType.Private,
                    entry => project.GetFileType(entry) == FileType.Immutable
                );

                return(repositoryAlreadyExisted
                                        ? CreateAndPushResult.Overwritten
                                        : CreateAndPushResult.Created);
            }
            catch (Exception ex)
            {
                _logger.LogError
                (
                    (EventId)0,
                    ex,
                    "Failed to create repository {RepoName} in organization {Org}.", repoName, orgName
                );

                return(CreateAndPushResult.Failed);
            }
        }