Skip to content

Commit

Permalink
New deployment type
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelhor committed Feb 27, 2022
1 parent ceb2d7f commit dbb5c6e
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 56 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ inputs:
renumberSteps:
description: 'Renumber the orchestration steps. Possible values: true, or false'
required: false
default: 'false'
addAppInsightsStep:
description: 'Add App Insights orchestration steps to the the user journeys.'
required: false
default: 'false'
verbose:
description: 'Log level verbose.'
required: false
default: 'false'
runs:
using: 'node12'
main: 'dist/index.js'
98 changes: 73 additions & 25 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

115 changes: 85 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Polyfill for graph client
import { Client } from '@microsoft/microsoft-graph-client'
import { convertPatternGroupsToTasks } from 'fast-glob/out/managers/tasks';
import { connected } from 'process';
import { ClientCredentialsAuthProvider } from './auth'; const core = require('@actions/core')
const fs = require('fs')
const path = require('path')
Expand All @@ -11,70 +13,115 @@ const fg = require('fast-glob')
// XML parsing
const { DOMParser } = require('xmldom')

async function run(): Promise<void> {
try {
const folder = core.getInput('folder')
const files = core.getInput('files')
const tenant = core.getInput('tenant')
const clientId = core.getInput('clientId')
const clientSecret = core.getInput('clientSecret')
const addAppInsightsStep = core.getInput('addAppInsightsStep')
const renumberSteps = core.getInput('renumberSteps')
enum DeploymentType {
All,
CommaDelimiter,
JSON
}

class Settings {
public folder: string = ''
files: any
tenant: string = ''
clientId: string = ''
clientSecret: string = ''
addAppInsightsStep: boolean = false
renumberSteps: boolean = false
verbose: boolean = false
}

core.info('Deploy custom policy GitHub Action v5.2 started.')
async function run(): Promise<void> {

if (clientId === 'test') {
const settings: Settings = new Settings()
try {
settings.folder = core.getInput('folder')
settings.files = core.getInput('files')
settings.tenant = core.getInput('tenant')
settings.clientId = core.getInput('clientId')
settings.clientSecret = core.getInput('clientSecret')
settings.addAppInsightsStep = core.getInput('addAppInsightsStep') === true || core.getInput('addAppInsightsStep') === 'true'
settings.renumberSteps = core.getInput('renumberSteps') === true || core.getInput('renumberSteps') === 'true'
settings.verbose = core.getInput('verbose') === true || core.getInput('verbose') === 'true'
let deploymentType: DeploymentType

core.info('Deploy custom policy GitHub Action v5.3 started.')

if (settings.clientId === 'test') {
core.info('GitHub Action test successfully completed.')
return
}

if (clientId === null || clientId === undefined || clientId === '') {
if (settings.clientId === '') {
core.setFailed("The 'clientId' parameter is missing.")
}

if (folder === null || folder === undefined || folder === '') {
if (settings.folder === '') {
core.setFailed("The 'folder' parameter is missing.")
}

if (files === null || files === undefined || files === '') {
if (settings.files === '') {
core.setFailed("The 'files' parameter is missing.")
}

if (tenant === null || tenant === undefined || tenant === '') {
if (settings.tenant === '') {
core.setFailed("The 'tenant' parameter is missing.")
}

if (clientSecret === null || clientSecret === undefined || clientSecret === ''
if (settings.clientSecret === ''
) {
core.setFailed(`The 'clientSecret' parameter is missing.`)
}

// Print the input parameters
if (settings.verbose)
core.info(JSON.stringify(settings))

// Create OAuth2 client
const client = Client.initWithMiddleware({
authProvider: new ClientCredentialsAuthProvider(
tenant,
clientId,
clientSecret
settings.tenant,
settings.clientId,
settings.clientSecret
),
defaultVersion: 'beta'
})

// Create an array of policy files
let filesArray = files.split(",")
let filesArray = settings.files.split(",")

if (settings.files === "*") {
deploymentType = DeploymentType.All
filesArray = await fg([`${settings.folder}/**/*.xml`], { dot: true })
}
else if (settings.files.indexOf(".json") > 0) {
deploymentType = DeploymentType.JSON
if (!fs.existsSync(`.github/workflows/${settings.files}`)) {
core.setFailed(`Can't find the .github/workflows/${settings.files} file`)
}

const deploymentFile = fs.readFileSync(`.github/workflows/${settings.files}`);
const deploymentJson = JSON.parse(deploymentFile);

if (files === "*") {
filesArray = await fg([`${folder}/**/*.xml`], { dot: true })
filesArray = deploymentJson.files
}
else {
deploymentType = DeploymentType.CommaDelimiter
}

core.info(`Deployment type: ${DeploymentType[deploymentType]}.`)

for (const f of filesArray) {

let filePath = ''

if (files === "*") {
if (deploymentType === DeploymentType.All) {
filePath = f.trim()
}
else {
filePath = path.join(folder, f.trim())
else if (deploymentType === DeploymentType.JSON) {
filePath = path.join(settings.folder, f.path.trim())
}
else if (deploymentType === DeploymentType.CommaDelimiter) {
filePath = path.join(settings.folder, f.trim())
}

if (filePath.length > 0 && fs.existsSync(filePath)) {
Expand All @@ -92,20 +139,28 @@ async function run(): Promise<void> {
// Replace yourtenant.onmicrosoft.com with the tenant name parameter
if (policyXML.indexOf("yourtenant.onmicrosoft.com") > 0) {
//core.info(`Policy ${filePath} replacing yourtenant.onmicrosoft.com with ${tenant}.`)
policyXML = policyXML.replace(new RegExp("yourtenant.onmicrosoft.com", "gi"), tenant)
policyXML = policyXML.replace(new RegExp("yourtenant.onmicrosoft.com", "gi"), settings.tenant)
}

// Use the deployment JSON file to find and replace in the custom policy file
if (deploymentType === DeploymentType.JSON && f.replacements !== undefined) {
for (const r of f.replacements) {
policyXML = policyXML.replace(new RegExp(r.find, "gi"), r.replace)
}
}

// Add Azure AppInsights orchestration step at the begging of the collection
if (addAppInsightsStep !== null && addAppInsightsStep !== undefined || addAppInsightsStep === true) {
if (settings.addAppInsightsStep) {
policyXML = addAppInsightsOrchestrationStep(policyXML)
}

// Renumber the orchestration steps
if (renumberSteps !== null && renumberSteps !== undefined || renumberSteps === true) {
if (settings.renumberSteps) {
policyXML = renumberOrchestrationSteps(policyXML)
}

//core.info(policyXML)
if (settings.verbose)
core.info(policyXML)

const fileStream = new Readable()
fileStream.push(policyXML)
Expand Down Expand Up @@ -152,7 +207,7 @@ function addAppInsightsOrchestrationStep(xmlStringDocument: string) {

OrchestrationStep.appendChild(ClaimsExchanges)
ClaimsExchanges.appendChild(ClaimsExchange)

// There is only one OrchestrationSteps element in a UserJourney, add the new element at the first place
ParentOrchestrationSteps[0].insertBefore(OrchestrationStep, ParentOrchestrationSteps[0].firstChild)
}
Expand Down

0 comments on commit dbb5c6e

Please sign in to comment.