Framework/Helpers/KustoQueries.ps1

Set-StrictMode -Version Latest 
class KustoQueries
{
    static [string] $VariableGroupQuery = 'set notruncation;\nBuildDefinition\n| where OrganizationName =~ ''{0}''\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==''{1}''\n| extend vargrp = parse_json(Data).variableGroups\n| mv-apply vargrp on (project vargrp.id)\n| where isnotnull( vargrp_id)\n| summarize arg_max(EtlProcessDate, *) by tostring(vargrp_id),BuildDefinitionId\n| project varid = vargrp_id, PipelineId = tostring(BuildDefinitionId), EtlProcessDate=CreatedDate, PipelineType = \"Build\", PipelineName = tostring(BuildDefinitionName)\n| union(\nRelease\n| where OrganizationName =~ ''{0}''\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==''{1}''\n| extend varsdetails = todynamic(Data).variableGroups\n| mv-apply varsdetails on (project varsdetails.id)\n| summarize arg_max(EtlProcessDate, *) by tostring(varsdetails_id)\n| project varid = varsdetails_id, PipelineId = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType = \"Release\", PipelineName = tostring(ReleaseDefinitionName)\n| union (\nReleaseEnvironment\n| where OrganizationName =~ ''{0}''\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==''{1}''\n| extend varsdetails = todynamic(Data).variableGroups\n| mv-apply varsdetails on (project varsdetails.id)\n| summarize arg_max(EtlProcessDate, *) by tostring(varsdetails_id)\n| project varid = varsdetails_id, PipelineId = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType = \"Release\", PipelineName = tostring(ReleaseDefinitionName)\n)\n)'

    static [string] $SecureFileBuildQuery = 'BuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| extend securefile = parse_json(ProcessPhaseStepInput).secureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(BuildDefinitionId), EtlProcessDate, PipelineType=\"Build\" \n| union (\nBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| search \"provProfileSecureFile\"\n| extend securefile = parse_json(ProcessPhaseStepInput).provProfileSecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile), tostring(BuildDefinitionId)\n| project securefile, PipelineDefinition = tostring(BuildDefinitionId), EtlProcessDate, PipelineType=\"Build\"\n| union(\nBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| search \"certSecureFile\"\n| extend securefile = parse_json(ProcessPhaseStepInput).certSecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(BuildDefinitionId), EtlProcessDate, PipelineType=\"Build\"\n) \n)\n|union(\nBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| search \"keystoreFile\"\n| extend securefile = parse_json(ProcessPhaseStepInput).keystoreFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(BuildDefinitionId), EtlProcessDate, PipelineType=\"Build\"\n)\n|union(\nBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| search \"sshKeySecureFile\"\n| extend securefile = parse_json(ProcessPhaseStepInput).sshKeySecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(BuildDefinitionId), EtlProcessDate, PipelineType=\"Build\"\n)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)'

