Ejemplo n.º 1
0
        private static async Task <IList <(string, Reminder)> > FindNewReminders(
            CallbackModel.CommitModel[] commits, CallbackModel requestObject,
            GitHubClient installationClient)
        {
            var reminderFiles = CommitParser.GetReminders(commits);
            var results       = new List <(string, Reminder)>(reminderFiles.Length); // potentially lower but never higher than number of files

            foreach (string newFile in reminderFiles)
            {
                var reminderParser = GetReminderParser(newFile);
                if (reminderParser == null)
                {
                    // unsupported file type
                    continue;
                }

                var content = await installationClient.Repository.Content.GetAllContentsByRef(
                    requestObject.Repository.Id,
                    newFile,
                    requestObject.Ref);

                var reminder = reminderParser.Parse(content.First().Content);
                results.Add((newFile, reminder));
            }

            return(results);
        }
Ejemplo n.º 2
0
 static Task CreateCommitComment(GitHubClient client, CallbackModel request, string comment)
 {
     return(client.Repository.Comment.Create(
                request.Repository.Id,
                request.HeadCommit.Id,
                new NewCommitComment(comment)));
 }
Ejemplo n.º 3
0
        private static async Task DeleteRemovedReminders(IDocumentClient documentClient, ILogger log,
                                                         CallbackModel requestObject, GitHubClient client)
        {
            var deletedReminders = CommitParser.GetDeletedReminders(requestObject.Commits);

            foreach (var deletedReminder in deletedReminders)
            {
                var reminderParser = GetReminderParser(deletedReminder);
                if (reminderParser == null)
                {
                    // unsupported file type
                    continue;
                }

                try
                {
                    var documentId  = BuildDocumentId(requestObject, deletedReminder);
                    var documentUri = UriFactory.CreateDocumentUri("annoydb", "reminders", documentId);
                    await documentClient.DeleteDocumentAsync(documentUri,
                                                             new RequestOptions()
                    {
                        PartitionKey = new PartitionKey(documentId)
                    });
                    await CreateCommitComment(client, requestObject, $"Deleted reminder '{deletedReminder}'");
                }
                catch (Exception e)
                {
                    log.LogError(e, "Failed to delete reminder");
                    await CreateCommitComment(
                        client,
                        requestObject,
                        $"Failed to delete reminder {deletedReminder}: {string.Join(Environment.NewLine, e.Message, e.StackTrace)}");

                    throw;
                }
            }
        }
Ejemplo n.º 4
0
 static string BuildDocumentId(CallbackModel request, string fileName)
 {
     return($"{request.Installation.Id}-{request.Repository.Id}-{fileName.Split('/').Last()}");
 }
Ejemplo n.º 5
0
        public static CallbackModel ParseJson(string requestBody)
        {
            CallbackModel requestObject = JsonConvert.DeserializeObject <CallbackModel>(requestBody);

            return(requestObject);
        }