Beispiel #1
0
        public async Task CreateCommit(long repoId, string directoryToAdd, string commitText)
        {
            var headMasterRef   = "heads/master";
            var github          = GetClient();
            var masterReference = await github.Git.Reference.Get(repoId, headMasterRef);

            var latestCommit = await GetLatestSHA(repoId, headMasterRef);

            var nt = new NewTree {
                BaseTree = latestCommit.Tree.Sha
            };

            string[] filePaths = Directory.GetFiles(string.Format(@"{0}\", directoryToAdd));

            foreach (var filePath in filePaths)
            {
                var linesOfCode = await File.ReadAllLinesAsync(filePath);

                var newTreeItem = new NewTreeItem {
                    Mode = "100644", Type = TreeType.Blob, Content = linesOfCode.ToString(), Path = filePath
                };
                nt.Tree.Add(newTreeItem);
            }

            var newTree = await github.Git.Tree.Create(repoId, nt);

            var newCommit = new NewCommit(commitText, newTree.Sha, masterReference.Object.Sha);
            var commit    = await github.Git.Commit.Create(repoId, newCommit);

            await github.Git.Reference.Update(repoId, headMasterRef, new ReferenceUpdate(commit.Sha));
        }
Beispiel #2
0
        private async Task <Reference> PushFix(IGitHubClient client, Repository repository, Commit latest, string jenkinsFile)
        {
            var oldTree = await client.Git.Tree.Get(repository.Owner.Login, repository.Name, latest.Sha).ConfigureAwait(false);

            var newTree = new NewTree
            {
                BaseTree = oldTree.Sha
            };

            var blobReference = await CreateBlob(client, repository, jenkinsFile).ConfigureAwait(false);

            var treeItem = new NewTreeItem()
            {
                Path = JenkinsFileName,
                Mode = FileMode,
                Type = TreeType.Blob,
                Sha  = blobReference.Sha
            };

            newTree.Tree.Add(treeItem);

            var createdTree = await client.Git.Tree.Create(repository.Owner.Login, repository.Name, newTree).ConfigureAwait(false);

            var commit         = new NewCommit($"Update {LibraryName} to latest version.", createdTree.Sha, new[] { latest.Sha });
            var commitResponse = await client.Git.Commit.Create(repository.Owner.Login, repository.Name, commit).ConfigureAwait(false);

            var refUpdate = new ReferenceUpdate(commitResponse.Sha);

            return(await client.Git.Reference.Update(repository.Owner.Login, repository.Name, $"heads/{_branchName}", refUpdate).ConfigureAwait(false));
        }
Beispiel #3
0
        protected override async Task StoreNewCsvAsync(IEnumerable <MediaDetails> medias, string path)
        {
            var newCsvText = await CsvFileHelper.CsvTextFromMediaAsync(medias);

            var newTreeItem = new NewTreeItem {
                Mode = "100644", Type = TreeType.Blob, Content = newCsvText, Path = path
            };

            newTree.Tree.Add(newTreeItem);
        }
Beispiel #4
0
        private void ConstructTreeInternal(Folder folderInfo, NewTree tree, string parentPath)
        {
            var path = string.Empty;

            if (string.IsNullOrEmpty(folderInfo.Name))
            {
                if (!string.IsNullOrEmpty(parentPath))
                {
                    throw new ArgumentException("Invalid arguments");
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(parentPath))
                {
                    path = parentPath;
                }

                path = path + folderInfo.Name + "/";
            }

            if (folderInfo.Files != null)
            {
                foreach (var file in folderInfo.Files)
                {
                    var filePath = path + file.Name;

                    var newBlob = new NewBlob()
                    {
                        Content  = file.Content,
                        Encoding = EncodingType.Utf8
                    };

                    var createdBlob = client.Git.Blob.Create(repo.Id, newBlob);
                    createdBlob.Wait();
                    var item = new NewTreeItem()
                    {
                        Path = filePath,
                        Mode = Octokit.FileMode.File,
                        Sha  = createdBlob.Result.Sha,
                        Type = TreeType.Blob
                    };

                    tree.Tree.Add(item);
                }
            }

            if (folderInfo.SubFolders != null)
            {
                foreach (var folder in folderInfo.SubFolders)
                {
                    this.ConstructTreeInternal(folder, tree, path);
                }
            }
        }
        /// <summary>
        /// Creates a new project tree.
        /// </summary>
        private async Task <NewTree> GetTreeToPushAsync(
            GitHubRepository repository,
            IList <IArchiveFile> fileContents,
            string baseTreeSha)
        {
            NewTree newTree = new NewTree()
            {
                BaseTree = baseTreeSha
            };

            foreach (var entry in fileContents)
            {
                var newTreeItem = new NewTreeItem()
                {
                    Path = entry.FullPath,
                    Mode = "100644" /*blob*/,
                    Type = TreeType.Blob
                };

                if (entry.Ascii)
                {
                    newTreeItem.Content = entry.GetEncodedData();
                }
                else
                {
                    var blobRef = await RetryGitHubOperationIfNeededAsync
                                  (
                        () => _client.Git.Blob.Create
                        (
                            repository.Owner,
                            repository.Name,
                            new NewBlob()
                    {
                        Encoding = EncodingType.Base64,
                        Content  = entry.GetEncodedData()
                    }
                        )
                                  );

                    newTreeItem.Sha = blobRef.Sha;
                }

                newTree.Tree.Add(newTreeItem);
            }

            return(newTree);
        }
Beispiel #6
0
        private static async Task CommitTranscriptToGithub(TranscriptionResult transcriptionResult, ILogger log)
        {
            var gitHubPAT      = Environment.GetEnvironmentVariable(nameof(AppSettings.GitHubToken), EnvironmentVariableTarget.Process);
            var gitHubUserName = Environment.GetEnvironmentVariable(nameof(AppSettings.GitHubUserName), EnvironmentVariableTarget.Process);
            var gitHubRepoName = Environment.GetEnvironmentVariable(nameof(AppSettings.GitHubRepoName), EnvironmentVariableTarget.Process);
            var headMasterRef  = "heads/master";

            var githubClient = new GitHubClient(new ProductHeaderValue($"{gitHubUserName}-{gitHubRepoName}"))
            {
                Credentials = new Credentials(gitHubPAT, AuthenticationType.Bearer)
            };

            log.LogInformation("Get latest commit sha of master");
            var masterReference = await githubClient.Git.Reference.Get(gitHubUserName, gitHubRepoName, headMasterRef);

            var latestCommit = await githubClient.Git.Commit.Get(gitHubUserName, gitHubRepoName, masterReference.Object.Sha);


            // New Tree
            var nt = new NewTree
            {
                BaseTree = latestCommit.Tree.Sha,
            };

            var newTreeItem = new NewTreeItem
            {
                Mode    = "100644",
                Type    = TreeType.Blob,
                Content = JsonConvert.SerializeObject(transcriptionResult, Formatting.Indented),
                Path    = $"transcripts/{transcriptionResult.PublishDate}_{transcriptionResult.Title}.json"
            };

            nt.Tree.Add(newTreeItem);

            log.LogInformation("Add new tree");


            var newTree = await githubClient.Git.Tree.Create(gitHubUserName, gitHubRepoName, nt);

            log.LogInformation("Add New Commit");
            var newCommit = new NewCommit($"Add {transcriptionResult.Title}", newTree.Sha, masterReference.Object.Sha);
            var commit    = await githubClient.Git.Commit.Create(gitHubUserName, gitHubRepoName, newCommit);

            log.LogInformation(" Update HEAD with the commit");
            await githubClient.Git.Reference.Update(gitHubUserName, gitHubRepoName, headMasterRef, new ReferenceUpdate(commit.Sha));
        }
Beispiel #7
0
        private async Task <List <NewTreeItem> > CreateNewTreeItemList(StringBuilder sb, IEnumerable <string> pathArray, VersionData versionData)
        {
            var newTreeItemList = new List <NewTreeItem>();

            sb.Append("[Update Data] ");

            foreach (var path in pathArray)
            {
                var fileName      = Path.GetFileNameWithoutExtension(path);
                var fileExtension = Path.GetExtension(path);

                if (versionData.GetVersionValue(fileName) == null)
                {
                    versionData.AddNewVerionData(fileName);
                }
                var fileVersionName = versionData.GetNextVersion(fileName);

                var fileFullName = string.Format("{0}{1}", fileVersionName, fileExtension);

                fileExtension = fileExtension.Replace(".", "");

                var fileToBase64 = Convert.ToBase64String(File.ReadAllBytes(path));
                var newBlob      = new NewBlob
                {
                    Encoding = EncodingType.Base64,
                    Content  = fileToBase64
                };

                var newBlobRef = await Client.Git.Blob.Create(OwnerSpaceName, RepositoryName, newBlob);

                var newTreeItem = new NewTreeItem
                {
                    Path = string.Format("{0}/{1}/{2}/{3}", BaseDataPath, fileExtension, fileName, fileFullName),
                    Mode = "100644",
                    Type = TreeType.Blob,
                    Sha  = newBlobRef.Sha
                };

                newTreeItemList.Add(newTreeItem);
                sb.AppendFormat(" {0} /", fileFullName);
            }

            return(newTreeItemList);
        }
Beispiel #8
0
    static NewTree BuildNewTreeFrom(TreeResponse destinationParentTree)
    {
        var newTree = new NewTree();

        foreach (var treeItem in destinationParentTree.Tree)
        {
            var newTreeItem = new NewTreeItem
            {
                Mode = treeItem.Mode,
                Path = treeItem.Path,
                Sha  = treeItem.Sha,
                Type = treeItem.Type.Value
            };

            newTree.Tree.Add(newTreeItem);
        }

        return(newTree);
    }
Beispiel #9
0
        public async Task CommitAsync()
        {
            var owner         = "dotnet";
            var repo          = "apireviews";
            var branch        = "heads/master";
            var markdown      = $"# Quick Reviews {Date.ToString("d")}\n\n{GetMarkdown()}";
            var path          = $"{Date.Year}/{Date.Month:00}-{Date.Day:00}-quick-reviews/README.md";
            var commitMessage = $"Add quick review notes for {Date.ToString("d")}";

            var github          = GitHubClientFactory.Create();
            var masterReference = await github.Git.Reference.Get(owner, repo, branch);

            var latestCommit = await github.Git.Commit.Get(owner, repo, masterReference.Object.Sha);

            var recursiveTreeResponse = await github.Git.Tree.GetRecursive(owner, repo, latestCommit.Tree.Sha);

            var file = recursiveTreeResponse.Tree.SingleOrDefault(t => t.Path == path);

            if (file == null)
            {
                var newTreeItem = new NewTreeItem
                {
                    Mode    = "100644",
                    Path    = path,
                    Content = markdown
                };

                var newTree = new NewTree
                {
                    BaseTree = latestCommit.Tree.Sha
                };
                newTree.Tree.Add(newTreeItem);

                var newTreeResponse = await github.Git.Tree.Create(owner, repo, newTree);

                var newCommit         = new NewCommit(commitMessage, newTreeResponse.Sha, latestCommit.Sha);
                var newCommitResponse = await github.Git.Commit.Create(owner, repo, newCommit);

                var newReference         = new ReferenceUpdate(newCommitResponse.Sha);
                var newReferenceResponse = await github.Git.Reference.Update(owner, repo, branch, newReference);
            }
        }
Beispiel #10
0
        private async Task <NewTreeItem> CreateNewVersionTreeItem(StringBuilder sb, VersionData versionData)
        {
            sb.Append(" Update version");

            var newBlob = new NewBlob
            {
                Encoding = EncodingType.Utf8,
                Content  = versionData.ToString()
            };

            var newBlobRef = await Client.Git.Blob.Create(OwnerSpaceName, RepositoryName, newBlob);

            var newTreeItem = new NewTreeItem
            {
                Path = VersionDataPath,
                Mode = "100644",
                Type = TreeType.Blob,
                Sha  = newBlobRef.Sha
            };

            return(newTreeItem);
        }
Beispiel #11
0
        public async static Task <Reference> CreateFeatureBranch(string owner, string repo, string parentSha, string branchName)
        {
            var github = Helper.GetAuthenticatedClient();

            // Create content blob
            var baselineBlob = new NewBlob
            {
                Content  = "I am overwriting this blob with something new",
                Encoding = EncodingType.Utf8
            };
            var baselineBlobResult = await github.Git.Blob.Create(owner, repo, baselineBlob);

            // Create tree item
            var treeItem = new NewTreeItem
            {
                Type = TreeType.Blob,
                Mode = FileMode.File,
                Path = "README.md",
                Sha  = baselineBlobResult.Sha
            };

            // Create tree
            var newTree = new NewTree();

            newTree.Tree.Add(treeItem);
            var tree = await github.Git.Tree.Create(owner, repo, newTree);

            // Create commit
            var newCommit = new NewCommit("this is the new commit", tree.Sha, parentSha);
            var commit    = await github.Git.Commit.Create(owner, repo, newCommit);

            // Create branch
            var branch = await github.Git.Reference.Create(owner, repo, new NewReference($"refs/heads/{branchName}", commit.Sha));

            // Return commit
            return(branch);
        }
        /// <summary>
        /// Uploads results to GitHub/Azure Table Storage
        /// </summary>
        /// <param name="scorecards">List of Scorecard instances to be uploaded</param>
        /// <param name="githubClient">An authenticated Octokit.GitHubClient instance</param>
        /// <param name="storageAccountKey">Key to the rollout scorecards storage account</param>
        /// <param name="githubConfig">GitHubConfig object representing config</param>
        public async static Task UploadResultsAsync(List <Scorecard> scorecards, GitHubClient githubClient, string storageAccountKey, GithubConfig githubConfig, bool skipPr = false)
        {
            // We batch the scorecards by date so they can be sorted into markdown files properly
            IEnumerable <ScorecardBatch> scorecardBatches = scorecards
                                                            .GroupBy(s => s.Date).Select(g => new ScorecardBatch {
                Date = g.Key, Scorecards = g.ToList()
            });

            const string TargetBranch = "main";

            if (!skipPr)
            {
                Reference targetBranch = await githubClient.Git.Reference
                                         .Get(githubConfig.ScorecardsGithubOrg, githubConfig.ScorecardsGithubRepo, "heads/" + TargetBranch);

                string    newBranchName = $"{DateTime.Today:yyyy-MM-dd}-Scorecard-Update";
                string    newBranchRef  = $"heads/{newBranchName}";
                Reference newBranch;

                // If this succeeds than the branch exists and we should update it directly
                try
                {
                    newBranch = await githubClient.Git.Reference.Get(githubConfig.ScorecardsGithubOrg,
                                                                     githubConfig.ScorecardsGithubRepo, newBranchRef);
                }
                // If not, we've got to create the new branch
                catch (NotFoundException)
                {
                    newBranch = await githubClient.Git.Reference.CreateBranch(githubConfig.ScorecardsGithubOrg,
                                                                              githubConfig.ScorecardsGithubRepo, newBranchName, targetBranch);
                }

                TreeResponse currentTree = await githubClient.Git.Tree.Get(githubConfig.ScorecardsGithubOrg,
                                                                           githubConfig.ScorecardsGithubRepo, newBranchRef);

                NewTree newTree = new NewTree
                {
                    BaseTree = currentTree.Sha,
                };

                // We loop over the batches and generate a markdown file for each rollout date
                foreach (ScorecardBatch scorecardBatch in scorecardBatches)
                {
                    List <RepoMarkdown> repoMarkdowns = scorecardBatch.Scorecards.Select(s => CreateRepoMarkdown(s)).ToList();

                    string scorecardBatchMarkdown = $"# {scorecardBatch.Date.Date:dd MMMM yyyy} Rollout Summaries\n\n" +
                                                    $"{string.Join('\n', repoMarkdowns.Select(md => md.Summary))}\n" +
                                                    $"# Itemized Scorecard\n\n" +
                                                    $"{string.Join('\n', repoMarkdowns.Select(md => md.Breakdown))}";

                    string scorecardBatchFilePath =
                        $"{githubConfig.ScorecardsDirectoryPath}Scorecard_{scorecardBatch.Date.Date:yyyy-MM-dd}.md";

                    NewTreeItem markdownBlob = new NewTreeItem
                    {
                        Path    = scorecardBatchFilePath,
                        Mode    = _gitFileBlobMode,
                        Type    = TreeType.Blob,
                        Content = scorecardBatchMarkdown,
                    };
                    newTree.Tree.Add(markdownBlob);
                }

                TreeResponse treeResponse = await githubClient.Git.Tree.Create(githubConfig.ScorecardsGithubOrg, githubConfig.ScorecardsGithubRepo, newTree);

                // Commit the new response to the new branch
                NewCommit newCommit = new NewCommit("Add scorecards for " +
                                                    string.Join(", ", scorecardBatches.Select(s => s.Date.Date.ToString("yyyy-MM-dd"))),
                                                    treeResponse.Sha,
                                                    newBranch.Object.Sha);
                Commit commit = await githubClient.Git.Commit
                                .Create(githubConfig.ScorecardsGithubOrg, githubConfig.ScorecardsGithubRepo, newCommit);

                ReferenceUpdate update     = new ReferenceUpdate(commit.Sha);
                Reference       updatedRef = await githubClient.Git.Reference.Update(githubConfig.ScorecardsGithubOrg,
                                                                                     githubConfig.ScorecardsGithubRepo, newBranchRef, update);

                PullRequestRequest prRequest = new PullRequestRequest
                {
                    Base  = TargetBranch,
                    Head  = newBranchName,
                    State = ItemStateFilter.Open,
                };
                // If an open PR exists already, we shouldn't try to create a new one
                List <PullRequest> prs =
                    (await githubClient.PullRequest.GetAllForRepository(githubConfig.ScorecardsGithubOrg, githubConfig.ScorecardsGithubRepo)).ToList();
                if (!prs.Any(pr => pr.Head.Ref == newBranchName))
                {
                    NewPullRequest newPullRequest = new NewPullRequest(newCommit.Message, newBranchName, TargetBranch);
                    await githubClient.PullRequest.Create(githubConfig.ScorecardsGithubOrg, githubConfig.ScorecardsGithubRepo, newPullRequest);
                }
            }

            // Upload the results to Azure Table Storage (will overwrite previous entries with new data if necessary)
            CloudTable table = Utilities.GetScorecardsCloudTable(storageAccountKey);

            foreach (Scorecard scorecard in scorecards)
            {
                ScorecardEntity scorecardEntity = new ScorecardEntity(scorecard.Date, scorecard.Repo.Repo)
                {
                    TotalScore           = scorecard.TotalScore,
                    TimeToRolloutSeconds = scorecard.TimeToRollout.TotalSeconds,
                    CriticalIssues       = scorecard.CriticalIssues,
                    Hotfixes             = scorecard.Hotfixes,
                    Rollbacks            = scorecard.Rollbacks,
                    DowntimeSeconds      = scorecard.Downtime.TotalSeconds,
                    Failure             = scorecard.Failure,
                    TimeToRolloutScore  = scorecard.TimeToRolloutScore,
                    CriticalIssuesScore = scorecard.CriticalIssueScore,
                    HotfixScore         = scorecard.HotfixScore,
                    RollbackScore       = scorecard.RollbackScore,
                    DowntimeScore       = scorecard.DowntimeScore,
                };
                await table.ExecuteAsync(TableOperation.InsertOrReplace(scorecardEntity));
            }
        }
Beispiel #13
0
    async Task <string> BuildTargetTree(TargetTree tt)
    {
        var treeFrom = await gateway.TreeFrom(tt.Current, false);

        NewTree newTree;

        if (treeFrom == null)
        {
            newTree = new NewTree();
        }
        else
        {
            var destinationParentTree = treeFrom.Item2;
            newTree = BuildNewTreeFrom(destinationParentTree);
        }

        foreach (var st in tt.SubTreesToUpdate.Values)
        {
            RemoveTreeItemFrom(newTree, st.Current.Name);
            var sha = await BuildTargetTree(st);

            if (sha == TargetTree.EmptyTreeSha)
            {
                // Resulting tree contains no items
                continue;
            }

            var newTreeItem = new NewTreeItem
            {
                Mode = "040000",
                Path = st.Current.Name,
                Sha  = sha,
                Type = TreeType.Tree
            };

            newTree.Tree.Add(newTreeItem);
        }

        foreach (var l in tt.LeavesToDrop.Values)
        {
            RemoveTreeItemFrom(newTree, l.Name);
        }

        foreach (var l in tt.LeavesToCreate.Values)
        {
            var destination = l.Item1;
            var source      = l.Item2;

            RemoveTreeItemFrom(newTree, destination.Name);

            await SyncLeaf(source, destination);

            switch (source.Type)
            {
            case TreeEntryTargetType.Blob:
                var sourceBlobItem = (await gateway.BlobFrom(source, true).ConfigureAwait(false)).Item2;
                newTree.Tree.Add(
                    new NewTreeItem
                {
                    Mode = sourceBlobItem.Mode,
                    Path = destination.Name,
                    Sha  = source.Sha,
                    Type = TreeType.Blob
                });
                break;

            case TreeEntryTargetType.Tree:
                newTree.Tree.Add(
                    new NewTreeItem
                {
                    Mode = "040000",
                    Path = destination.Name,
                    Sha  = source.Sha,
                    Type = TreeType.Tree
                });
                break;

            default:
                throw new NotSupportedException();
            }
        }

        if (newTree.Tree.Count == 0)
        {
            return(TargetTree.EmptyTreeSha);
        }

        return(await gateway.CreateTree(newTree, tt.Current.Owner, tt.Current.Repository));
    }