コード例 #1
0
        /// <summary>
        /// Get all necessary info from GitHub json message
        /// Perform function operations.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="binder"></param>
        /// <param name="log"></param>
        /// <returns></returns>
        private static async Task <bool> ProcessCommitAsync(dynamic data, Binder binder, TraceWriter log)
        {
            var repositoryId = (long)data.repository.id;
            var branch       = (string)data.repository.default_branch;
            var commits      = new List <string>();

            foreach (var commit in data.commits)
            {
                commits.Add(commit.id.ToString());
            }

            commits.Add(data.head_commit.id.ToString());
            var mdFiles = await GetAllMdFilesTask("MarkdownParser", repositoryId, branch, commits, log);

            var jsonFiles = PrepareJsonData(mdFiles, log);

            try
            {
                return(await Utilities.WriteJsonFilesToFileShareTask(jsonFiles,
                                                                     Utilities.GetEnvironmentVariable("AzureWebJobsStorage"),
                                                                     Utilities.GetEnvironmentVariable("OutputFileShareName")));
            }
            catch (Exception e)
            {
                log.Info("There was an exception thrown during writing json files to FileShare: " + e.Message);
                return(false);
            }
        }
コード例 #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(WebHookType = "github")] HttpRequestMessage req,
                                                           Binder binder, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            try
            {
                dynamic data = await req.Content.ReadAsAsync <object>();

                var result = await ProcessCommitAsync(data, binder, log);

                return(result
                    ? req.CreateResponse(HttpStatusCode.OK)
                    : req.CreateErrorResponse(HttpStatusCode.NoContent, "error."));
            }
            catch (ReflectionTypeLoadException ex)
            {
                var sb = new StringBuilder();
                foreach (var exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    var exFileNotFound = exSub as FileNotFoundException;
                    if (!string.IsNullOrEmpty(exFileNotFound?.FusionLog))
                    {
                        sb.AppendLine("Fusion Log:");
                        sb.AppendLine(exFileNotFound.FusionLog);
                    }
                    sb.AppendLine();
                }
                return(req.CreateErrorResponse(HttpStatusCode.NoContent, sb.ToString()));
            }
        }
コード例 #3
0
        public static async Task <IActionResult> QueryByTime(
            [HttpTrigger(AuthorizationLevel.Admin, "get", Route = "queryByTime/{version}/{collectionName}/{fromTime}/{toTime}")] HttpRequest req,
            Binder binder,
            string version,
            string collectionName,
            string fromTime,
            string toTime,
            ILogger log)
        {
            if (!DateTime.TryParse(fromTime, out var from))
            {
                return(new BadRequestObjectResult("Invalid fromTime value"));
            }
            if (!DateTime.TryParse(toTime, out var to))
            {
                return(new BadRequestObjectResult("Invalid toTime value"));
            }
            var querySettings = new QuerySettings(from, to, new QueryParams(req));

            log.LogInformation($"Retrieving from {collectionName}.");

            var storage = await Utils.GetStorageAsync(binder, collectionName);

            if (storage == null)
            {
                return(new NotFoundObjectResult($"Table or blob container '{collectionName}' does not exist"));
            }

            var runner = new QueryRunner(new[] { storage }, querySettings);

            return(new OkObjectResult(querySettings.OnlyCount ?
                                      (object)await runner.GetResultCountAsync() :
                                      await runner.GetMultipleResultsAsync()));
        }
コード例 #4
0
        public static async Task <IActionResult> QueryById(
            [HttpTrigger(AuthorizationLevel.Admin, "get", Route = "queryById/{version}/{collectionNames}/{id}")] HttpRequest req,
            Binder binder,
            string version,
            string collectionNames,
            string id,
            ILogger log)
        {
            var querySettings = new QuerySettings(id, new QueryParams(req));

            log.LogInformation($"Retrieving from {collectionNames}/{id}.");

            var storages = new List <Storage>();

            foreach (var collectionName in collectionNames.Split(','))
            {
                var storage = await Utils.GetStorageAsync(binder, collectionName);

                if (storage == null)
                {
                    return(new NotFoundObjectResult($"Table or blob container '{collectionName}' does not exist"));
                }
                else if (storages.Any(x => x.Table.Name == storage.Table.Name))
                {
                    return(new NotFoundObjectResult($"Duplicate collection '{collectionName}'"));
                }
                storages.Add(storage);
            }

            var runner = new QueryRunner(storages, querySettings);

            return(new OkObjectResult(querySettings.OnlyCount ?
                                      (object)await runner.GetResultCountAsync() :
                                      await runner.GetMultipleResultsAsync()));
        }
コード例 #5
0
        public static async Task <IActionResult> GetByKey(
            [HttpTrigger(AuthorizationLevel.Admin, "get", Route = "getByKey/{version}/{collectionName}/{partitionKey}/{rowKey}")] HttpRequest req,
            Binder binder,
            string version,
            string collectionName,
            string partitionKey,
            string rowKey,
            ILogger log)
        {
            var querySettings = new QuerySettings(partitionKey, rowKey, new QueryParams(req));

            log.LogInformation($"Retrieving from {collectionName}/{partitionKey}/{rowKey}.");

            var storage = await Utils.GetStorageAsync(binder, collectionName);

            if (storage == null)
            {
                return(new NotFoundObjectResult($"Table or blob container '{collectionName}' does not exist"));
            }

            var runner = new QueryRunner(new[] { storage }, querySettings);

            return(new OkObjectResult(await runner.GetSingleResultAsync()));
        }
コード例 #6
0
        public static void Run([BlobTrigger("sto01/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, Microsoft.Azure.WebJobs.Binder binder, ILogger log, ExecutionContext context)
        {
            string              connStrA          = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            BlobServiceClient   blobServiceClient = new BlobServiceClient(connStrA);
            BlobContainerClient containerA        = blobServiceClient.GetBlobContainerClient("sto02");      // Folder

            var zip = new ZipArchive(myBlob);

            foreach (ZipArchiveEntry file in zip.Entries)
            {
                using (var reader = new StreamReader(file.Open()))
                {
                    string jsonA = reader.ReadToEnd();
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Based on input list of tuples creates blob for each.
        /// Name of the blob is first string in tuple, content of blob is second string in tuple
        /// Imperative bindings used here:
        /// https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#imperative-bindings
        /// </summary>
        /// <param name="jsonData">list of tuples with json data, item1-fileName, item2-json content</param>
        /// <param name="binder">Binder used for imperative bindings</param>
        /// <param name="log">traceWriter for logging exceptions</param>
        /// <returns>true if success, false otherwise</returns>
        public static async Task <bool> WriteJsonFilesToBlobsTask(List <Tuple <string, string> > jsonData, Binder binder,
                                                                  TraceWriter log)
        {
            try
            {
                // by default functionapp storage account is used.
                foreach (var json in jsonData)
                {
                    if (string.IsNullOrEmpty(json.Item2)) // when content is empty we should delete blob.
                    {
                        var blob =
                            await binder.BindAsync <CloudBlockBlob>(new BlobAttribute($"json-container/{json.Item1}"));

                        blob.Delete();
                    }
                    else
                    {
                        var blob =
                            await binder.BindAsync <CloudBlockBlob>(new BlobAttribute($"json-container/{json.Item1}"));

                        blob.Properties.ContentType = "application/json; charset=utf-8";
                        blob.UploadText(json.Item2, Encoding.UTF8);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                log.Info("There was an exception thrown during writing json files to blobs: " + e.Message);
                return(false);
            }
        }