Last active
September 28, 2021 08:28
-
-
Save BlythMeister/2a5b2de22ba441a470cc800a4bee8184 to your computer and use it in GitHub Desktop.
GitHub Mark Files As Viewed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<NuGetReference Prerelease="true">Octokit.GraphQL</NuGetReference> | |
<Namespace>Octokit.GraphQL</Namespace> | |
<Namespace>Octokit.GraphQL.Model</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
</Query> | |
async Task Main() | |
{ | |
var prNumber = 1; | |
var allFiles = true; | |
var fileNameContains = ""; | |
var filesViewed = true; | |
var org = "BlythMeister"; | |
var repoName = "Gallifrey"; | |
var apiToken = "my_api_key"; | |
await DoStuff(apiToken, org, repoName, prNumber, allFiles, fileNameContains, filesViewed); | |
} | |
async Task DoStuff(string apiToken, string org, string repoName, int prNumber, bool allFiles, string fileNameContains) | |
{ | |
var productInformation = new ProductHeaderValue("FileAsViewed", "0"); | |
var connection = new Connection(productInformation, apiToken); | |
var prQuery = new Query() | |
.RepositoryOwner(org) | |
.Repository(repoName) | |
.PullRequest(prNumber) | |
.Select(x => x.Id) | |
.Compile(); | |
var filesQuery = new Query() | |
.RepositoryOwner(org) | |
.Repository(repoName) | |
.PullRequest(prNumber) | |
.Files(null, null, null, null) | |
.AllPages() | |
.Select(x => x.Path) | |
.Compile(); | |
var pr = await connection.Run(prQuery); | |
var files = await connection.Run(filesQuery); | |
foreach (var file in files.Where(x => allFiles || x.Contains(fileNameContains))) | |
{ | |
string mutation; | |
if(fileViewed) | |
{ | |
Console.WriteLine($"Marking '{file}' as viewed"); | |
mutation = $"{{\"query\":\"mutation {{markFileAsViewed(input: {{pullRequestId:\\\"{pr}\\\",path:\\\"{file}\\\",clientMutationId:\\\"{file}\\\"}}){{ clientMutationId }}}}\""; | |
} | |
else | |
{ | |
Console.WriteLine($"Marking '{file}' as un-viewed"); | |
mutation = $"{{\"query\":\"mutation {{unmarkFileAsViewed(input: {{pullRequestId:\\\"{pr}\\\",path:\\\"{file}\\\",clientMutationId:\\\"{file}\\\"}}){{ clientMutationId }}}}\""; | |
} | |
var res = await connection.Run(mutation); | |
var expectedResponseContains = $"\"clientMutationId\":\"{file}\""; | |
if (!res.Contains(expectedResponseContains)) | |
{ | |
Console.WriteLine($"ERROR ON '{file}'"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment