Github Actions — Create an Event Using Microsoft Graph

Anoop
The Startup
Published in
4 min readDec 4, 2019

In this post, we will see a GitHub Action that creates an event in a user’s Outlook calendar using Microsoft Graph (via PnP js).

An example usage might be — when a pull request is submitted in the DEV branch on the GitHub repository, an event is created in the repository owner’s calendar on the next day which contains the details of the pull request. The owner will get a reminder on the next day at the specified time and then the owner can review the pull request and can the required take action.

GitHub Actions to create event

Set up

Firstly if GitHub Actions are new to you, please have a quick look at this short video to understand what GitHub actions are and then have a look at this video by GitHub to understand more details.

Pre-requisites for using the action

Since the GitHub Action explained in this post uses Microsoft Graph, we need to create an app registration in Azure AD so that we can communicate with Office 365. To do that please follow the instructions mentioned in this video by Microsoft. Provide the app “Calendars.ReadWrite” permissions and create a client secret for that app.

In the GitHub repository where this action will be used, create the following 3 secrets by navigating to “Settings > Secrets”:

  • TENANT_NAME — The name of your Office 365 tenant (e.g. contoso.onmicrosoft.com)
  • APP_ID — The Id of the app registration created above
  • APP_SECRET — A secret of the app registration created above.
Secrets in the GitHub repository

More information on secrets can be found here.

Using the action in workflow

In the GitHub repository, click on “Actions”. GitHub will recognize project type and will show us some predefined workflows which can be used. However, we will not use a predefined one and instead start from scratch. So click on “Set up a workflow yourself”.

This will open a YAML file with some code in it. Delete all of and enter the following

When there is a pull request, the above workflow will create an event for user ‘user@contoso.onmicrosoft.com’ on the next day from 12:00 to 13:00 (automatic as no start or end is specified).

If more information about the event is needed then, all of the information attached to an event is available in the github.event variable. We can use github.event object in the required steps — e.g. in the body of the calendar event github.event.compare .

More details on the usage of this action can be found here.

Code related to the action

The entire code of the action can be found here. Please feel free to fork it and make changes accordingly.

The process-related to developing an Action (Node js) is similar to developing any Node js project. Once we are happy the code does what it is should do, we add some files related to make the code into a GitHub action. Instead of me rewriting the whole process, here is an article from GitHub which explains on how to create a simple JavaScript action.

The same concept was used to create the “Microsoft Graph create event” action. Once the base setup was ready, we install the modules that we need to use Microsoft Graph and those are related to the cool PnP js.

npm install @pnp/logging @pnp/common @pnp/odata @pnp/graph @pnp/nodejs --save

To make use of the code related to actions i.e. say getting inputs or providing outputs we install the ‘@actions/core’ library

npm install @actions/core

We can then get the input variables using the code shown below

const subject = core.getInput('subject');

Once we get all the required variables we simply use PnP js to create a graph client and call the required method:

Summary

What we have seen above is a simple action that uses the power of PnP js and Microsoft graph to create an event. With Microsoft Graph there are endless possibilities that we can think of developing. I hope more and more actions get developed in the future.

--

--

Anoop
The Startup

Microsoft MVP. M365 Developer Architect at Content+Cloud.