GitHub Actions — Create an Event with Microsoft Graph using MSAL

In this post, we look at a GitHub Action that creates an event in a user’s Outlook calendar with Microsoft Graph using MSAL for Node.

In a related post published previously, I have explained how we can do the same using PnP JS. In this post we will be modifying that approach to use MSAL for Node instead of PnP JS.

Example scenario

Setup

Pre-requisites for using the action

App registration

Repository secrets

  1. CLIENT_ID : The Id of the app registration created above
  2. CLIENT_SECRET : A secret of the app registration created above.
  3. TENANT_ID : The Id of your Microsoft 365 tenant.

Using the action in workflow

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.

Repo related to the action

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.

Packages

npm install @azure/msal-node

We also install

  1. The package related to Microsoft Graph so that we can use the Graph client.
  2. date-fns package to help with the date related functionality

npm install @microsoft/microsoft-graph-client isomorphic-fetch date-fns

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

There are more such packages provided by the GitHub team which can be found here which will help in creating the code for Actions.

We can instead download the repository and install all the packages by running npm i .

Code

  1. main.ts — Gets the inputs from the workflow and passes them to helper methods in other files.
  2. graph.ts — Has methods to create an event in a person’s calendar with the help of Graph client.
  3. auth.ts — Makes use of MSAL to get an access token which will then be used by methods in graph.ts

In main.ts we get the input variables using the example code shown below:

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

We do the same for all the inputs.

Once we have all the inputs, we perform the authentication with the help of client id, client secret and the tenant id. After that we compose the event object and pass it to the Graph API /users/{username}/calendars/events .

Summary

Microsoft MVP. M365 Developer Architect at Content+Cloud.