Setting Authorization Header in Webhooks

Sure :slight_smile:

I have created a lambda function called forwardPrismicWebhook (to Github Actions).

I have set Prismic up to call the lambda's url with a query parameter for the prismic repository, so that I can use the same lambda for multiple Prismic repos. E:g. forward-prismic-webhook-url?repository=myrepo.

In my lambda I do something like this:

const bodyFromPrismic = JSON.parse(event.body);
const data = {
   event_type: "prismic-publish",
};

const githubActionsUrl = getGithubActionsUrl(
   event.queryStringParameters.repository
);

const config = createConfig(bodyFromPrismic.secret, data);

try {
  const resp = await axios.post(githubActionsUrl, data, config);
  console.log("Response from Github Actions: ", resp);
  return successResponse(
    event,
    "Github Actions webhook called successfully!"
  );
} catch (err) {
  console.error(err);
  return errorResponse(event, "Failed to call Github Actions webhook!");
}

The getGithubActionsUrl method is just a switch-case that returns the url to the corresponding Github Actions for one of my Prismic repositories.

The createConfig creates the headers I needed to call Github Actions with

const createConfig = (authToken: string, data: any) => {
  return {
    headers: {
      "Content-Type": "application/json",
      "Content-Length": data.length,
      Accept: "application/vnd.github.everest-preview+json",
      Authorization: "Bearer " + authToken,
      "User-Agent": "Prismic",
    },
  };
};
3 Likes