Connecting Jira webhooks to SPARQL UPDATE

Building a real-time mirror of Jira data in a triplestore using Cloud Run

To connect a Jira webhook to a SPARQL update, you can create an Express web application that listens for incoming Jira webhook events, parses the event data, and executes a SPARQL update query to update a RDF triplestore or a Solid POD.

Here’s an example of how to create an Express application that listens for incoming Jira webhook events and executes a SPARQL update query to add a new issue to a RDF triplestore:

First, install the necessary dependencies:

npm install express body-parser sparql-client

Create an app.js file and import the required modules:

const express = require("express");
const bodyParser = require("body-parser");
const { SparqlClient, SPARQL } = require("sparql-client-2");

const app = express();
app.use(bodyParser.json());

Define the endpoint for executing the SPARQL update:

app.post("/jira-webhook", async (req, res) => {
  const event = req.body;
  console.log("Received Jira webhook event:", event);

  // Extract relevant data from the event payload
  const issue = {
    id: event.issue.id,
    key: event.issue.key,
    summary: event.issue.fields.summary,
    description: event.issue.fields.description,
    status: event.issue.fields.status.name,
    created: event.issue.fields.created,
    updated: event.issue.fields.updated,
    issuetype: event.issue.fields.issuetype.name,
    reporter: event.issue.fields.reporter.name,
    assignee: event.issue.fields.assignee.name
  };

  // Execute a SPARQL update to add the new issue to a triplestore
  const query = SPARQL`
    PREFIX jira: <http://example.com/ontology/jira#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

    INSERT DATA {
      jira:${issue.id} a jira:Issue ;
        jira:key "${issue.key}" ;
        jira:summary "${issue.summary}" ;
        jira:description "${issue.description}" ;
        jira:status "${issue.status}" ;
        jira:created "${issue.created}"^^xsd:dateTime ;
        jira:updated "${issue.updated}"^^xsd:dateTime ;
        jira:issuetype "${issue.issuetype}" ;
        jira:reporter "${issue.reporter}" ;
        jira:assignee "${issue.assignee}" .
    }
  `;
  try {
    const result = await client.query(query).execute();
    console.log(`SPARQL update executed successfully: ${result}`);
  } catch (err) {
    console.error(`Error executing SPARQL update: ${err}`);
  }

  res.status(200).send("Webhook event received");
});

Start the Express server:

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Create a webhook in Jira that points to the URL of your Express server followed by /jira-webhook. Make sure to select the appropriate events that should trigger the webhook.

With this code, your Express application will listen for incoming Jira webhook events and extract the relevant data from the event payload. The application will then execute a SPARQL update query to add the new issue data to a RDF triplestore. You can adapt this code to execute different linked data usecases.