    static [string] $SecureFileReleaseQuery = 'ReleaseEnvironment\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| project EtlProcessDate,Data,ReleaseDefinitionId,ReleaseEnvironmentId\n| extend securefile= todynamic(Data)\n| mv-apply securefile on (project securefile.deployPhasesSnapshot)\n| mv-apply securefile_deployPhasesSnapshot on (project securefile_deployPhasesSnapshot.workflowTasks)\n| where isnotnull(securefile_deployPhasesSnapshot_workflowTasks) and isnotempty(securefile_deployPhasesSnapshot_workflowTasks)\n| mv-apply securefile_deployPhasesSnapshot_workflowTasks on (project securefile_deployPhasesSnapshot_workflowTasks.inputs)\n| extend securefilejson = dynamic_to_json(todynamic(securefile_deployPhasesSnapshot_workflowTasks_inputs))\n| extend securefile = parse_json(securefilejson).secureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType=\"Release\"\n|union (\nReleaseEnvironment\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| project EtlProcessDate,Data,ReleaseDefinitionId,ReleaseEnvironmentId\n| extend securefile= todynamic(Data)\n| mv-apply securefile on (project securefile.deployPhasesSnapshot)\n| mv-apply securefile_deployPhasesSnapshot on (project securefile_deployPhasesSnapshot.workflowTasks)\n| where isnotnull(securefile_deployPhasesSnapshot_workflowTasks) and isnotempty(securefile_deployPhasesSnapshot_workflowTasks)\n| mv-apply securefile_deployPhasesSnapshot_workflowTasks on (project securefile_deployPhasesSnapshot_workflowTasks.inputs)\n| extend securefilejson = dynamic_to_json(todynamic(securefile_deployPhasesSnapshot_workflowTasks_inputs))\n| extend securefile = parse_json(securefilejson).sshKeySecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType=\"Release\"\n)\n|union (\nReleaseEnvironment\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| project EtlProcessDate,Data,ReleaseDefinitionId,ReleaseEnvironmentId\n| extend securefile= todynamic(Data)\n| mv-apply securefile on (project securefile.deployPhasesSnapshot)\n| mv-apply securefile_deployPhasesSnapshot on (project securefile_deployPhasesSnapshot.workflowTasks)\n| where isnotnull(securefile_deployPhasesSnapshot_workflowTasks) and isnotempty(securefile_deployPhasesSnapshot_workflowTasks)\n| mv-apply securefile_deployPhasesSnapshot_workflowTasks on (project securefile_deployPhasesSnapshot_workflowTasks.inputs)\n| extend securefilejson = dynamic_to_json(todynamic(securefile_deployPhasesSnapshot_workflowTasks_inputs))\n| extend securefile = parse_json(securefilejson).keystoreFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType=\"Release\"\n)\n|union (\nReleaseEnvironment\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| project EtlProcessDate,Data,ReleaseDefinitionId,ReleaseEnvironmentId\n| extend securefile= todynamic(Data)\n| mv-apply securefile on (project securefile.deployPhasesSnapshot)\n| mv-apply securefile_deployPhasesSnapshot on (project securefile_deployPhasesSnapshot.workflowTasks)\n| where isnotnull(securefile_deployPhasesSnapshot_workflowTasks) and isnotempty(securefile_deployPhasesSnapshot_workflowTasks)\n| mv-apply securefile_deployPhasesSnapshot_workflowTasks on (project securefile_deployPhasesSnapshot_workflowTasks.inputs)\n| extend securefilejson = dynamic_to_json(todynamic(securefile_deployPhasesSnapshot_workflowTasks_inputs))\n| extend securefile = parse_json(securefilejson).certSecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType=\"Release\"\n)\n|union (\nReleaseEnvironment\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where ProjectId ==\"{1}\"\n| project EtlProcessDate,Data,ReleaseDefinitionId,ReleaseEnvironmentId\n| extend securefile= todynamic(Data)\n| mv-apply securefile on (project securefile.deployPhasesSnapshot)\n| mv-apply securefile_deployPhasesSnapshot on (project securefile_deployPhasesSnapshot.workflowTasks)\n| where isnotnull(securefile_deployPhasesSnapshot_workflowTasks) and isnotempty(securefile_deployPhasesSnapshot_workflowTasks)\n| mv-apply securefile_deployPhasesSnapshot_workflowTasks on (project securefile_deployPhasesSnapshot_workflowTasks.inputs)\n| extend securefilejson = dynamic_to_json(todynamic(securefile_deployPhasesSnapshot_workflowTasks_inputs))\n| extend securefile = parse_json(securefilejson).provProfileSecureFile\n| where isnotnull( securefile)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)\n| project securefile, PipelineDefinition = tostring(ReleaseDefinitionId), EtlProcessDate, PipelineType=\"Release\"\n)\n| summarize arg_max(EtlProcessDate, *) by tostring(securefile)'

    static [string] $SecureFileYAMLQuery = 'YamlBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where StepRefName =~ \"DownloadSecureFile@1\"\n| extend securefile = parse_json(ProcessPhaseStepInput).secureFile\n| summarize arg_max(EtlProcessDate,*) by tostring(securefile)\n | union(YamlBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where StepRefName =~ \"InstallAppleProvisioningProfile@1\"\n| extend securefile = parse_json(ProcessPhaseStepInput).provProfileSecureFile\n| summarize arg_max(EtlProcessDate,*) by tostring(securefile))\n| union (\nYamlBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where StepRefName =~ \"InstallAppleCertificate@2\"\n| extend securefile = parse_json(ProcessPhaseStepInput).certSecureFile\n| summarize arg_max(EtlProcessDate,*) by tostring(securefile)\n)\n|union (\nYamlBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where StepRefName =~ \"InstallSSHKey@0\"\n| extend securefile = parse_json(ProcessPhaseStepInput).sshKeySecureFile\n| summarize arg_max(EtlProcessDate,*) by tostring(securefile)\n)\n| union (\nYamlBuildDefinitionPhaseStep\n| where OrganizationName =~ \"{0}\"\n| where EtlProcessDate > ago({2}d)\n| where StepRefName =~ \"AndroidSigning@3\"\n| extend securefile = parse_json(ProcessPhaseStepInput).apksignerKeystoreFile\n| summarize arg_max(EtlProcessDate,*) by tostring(securefile)\n)\n | project securefile, EtlProcessDate, PipelineDefinition = tostring(BuildDefinitionId)'

}