Beispiel #1
0
        private static async Task WriteContributorsToRepo(string username, string password)
        {
            var nameOfThankyouBranch        = "thankyou";
            var contributorsHeader          = _parsedOptions.acknowledgementSection;
            var fileHoldingContributorsInfo = _parsedOptions.fileInRepoForAcknowledgement;
            var githubRepoOwner             = _parsedOptions.githubRepoOwner;
            var githubRepoName = _parsedOptions.githubRepoName;

            string tempPathGitFolder = CreateTempFolderForTheRepo();

            // Login to GitHub with Octokit
            var githubClient = new GitHubClient(new ProductHeaderValue(username));

            githubClient.Credentials = new Octokit.Credentials(password);

            var githubRepo = await githubClient.Repository.Get(githubRepoOwner, githubRepoName);

            var gitCredentialsHandler = new CredentialsHandler(
                (url, usernameFromUrl, types) =>
                new UsernamePasswordCredentials()
            {
                Username = username,
                Password = password
            });

            var cloneOptions = new CloneOptions();

            cloneOptions.CredentialsProvider = gitCredentialsHandler;
            LibGit2Sharp.Repository.Clone(githubRepo.CloneUrl, tempPathGitFolder, cloneOptions);

            using (var repo = new LibGit2Sharp.Repository(tempPathGitFolder))
            {
                var remote        = repo.Network.Remotes["origin"];
                var defaultBranch = repo.Branches.FirstOrDefault(b => b.IsCurrentRepositoryHead == true);
                var refSpecs      = remote.FetchRefSpecs.Select(x => x.Specification);

                var remoteThankYouBranch = repo.Branches.FirstOrDefault(b => b.FriendlyName == repo.Network.Remotes.FirstOrDefault()?.Name + "/" + nameOfThankyouBranch);

                if (remoteThankYouBranch != null)
                {
                    Commands.Checkout(repo, remoteThankYouBranch);
                }

                if (repo.Head.FriendlyName != nameOfThankyouBranch)
                {
                    var newThankyouBranch = repo.CreateBranch(nameOfThankyouBranch);
                    repo.Branches.Update(newThankyouBranch,
                                         b => b.UpstreamBranch = newThankyouBranch.CanonicalName,
                                         b => b.Remote         = repo.Network.Remotes.First().Name);
                    Commands.Checkout(repo, newThankyouBranch);
                }


                var pathToReadme = Path.Combine(tempPathGitFolder, fileHoldingContributorsInfo);

                // Change the file and save it
                var inputLines = await File.ReadAllLinesAsync(pathToReadme);

                var outputLines = MarkdownProcessor.AddContributorsToMarkdownFile(inputLines, _contributorsToday);
                await File.WriteAllLinesAsync(pathToReadme, outputLines);

                var status = repo.RetrieveStatus(fileHoldingContributorsInfo);
                if (status == FileStatus.ModifiedInWorkdir)
                {
                    try
                    {
                        // Commit the file to the repo, on a non-master branch
                        repo.Index.Add(fileHoldingContributorsInfo);
                        repo.Index.Write();

                        var gitAuthorName  = _parsedOptions.gitAuthorName;
                        var gitAuthorEmail = _parsedOptions.gitAuthorEmail;

                        // Create the committer's signature and commit
                        var author    = new LibGit2Sharp.Signature(gitAuthorName, gitAuthorEmail, DateTime.Now);
                        var committer = author;

                        // Commit to the repository
                        var commit = repo.Commit("A new list of awesome contributors", author, committer);

                        //Push the commit to origin
                        LibGit2Sharp.PushOptions options = new LibGit2Sharp.PushOptions();
                        options.CredentialsProvider = gitCredentialsHandler;
                        repo.Network.Push(repo.Head, options);

                        // Check if there is already a PR exist for the same branch
                        var prsOfRepo = await githubClient.PullRequest.GetAllForRepository(githubRepoOwner, githubRepoName, new PullRequestRequest { State = ItemStateFilter.Open });

                        var currentPR = prsOfRepo.FirstOrDefault(x => x.Head.Label == $"{githubRepoOwner}:{nameOfThankyouBranch}");

                        if (currentPR == null)
                        {
                            //  Create a PR on the repo for the branch "thank you"
                            await githubClient.PullRequest.Create(username, githubRepoName, new NewPullRequest("Give credit for people on Twitch chat", nameOfThankyouBranch, defaultBranch.FriendlyName));
                        }
                        else
                        {
                            _logger.LogWarning($"Pull Rrequest is already created. Check PR {currentPR.Id}");
                        }
                    }
                    catch (Exception ex)
                    {
                        // This exception might be caused by an already existing PR for this branch. In this case it's ok, otherwise we will just log it.
                        _logger.LogError(ex, $"Ops, We couldn't create the PR. But here is the list of contributors you were trying to add {string.Join(", ", _contributorsToday.Select(c => c.Name))}");
                    }
                }
                else
                {
                    _logger.LogInformation("There was no changes on the README file, this means that either there was no contributors, or all today's contributors are duplicates.");
                }
            }
        }
