protected override async Task ExecuteCoreAsync(ReportExecutionContext context)
        {
            context.Log.LogInformation($"Started function execution: {DateTime.Now}");

            var storageConnString   = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            BlobServiceClient   bsc = new BlobServiceClient(storageConnString);
            BlobContainerClient bcc = bsc.GetBlobContainerClient(ContainerName);

            // create the container
            bcc.CreateIfNotExists();

            context.Log.LogInformation("Storage account accessed");

            DateTime to = DateTime.UtcNow;

            foreach (RepositoryConfiguration repositoryConfiguration in context.RepositoryConfigurations)
            {
                // retrieve the last accessed time for this repository
                BlobClient bc          = bcc.GetBlobClient($"{repositoryConfiguration.Owner}_{repositoryConfiguration.Name}");
                DateTime   lastDateRun = DateTime.UtcNow.AddDays(-1);

                try
                {
                    string content = StreamHelpers.GetContentAsString(bc.Download().Value.Content);
                    lastDateRun = DateTime.Parse(content);
                }
                catch
                {
                }

                context.Log.LogInformation("Last processed date for {0} is {1}", repositoryConfiguration, lastDateRun);

                string owner = repositoryConfiguration.Owner;
                string repo  = repositoryConfiguration.Name;

                context.Log.LogInformation("Processing repository {0}\\{1}", owner, repo);

                HtmlPageCreator emailBody = new HtmlPageCreator($"New items in {repo}");

                // get new issues
                RetrieveNewItems(context, CreateQueryForNewItems(repositoryConfiguration, IssueIsQualifier.Issue), lastDateRun, emailBody, "New issues");

                // get new PRs
                RetrieveNewItems(context, CreateQueryForNewItems(repositoryConfiguration, IssueIsQualifier.PullRequest), lastDateRun, emailBody, "New PRs");

                // get needs attention issues
                RetrieveNeedsAttentionIssues(context, repositoryConfiguration, emailBody);

                emailBody.AddContent($"<p>Last checked range: {lastDateRun} -> {to} </p>");

                context.Log.LogInformation("Sending email...");
                // send the email
                EmailSender.SendEmail(context.SendGridToken, context.FromAddress, emailBody.GetContent(), repositoryConfiguration.ToEmail, repositoryConfiguration.CcEmail, $"New issues in the {repo} repo as of {to.ToShortDateString()}", context.Log);

                context.Log.LogInformation("Email sent...");

                bc.Upload(StreamHelpers.GetStreamForString(to.ToUniversalTime().ToString()), overwrite: true);
                context.Log.LogInformation($"Persisted last event time for {repositoryConfiguration.Owner}\\{repositoryConfiguration.Name} as {to}");
            }
        }
示例#2
0
        public override void Execute()
        {
            _log.LogInformation($"Started function execution: {DateTime.Now}");

            var storageConnString   = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            BlobServiceClient   bsc = new BlobServiceClient(storageConnString);
            BlobContainerClient bcc = bsc.GetBlobContainerClient(ContainerName);

            // create the container
            bcc.CreateIfNotExists();

            _log.LogInformation("Storage account accessed");

            DateTime to = DateTime.UtcNow;
            DateTime fromTwoDaysBack = DateTime.UtcNow.AddDays(-2);

            foreach (var repositoryConfig in _cmdLine.RepositoriesList)
            {
                // retrieve the last accessed time for this repository
                BlobClient bc          = bcc.GetBlobClient($"{repositoryConfig.Owner}_{repositoryConfig.Repo}");
                DateTime   lastDateRun = DateTime.UtcNow.AddDays(-1);

                try
                {
                    string content = StreamHelpers.GetContentAsString(bc.Download().Value.Content);
                    lastDateRun = DateTime.Parse(content);
                }
                catch
                {
                }

                _log.LogInformation("Last processed date for {0} is {1}", repositoryConfig, lastDateRun);

                string owner = repositoryConfig.Owner;
                string repo  = repositoryConfig.Repo;

                _log.LogInformation("Processing repository {0}\\{1}", owner, repo);

                HtmlPageCreator emailBody = new HtmlPageCreator($"New items in {repo}");

                SearchIssuesRequest requestOptions = new SearchIssuesRequest()
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    Created = DateRange.Between(fromTwoDaysBack, to),
#pragma warning restore CS0618 // Type or member is obsolete
                    Order = SortDirection.Descending,
                    Repos = new RepositoryCollection()
                };

                requestOptions.Repos.Add(owner, repo);

                // get the issues
                requestOptions.Is = new[] { IssueIsQualifier.Open, IssueIsQualifier.Issue };
                RetrieveItemsFromGitHub(requestOptions, lastDateRun, emailBody, "New issues");

                // get the PRs
                requestOptions.Is = new[] { IssueIsQualifier.Open, IssueIsQualifier.PullRequest };
                RetrieveItemsFromGitHub(requestOptions, lastDateRun, emailBody, "New PRs");

                emailBody.AddContent($"<p>Last checked range: {lastDateRun} -> {to} </p>");

                _log.LogInformation("Sending email...");
                // send the email
                EmailSender.SendEmail(_cmdLine.EmailToken, _cmdLine.FromEmail, emailBody.GetContent(), repositoryConfig.ToEmail, repositoryConfig.CcEmail, $"New issues in the {repo} repo as of {to.ToShortDateString()}", _log);

                _log.LogInformation("Email sent...");

                bc.Upload(StreamHelpers.GetStreamForString(to.ToUniversalTime().ToString()), overwrite: true);
                _log.LogInformation($"Persisted last event time for {repositoryConfig.Owner}\\{repositoryConfig.Repo} as {to}");
            }
        }