-
-
Save jaredpar/1605aad9c6b8869c8eaf8868b6c73c25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Load user secrets | |
| using System.Data; | |
| using System.Threading.Tasks; | |
| using Microsoft.Extensions.Configuration; | |
| using Octokit; | |
| var config = new ConfigurationBuilder() | |
| .AddUserSecrets<Program>() // Looks in the user secrets store | |
| .Build(); | |
| var token = config["GitHub.Token"]; | |
| if (string.IsNullOrEmpty(token)) | |
| { | |
| Console.WriteLine("GitHub token not found in user secrets."); | |
| return; | |
| } | |
| // Create an authenticated Octokit client | |
| var client = new GitHubClient(new ProductHeaderValue("MyApp")) | |
| { | |
| Credentials = new Credentials(token) | |
| }; | |
| // Test the client | |
| var user = await client.User.Current(); | |
| Console.WriteLine($"Authenticated as: {user.Login}"); | |
| await MarkMergedAsDone(client); | |
| static async Task MarkMergedAsDone(GitHubClient client) | |
| { | |
| var notificationsClient = client.Activity.Notifications; | |
| Console.WriteLine("List a repository to process (e.g., owner/repo):"); | |
| var line = Console.ReadLine()!; | |
| string owner, repo; | |
| if (line.Contains("/")) | |
| { | |
| var parts = line.Split(['/'], 2); | |
| owner = parts[0]; | |
| repo = parts[1]; | |
| } | |
| else | |
| { | |
| owner = "dotnet"; | |
| repo = line; | |
| } | |
| var apiOptions = new ApiOptions | |
| { | |
| PageSize = 10, | |
| PageCount = 1, | |
| StartPage = 1 | |
| }; | |
| var request = new NotificationsRequest() | |
| { | |
| Since = DateTimeOffset.Now.AddDays(-30) | |
| }; | |
| do | |
| { | |
| var notifications = await notificationsClient.GetAllForRepository(owner, repo, request, apiOptions); | |
| if (notifications.Count == 0) | |
| { | |
| Console.WriteLine("No more notifications to process."); | |
| break; | |
| } | |
| foreach (var notification in notifications) | |
| { | |
| if (notification.Subject.Type != "PullRequest") | |
| { | |
| continue; | |
| } | |
| var pullNumber = int.Parse(notification.Subject.Url.Split('/').Last()); | |
| var pullRequest = await client.PullRequest.Get(owner, repo, pullNumber); | |
| if (pullRequest.Merged) | |
| { | |
| Console.WriteLine($"Marking PR #{pullNumber} as read: {pullRequest.Title}"); | |
| await MarkAsDone(notification.Id); | |
| } | |
| } | |
| apiOptions.StartPage++; | |
| } while (true); | |
| async Task MarkAsDone(string threadId) | |
| { | |
| var endPoint = new Uri($"notifications/threads/{threadId}", UriKind.Relative); | |
| var result = await client.Connection.Delete<object>( | |
| endPoint, | |
| null, | |
| "application/vnd.github+json"); | |
| } | |
| } | |
| async Task ListAll() | |
| { | |
| var notificationsClient = client.Activity.Notifications; | |
| var apiOptions = new ApiOptions | |
| { | |
| PageSize = 10, | |
| PageCount = 1, | |
| StartPage = 1 | |
| }; | |
| do | |
| { | |
| var notifications = await notificationsClient.GetAllForCurrent(apiOptions); | |
| foreach (var notification in notifications) | |
| { | |
| Console.WriteLine($"Notification: {notification.Subject.Title} - {notification.Reason}"); | |
| } | |
| var line = Console.ReadLine(); | |
| if (!string.IsNullOrEmpty(line)) | |
| { | |
| break; | |
| } | |
| apiOptions.StartPage++; | |
| } while (true); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment