From ffd5afb083e58437d33ef46e37274b5b08d691bd Mon Sep 17 00:00:00 2001 From: Jayendran Arumugam Date: Mon, 5 Oct 2020 15:38:08 +0530 Subject: [PATCH 1/4] Added Github Actions --- .github/workflows/developrelease.yml | 33 ++++++++++++++++++++++ .github/workflows/main.yml | 42 ++++++++++++++++++++++++++++ .github/workflows/prbuild.yml | 23 +++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 .github/workflows/developrelease.yml create mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/prbuild.yml diff --git a/.github/workflows/developrelease.yml b/.github/workflows/developrelease.yml new file mode 100644 index 0000000..fca2f8b --- /dev/null +++ b/.github/workflows/developrelease.yml @@ -0,0 +1,33 @@ +name: Build, Test and Release a Beta version + +on: + push: + branches: [ develop ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 3.1.301 + - name: Install dependencies + run: dotnet restore ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj + - name: Build + run: dotnet build ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --configuration Release --no-restore + - name: Test + run: dotnet test ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --no-restore --verbosity normal + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + prerelease: true + diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..5bc812c --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,42 @@ +name: Build, Test & Release to Nuget from master branch + +on: + push: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Fetch all history for all tags and branches + run: git fetch --unshallow || true + - name: Install GitVersion + uses: gittools/actions/gitversion/setup@v0.9.4 + with: + versionSpec: '5.3.x' + - name: Use GitVersion + id: gitversion # step id used as reference for output values + uses: gittools/actions/gitversion/execute@v0.9.4 + - run: | + echo "NuGetVersion: ${{ steps.gitversion.outputs.nuGetVersion }}" + echo "NuGetPreReleaseTagV2: ${{ steps.gitversion.outputs.CommitsSinceVersionSourcePadded }}" + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 3.1.301 + - name: Install dependencies + run: dotnet restore ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj + - name: Build + run: dotnet build ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --configuration Release --no-restore + - name: Test + run: dotnet test ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --no-restore --verbosity normal + - name: Publish NuGet + uses: brandedoutcast/publish-nuget@v2.5.5 + with: + PROJECT_FILE_PATH: ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj + VERSION_STATIC: ${{ steps.gitversion.outputs.nuGetVersion }}-${{ steps.gitversion.outputs.CommitsSinceVersionSourcePadded }} + NUGET_KEY: ${{secrets.NUGET_KEY}} + diff --git a/.github/workflows/prbuild.yml b/.github/workflows/prbuild.yml new file mode 100644 index 0000000..a2fb4c8 --- /dev/null +++ b/.github/workflows/prbuild.yml @@ -0,0 +1,23 @@ +name: Build & Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 3.1.301 + - name: Install dependencies + run: dotnet restore ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj + - name: Build + run: dotnet build ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --configuration Release --no-restore + - name: Test + run: dotnet test ./AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj --no-restore --verbosity normal From ccaeeb2ca4aa13c245ab07405571533db45093e9 Mon Sep 17 00:00:00 2001 From: Jayendran Arumugam Date: Mon, 5 Oct 2020 21:00:01 +0530 Subject: [PATCH 2/4] EventGrid Package Removed with httpclient --- .../AzureFunctions.EventGrid.csproj | 1 - .../EventGridAsyncCollector.cs | 44 +++++++++++-------- .../EventGridAttribute.cs | 6 +++ AzureFunctions.EventGrid/EventGridModel.cs | 27 ++++++++++++ 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 AzureFunctions.EventGrid/EventGridModel.cs diff --git a/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj b/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj index 5781b49..1bb4cac 100644 --- a/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj +++ b/AzureFunctions.EventGrid/AzureFunctions.EventGrid.csproj @@ -16,7 +16,6 @@ You can specify all necessary event details you need. icon-logo.png - diff --git a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs index 046e8e9..619e50c 100644 --- a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs +++ b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs @@ -1,29 +1,33 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; -using Microsoft.Azure.EventGrid; -using Microsoft.Azure.EventGrid.Models; using Microsoft.Azure.WebJobs; +using Newtonsoft.Json; namespace AzureFunctions.EventGrid { public class EventGridAsyncCollector : IAsyncCollector { - private readonly string topicHostname; - + private readonly string topicEndPoint; + private readonly string topicKey; + private readonly IList eventCollection; - private readonly TopicCredentials topicCredentials; + private readonly EventGridAttribute attribute; + + private HttpClient httpClient; + public EventGridAsyncCollector(EventGridAttribute attribute) { this.eventCollection = new List(); - string topicEndpoint = attribute.TopicEndpoint; - string topicKey = attribute.TopicKey; + this.topicEndPoint = attribute.TopicEndpoint; + this.topicKey = attribute.TopicKey; + this.attribute = attribute; - this.topicHostname = new Uri(topicEndpoint).Host; - this.topicCredentials = new TopicCredentials(topicKey); } /// @@ -34,6 +38,11 @@ public EventGridAsyncCollector(EventGridAttribute attribute) /// . public Task AddAsync(Event item, CancellationToken cancellationToken = new CancellationToken()) { + if (httpClient == null) + { + httpClient = attribute.HttpClient ?? new HttpClient(); + } + this.eventCollection.Add(item); return Task.CompletedTask; @@ -48,22 +57,19 @@ public EventGridAsyncCollector(EventGridAttribute attribute) var eventGridEventCollection = CreateEventGridEventCollection(); if (eventGridEventCollection.Any()) { - using (var eventGridClient = new EventGridClient(topicCredentials)) - { - await eventGridClient.PublishEventsAsync( - topicHostname, - eventGridEventCollection, - cancellationToken); - } + HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(eventGridEventCollection), Encoding.UTF8, "application/json"); + httpContent.Headers.Add("aeg-sas-key", topicKey); + await httpClient.PostAsync(topicEndPoint, httpContent, cancellationToken); } } - private IList CreateEventGridEventCollection() + private IList CreateEventGridEventCollection() { - var eventGridEventCollection = new List(); + var eventGridEventCollection = new List(); foreach (var @event in eventCollection) { - var eventGridEvent = new EventGridEvent( + + var eventGridEvent = new EventGridModel( id: Guid.NewGuid().ToString("N"), subject: @event.Subject, dataVersion: @event.DataVersion, diff --git a/AzureFunctions.EventGrid/EventGridAttribute.cs b/AzureFunctions.EventGrid/EventGridAttribute.cs index 0336283..169a244 100644 --- a/AzureFunctions.EventGrid/EventGridAttribute.cs +++ b/AzureFunctions.EventGrid/EventGridAttribute.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using Microsoft.Azure.WebJobs.Description; namespace AzureFunctions.EventGrid @@ -20,5 +21,10 @@ public class EventGridAttribute : Attribute /// [AppSetting] public string TopicKey { get; set; } + + /// + /// If you have already instantiated an , pass it to this property. + /// + public HttpClient HttpClient { get; set; } } } diff --git a/AzureFunctions.EventGrid/EventGridModel.cs b/AzureFunctions.EventGrid/EventGridModel.cs new file mode 100644 index 0000000..dfe2341 --- /dev/null +++ b/AzureFunctions.EventGrid/EventGridModel.cs @@ -0,0 +1,27 @@ +using System; + +namespace AzureFunctions.EventGrid +{ + class EventGridModel :Event + { + // + // Summary: + // Gets or sets an unique identifier for the event. + public string Id { get; set; } + + // + // Summary: + // Gets or sets the time (in UTC) the event was generated. + public DateTime EventTime { get; set; } + + public EventGridModel(string id,string subject, string dataVersion, string eventType, object data,DateTime eventTime) + { + this.Id = id; + this.Subject = subject; + this.DataVersion = dataVersion; + this.EventType = eventType; + this.Data = data; + this.EventTime = eventTime; + } + } +} From a8d0f0fd845656714fa5dea7755b716aa4d0c8b9 Mon Sep 17 00:00:00 2001 From: Jayendran Arumugam Date: Mon, 5 Oct 2020 21:14:29 +0530 Subject: [PATCH 3/4] mentioning correct access modifier --- AzureFunctions.EventGrid/EventGridModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AzureFunctions.EventGrid/EventGridModel.cs b/AzureFunctions.EventGrid/EventGridModel.cs index dfe2341..928ae93 100644 --- a/AzureFunctions.EventGrid/EventGridModel.cs +++ b/AzureFunctions.EventGrid/EventGridModel.cs @@ -2,7 +2,7 @@ namespace AzureFunctions.EventGrid { - class EventGridModel :Event + internal class EventGridModel :Event { // // Summary: From 338cde0c7b7d720c0e12b28897e84651048c7a8a Mon Sep 17 00:00:00 2001 From: jayendran arumugam Date: Tue, 6 Oct 2020 12:02:32 +0530 Subject: [PATCH 4/4] Fixing PR comments --- AzureFunctions.EventGrid/EventGridAsyncCollector.cs | 2 +- README.md | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs index 619e50c..6ad167b 100644 --- a/AzureFunctions.EventGrid/EventGridAsyncCollector.cs +++ b/AzureFunctions.EventGrid/EventGridAsyncCollector.cs @@ -18,7 +18,7 @@ public class EventGridAsyncCollector : IAsyncCollector private readonly IList eventCollection; private readonly EventGridAttribute attribute; - private HttpClient httpClient; + private static HttpClient httpClient; public EventGridAsyncCollector(EventGridAttribute attribute) diff --git a/README.md b/README.md index f798f07..103a0d2 100644 --- a/README.md +++ b/README.md @@ -65,4 +65,15 @@ private class MyCustomEvent ``` The publishing of the events will be executed after the Azure Function is finished, -in the `FlushAsync` method of the `IAsyncCollector`. \ No newline at end of file +in the `FlushAsync` method of the `IAsyncCollector`. + +In order for this to work you need a `local.settings.json` file with the following values. For `EventGridBindingSampleTopicEndpoint` setting, you have to provide the endpoint along with the [api-version](https://docs.microsoft.com/en-us/rest/api/eventgrid/dataplane/publishevents/publishevents) header. + +``` +{ + "Values": { + "EventGridBindingSampleTopicEndpoint": "https://{topicHostname}/api/events?api-version=2018-01-01", + "EventGridBindingSampleTopicKey": "{Topic Key}" + } +} +``` \ No newline at end of file