Skip to content

Commit

Permalink
Merge pull request #16 from thygesteffensen/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
thygesteffensen authored Jan 30, 2021
2 parents 45dbaf1 + 986b6fe commit 849d8fa
Show file tree
Hide file tree
Showing 135 changed files with 2,269 additions and 1,529 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Build

on:
pull_request:
types: [assigned, opened, synchronize, reopened]

jobs:
test:
runs-on: windows-latest
name: Testing
steps:
- name: Checkout code base
uses: actions/checkout@v2

# - name: Add nuget Source
# env:
# GH_TOKEN: ${{ secrets.GH_TOKEN }}
# run: nuget sources Add -Name Github -Source https://nuget.pkg.github.com/thygesteffensen/index.json -UserName thygesteffensen -Password $env:GH_TOKEN
#
# - name: Set Github nuget API
# env:
# GH_TOKEN: ${{ secrets.GH_TOKEN }}
# run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json

- name: Run tests
run: dotnet test --verbosity normal --configuration Release

build:
runs-on: windows-latest
name: Building
steps:
- name: Checkout code base
uses: actions/checkout@v2

- name: Setup MSBuild
uses: microsoft/setup-msbuild@v1

# - name: Add nuget Source
# env:
# GH_TOKEN: ${{ secrets.GH_TOKEN }}
# run: nuget sources Add -Name Github -Source https://nuget.pkg.github.com/thygesteffensen/index.json -UserName thygesteffensen -Password $env:GH_TOKEN
#
# - name: Set Github nuget API
# env:
# GH_TOKEN: ${{ secrets.GH_TOKEN }}
# run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json

- name: Restore NuGet packages
run: dotnet restore PAMU_CDS.sln /p:Configuration=Release

- name: Run tests
run: dotnet test --verbosity normal --configuration Release

- name: Build solution
run: msbuild /p:OutputPath=../build /p:Configuration=Release /p:RestorePackages=false

- name: Archive build to artifacts
uses: actions/upload-artifact@v2
with:
name: build
path: |
build/PAMU_CDS.dll
retention-days: 5
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json

- name: Run tests
run: dotnet test --verbosity normal
run: dotnet test --verbosity normal --configuration Release

release:
name: Releasing
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
GIT_AUTHOR_NAME: thygesteffensen
GIT_AUTHOR_EMAIL: [email protected]
run: |
echo "RELEASE_VERSION=$((npx semantic-release --dry-run).Where({ $_ -like '*Release note*' }) | Out-String | Select-String '[0-9]+\.[0-9]+\.[0-9]+([-][a-zA-z]+[.][0-9]*)' | % { $_.Matches } | % { $_.Value })" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
echo "RELEASE_VERSION=$((npx semantic-release --dry-run).Where({ $_ -like '*Release note*' }) | Out-String | Select-String '[0-9]+\.[0-9]+\.[0-9]+([-][a-zA-z]+[.][0-9]*)?' | % { $_.Matches } | % { $_.Value })" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Print release verison
run: echo ${env:RELEASE_VERSION}
Expand All @@ -75,10 +75,10 @@ jobs:
run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json

- name: Restore NuGet packages
run: nuget restore PAMU_CDS.sln
run: dotnet restore PAMU_CDS.sln /p:Configuration=Release

- name: Package PAMU
run: msbuild /t:pack /p:PackageVersion=${env:RELEASE_VERSION} /p:OutputPath=..\\artifacts
run: msbuild /t:pack /p:PackageVersion=${env:RELEASE_VERSION} /p:Configuration=Release /p:OutputPath=..\\artifacts
if: ${{ env.RELEASE_VERSION }}