Beispiel #2
0
        private static async Task WriteContributorsToRepo(string username, string password)
        {
            var nameOfThankyouBranch = "thankyou";
            var repoUrl                     = _parsedOptions.gitRepositoryUrl;
            var contributorsHeader          = _parsedOptions.acknowledgementSection;
            var fileHoldingContributorsInfo = _parsedOptions.fileInRepoForAcknowledgement;

            string tempPathGitFolder = CreateTempFolderForTheRepo();

            var gitCredentialsHandler = new CredentialsHandler(
                (url, usernameFromUrl, types) =>
                new UsernamePasswordCredentials()
            {
                Username = username,
                Password = password
            });

            var cloneOptions = new CloneOptions();

            cloneOptions.CredentialsProvider = gitCredentialsHandler;
            LibGit2Sharp.Repository.Clone(repoUrl, tempPathGitFolder, cloneOptions);

            using (var repo = new LibGit2Sharp.Repository(tempPathGitFolder))
            {
                var remote        = repo.Network.Remotes["origin"];
                var defaultBranch = repo.Branches.FirstOrDefault(b => b.IsCurrentRepositoryHead == true);
                var refSpecs      = remote.FetchRefSpecs.Select(x => x.Specification);

                var remoteThankYouBranch = repo.Branches.FirstOrDefault(b => b.FriendlyName == repo.Network.Remotes.FirstOrDefault()?.Name + "/" + nameOfThankyouBranch);

                if (remoteThankYouBranch != null)
                {
                    Commands.Checkout(repo, remoteThankYouBranch);
                }

                if (repo.Head.FriendlyName != nameOfThankyouBranch)
                {
                    var newThankyouBranch = repo.CreateBranch(nameOfThankyouBranch);
                    repo.Branches.Update(newThankyouBranch,
                                         b => b.UpstreamBranch = newThankyouBranch.CanonicalName,
                                         b => b.Remote         = repo.Network.Remotes.First().Name);
                    Commands.Checkout(repo, newThankyouBranch);
                }


                var pathToReadme = Path.Combine(tempPathGitFolder, fileHoldingContributorsInfo);

                // Change the file and save it
                var inputLines = await File.ReadAllLinesAsync(pathToReadme);

                var outputLines = MarkdownProcessor.AddContributorsToMarkdownFile(inputLines, _contributorsToday);
                await File.WriteAllLinesAsync(pathToReadme, outputLines);

                var status = repo.RetrieveStatus(fileHoldingContributorsInfo);
                if (status == FileStatus.ModifiedInWorkdir)
                {
                    // Commit the file to the repo, on a non-master branch
                    repo.Index.Add(fileHoldingContributorsInfo);
                    repo.Index.Write();

                    var gitAuthorName  = _parsedOptions.gitAuthorName;
                    var gitAuthorEmail = _parsedOptions.gitAuthorEmail;

                    // Create the committer's signature and commit
                    var author    = new LibGit2Sharp.Signature(gitAuthorName, gitAuthorEmail, DateTime.Now);
                    var committer = author;

                    // Commit to the repository
                    var commit = repo.Commit("A new list of awesome contributors", author, committer);

                    //Push the commit to origin
                    LibGit2Sharp.PushOptions options = new LibGit2Sharp.PushOptions();
                    options.CredentialsProvider = gitCredentialsHandler;
                    repo.Network.Push(repo.Head, options);

                    // Login to GitHub with Octokit
                    var githubClient = new GitHubClient(new ProductHeaderValue(username));
                    githubClient.Credentials = new Octokit.Credentials(password);

                    try
                    {
                        //  Create a PR on the repo for the branch "thank you"
                        await githubClient.PullRequest.Create(username, "thankyou", new NewPullRequest("Give credit for people on Twitch chat", nameOfThankyouBranch, defaultBranch.FriendlyName));
                    }
                    catch (Exception ex)
                    {
                        // It's alright, the PR is already there. No harm is done.
                    }
                }
            }
        }