private static inputCheck InvalidInputs(inputs theInputs, ILogger log) { inputCheck ic = new inputCheck(); ic.failed = false; if (string.IsNullOrEmpty(theInputs.Pat)) { log.LogInformation("PAT is null"); ic.failed = true; ic.Field = "PAT"; ic.HowFailed = "null or missing"; return(ic); } else if (string.IsNullOrEmpty(theInputs.Org)) { log.LogInformation("Org is null"); ic.failed = true; ic.Field = "Org"; ic.HowFailed = "null or missing"; return(ic); } else if (string.IsNullOrEmpty(theInputs.scope)) { log.LogInformation("Scope is null"); ic.failed = true; ic.Field = "Scope"; ic.HowFailed = "null or missing"; return(ic); } else if (string.IsNullOrEmpty(theInputs.witID)) { log.LogInformation("witID is null"); ic.failed = true; ic.Field = "witID"; ic.HowFailed = "null or missing"; return(ic); } else if (string.IsNullOrEmpty(theInputs.project)) { log.LogInformation("Project is null"); ic.failed = true; ic.Field = "Project"; ic.HowFailed = "null or missing"; return(ic); } else { // nothing was wrong with the input return(ic); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Begin BuildsTriggeredPerUser function"); // read the request body string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); inputs theInputs = JsonConvert.DeserializeObject <inputs>(requestBody); theInputs.Pat = Environment.GetEnvironmentVariable("pat"); theInputs.scope = theInputs.scope.ToLower(); // check that all required parameters are present inputCheck ic = InvalidInputs(theInputs, log); if (ic.failed) { // at least one input was missing, inform user and end log.LogInformation($"The parameter \"{ic.Field}\" is {ic.HowFailed}"); return(new BadRequestObjectResult($"The parameter \"{ic.Field}\" is {ic.HowFailed}")); } else { // all inputs are present, proceed with getting the builds // first get all the projects in the org List <projectDetails> projects = getProjects(theInputs); // quick error check if (projects == null) { log.LogInformation("GetProjects API failed"); return(new BadRequestObjectResult("Unable to retrieve projects - API call unsuccessful")); } // next, get the builds List <buildDetails> builds = getBuilds(projects, theInputs); // count the builds per user // using SortedDictionary here to ensure columns line up correctly for CSV SortedDictionary <string, SortedDictionary <string, int> > usage = getUsage(builds, theInputs.scope); // check if we found any users who performed builds if (usage.Count > 0) { // create CSV report string csv = createCSV(usage); bool success = uploadToOrg(theInputs, csv); if (success) { log.LogInformation("Successfully uploaded the report"); return(new OkObjectResult($"CSV Uploaded Successfully")); } else { // inform that we failed to upload the report to Azure DevOps log.LogInformation("Failed to upload the report"); return(new BadRequestObjectResult("Failed to upload the report")); } } else { // inform that no usage was found log.LogInformation("Didn't find any build usage"); return(new BadRequestObjectResult("No Usage Found")); } } }