- name: Release to GitHub
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,8 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# End of https://www.toptal.com/developers/gitignore/api/rider,csharp
# End of https://www.toptal.com/developers/gitignore/api/rider,csharp
PAMU_CDS.sln.DotSettings
PAMU_CDS/Properties/
build/*.dll
build/*.config
2 changes: 2 additions & 0 deletions PAMU_CDS/Actions/CdsTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace PAMU_CDS.Actions
{
public class CdsTrigger : DefaultBaseActionExecutor
{
public static readonly string[] OperationId = {"SubscribeWebhookTrigger"};

public override Task<ActionResult> Execute()
{
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded});
Expand Down
27 changes: 14 additions & 13 deletions PAMU_CDS/Actions/CreateRecordAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,48 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using PAMU_CDS.Auxiliary;
using Parser;
using Parser.ExpressionParser;
using Parser.FlowParser.ActionExecutors;

namespace PAMU_CDS.Actions
{
public class CreateRecordAction : OpenApiConnectionActionExecutorBase
{
public static readonly string[] OperationId = {"CreateRecord"};

private readonly IOrganizationService _organizationService;
private readonly IState _state;

public CreateRecordAction(
IExpressionEngine expressionEngine,
IOrganizationService organizationService,
IState state) : base(
IExpressionEngine expressionEngine,
OrganizationServiceContext organizationServiceContext) : base(
expressionEngine)
{
_organizationService = organizationService ?? throw new ArgumentNullException(nameof(organizationService));
_state = state ?? throw new ArgumentNullException(nameof(state));
_organizationService = organizationServiceContext?.GetOrganizationService() ??
throw new ArgumentNullException(nameof(organizationServiceContext));
}

public override Task<ActionResult> Execute()
{

var result = new ActionResult();
var entity = new Entity();
entity = entity.CreateEntityFromParameters(Parameters);

try
{
entity.Id = _organizationService.Create(entity);

var retrievedEntity = _organizationService.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true));
_state.AddOutputs(ActionName, retrievedEntity.ToValueContainer());
result.ActionOutput = retrievedEntity.ToValueContainer();
result.ActionStatus = ActionStatus.Succeeded;
}
catch (InvalidPluginExecutionException)
catch (InvalidPluginExecutionException exp)
{
// We need to do some experiments on how the error handling works. Take a look at one of your customers.
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Failed});
return Task.FromResult(new ActionResult
{ActionStatus = ActionStatus.Failed, ActionExecutorException = exp});
}

return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded});
return Task.FromResult(result);
}
}
}
15 changes: 10 additions & 5 deletions PAMU_CDS/Actions/DeleteRecordAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ namespace PAMU_CDS.Actions
{
public class DeleteRecordAction : OpenApiConnectionActionExecutorBase
{
public static readonly string[] OperationId = {"CreateRecord"};

private readonly IOrganizationService _organizationService;

public DeleteRecordAction(IExpressionEngine expressionEngine, IOrganizationService organizationService) : base(
expressionEngine)
public DeleteRecordAction(
IExpressionEngine expressionEngine,
OrganizationServiceContext organizationServiceContext) : base(expressionEngine)
{
_organizationService = organizationService ?? throw new ArgumentNullException(nameof(organizationService));
_organizationService = organizationServiceContext?.GetOrganizationService() ??
throw new ArgumentNullException(nameof(organizationServiceContext));
}

public override Task<ActionResult> Execute()
Expand All @@ -26,10 +30,11 @@ public override Task<ActionResult> Execute()
{
_organizationService.Delete(entity.LogicalName, entity.Id);
}
catch (InvalidPluginExecutionException)
catch (InvalidPluginExecutionException exp)
{
// We need to do some experiments on how the error handling works. Take a look at one of your customers.
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Failed});
return Task.FromResult(new ActionResult
{ActionStatus = ActionStatus.Failed, ActionExecutorException = exp});
}

return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded});
Expand Down
93 changes: 93 additions & 0 deletions PAMU_CDS/Actions/DisAndAssociateEntitiesAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using PAMU_CDS.Auxiliary;
using Parser.ExpressionParser;
using Parser.FlowParser.ActionExecutors;

namespace PAMU_CDS.Actions
{
public class DisAndAssociateEntitiesAction : OpenApiConnectionActionExecutorBase
{
private const string AssociateId = "AssociateEntities";
private const string DisassociateId = "DisassociateEntities";
public static readonly string[] OperationId = {AssociateId, DisassociateId};

private readonly IOrganizationService _organizationService;

public DisAndAssociateEntitiesAction(
IExpressionEngine expressionEngine,
OrganizationServiceContext organizationServiceContext) : base(expressionEngine)
{
_organizationService = organizationServiceContext?.GetOrganizationService() ??
throw new ArgumentNullException(nameof(organizationServiceContext));
}


public override Task<ActionResult> Execute()
{
var entity = new Entity();
entity = entity.CreateEntityFromParameters(Parameters);

OrganizationRequest associateRequest;

switch (Host.OperationId)
{
case AssociateId:
{
var relatedEntity = ExtractEntityReferenceFromOdataId("item/@odata.id");
associateRequest = new AssociateRequest
{
Target = entity.ToEntityReference(),
Relationship = new Relationship(Parameters["associationEntityRelationship"].GetValue<string>()),
RelatedEntities = new EntityReferenceCollection {relatedEntity}
};
break;
}
case DisassociateId:
{
var relatedEntity = ExtractEntityReferenceFromOdataId("$id");

associateRequest = new DisassociateRequest
{
Target = entity.ToEntityReference(),
Relationship = new Relationship(Parameters["associationEntityRelationship"].GetValue<string>()),
RelatedEntities = new EntityReferenceCollection {relatedEntity}
};
break;
}
default:
throw new PowerAutomateException(
$"Action {nameof(DisAndAssociateEntitiesAction)} can only handle {AssociateId} and {DisassociateId} operations, not {Host.OperationId}.");
}

try
{
// TODO: Figure out how this handle bad associations and error handling.
// assignees: thygesteffensen
_organizationService.Execute(associateRequest);
}
catch (InvalidPluginExecutionException exp)
{
// We need to do some experiments on how the error handling works. Take a look at one of your customers.
return Task.FromResult(new ActionResult
{ActionStatus = ActionStatus.Failed, ActionExecutorException = exp});
}

return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded});
}

private EntityReference ExtractEntityReferenceFromOdataId(string itemKey)
{
// https://dglab6.crm4.dynamics.com/api/data/v9.1/contacts(8c711383-b933-eb11-a813-000d3ab11761)

var oDataId = Parameters[itemKey].GetValue<string>();
var entityName =
oDataId.Substring(oDataId.LastIndexOf('/') + 1, oDataId.IndexOf('(') - oDataId.LastIndexOf('/') - 2);
var entityId = oDataId.Substring(oDataId.IndexOf('(') + 1, oDataId.IndexOf(')') - oDataId.IndexOf('(') - 1);

return new EntityReference(entityName, new Guid(entityId));
}
}
}
29 changes: 16 additions & 13 deletions PAMU_CDS/Actions/GetItemAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ namespace PAMU_CDS.Actions
{
public class GetItemAction : OpenApiConnectionActionExecutorBase
{
public static readonly string[] OperationId = {"GetItem"};

private readonly IOrganizationService _organizationService;
private readonly IState _state;
private readonly ILogger<GetItemAction> _logger;

public GetItemAction(
IExpressionEngine expressionEngine,
IOrganizationService organizationService,
IState state,
OrganizationServiceContext organizationServiceContext,
ILogger<GetItemAction> logger) : base(expressionEngine)
{
_organizationService = organizationService ?? throw new ArgumentNullException(nameof(organizationService));
_state = state ?? throw new ArgumentNullException(nameof(state));
_organizationService = organizationServiceContext?.GetOrganizationService() ??
throw new ArgumentNullException(nameof(organizationServiceContext));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public override Task<ActionResult> Execute()
{
var result = new ActionResult();
var entity = new Entity();
entity.CreateEntityFromParameters(Parameters);

Expand All @@ -49,12 +50,13 @@ public override Task<ActionResult> Execute()

var response = (RetrieveResponse) _organizationService.Execute(retrieveRequest);


_state.AddOutputs(ActionName, response.Entity.ToValueContainer());
result.ActionOutput = response.Entity.ToValueContainer();
result.ActionStatus = ActionStatus.Succeeded;
}
catch (InvalidPluginExecutionException)
catch (InvalidPluginExecutionException exp)
{
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Failed});
return Task.FromResult(new ActionResult
{ActionStatus = ActionStatus.Failed, ActionExecutorException = exp});
}
catch (Exception exp) // MockupException
{
Expand All @@ -65,20 +67,22 @@ public override Task<ActionResult> Execute()
$"0x0 | Could not find a property named '{messageDivided[1]}' on type 'Microsoft.Dynamics.CRM.{messageDivided[3]}'",
exp);
}

return Task.FromResult(new ActionResult
{ActionStatus = ActionStatus.Failed, ActionExecutorException = exp});
}

return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded});
return Task.FromResult(result);
}

private RelationshipQueryCollection GetExpandedEntities(string entityName)
{
var paras = Parameters.GetValue<Dictionary<string, ValueContainer>>();
if (!paras.ContainsKey("$expand")) return null;

var t = new RelationshipQueryCollection();

var p = new OdataParser();
// TODO: Refactor with version alpha.18
var expand = p.Get(Parameters["$expand"].GetValue<string>());


Expand Down Expand Up @@ -121,7 +125,6 @@ private RelationshipQueryCollection GetExpandedEntities(string entityName)
private ColumnSet BuildColumnSet()
{
var columnSet = new ColumnSet();
// TODO: Refactor with version alpha.18
var paras = Parameters.GetValue<Dictionary<string, ValueContainer>>();
if (!paras.ContainsKey("$select"))
{
Expand Down
Loading

0 comments on commit 849d8fa

Please sign in to comment.