Skip to content

Commit 555ffa6

Browse files
author
Toehio
committed
Support for registering, listing and deleting webhooks
1 parent e2c2fec commit 555ffa6

File tree

1 file changed

+181
-2
lines changed

1 file changed

+181
-2
lines changed

lib/jira.js

Lines changed: 181 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,23 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
142142

143143
// This is the same almost every time, refactored to make changing it
144144
// later, easier
145-
this.makeUri = function(pathname, altBase) {
145+
this.makeUri = function(pathname, altBase, altApiVersion) {
146146
var basePath = 'rest/api/';
147147
if (altBase != null) {
148148
basePath = altBase;
149149
}
150150

151+
var apiVersion = this.apiVersion;
152+
if (altApiVersion != null) {
153+
apiVersion = altApiVersion;
154+
}
155+
151156
var uri = url.format({
152157
protocol: this.protocol,
153158
hostname: this.host,
154159
auth: this.username + ':' + this.password,
155160
port: this.port,
156-
pathname: basePath + this.apiVersion + pathname
161+
pathname: basePath + apiVersion + pathname
157162
});
158163
return uri;
159164
};
@@ -1421,4 +1426,178 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
14211426
});
14221427
};
14231428

1429+
// ## Register a webhook ##
1430+
// ### Takes ###
1431+
//
1432+
// * webhook: properly formatted webhook
1433+
// * callback: for when it's done
1434+
//
1435+
// ### Returns ###
1436+
// * error string
1437+
// * success object
1438+
//
1439+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1440+
/*
1441+
* Success object in the format:
1442+
* {
1443+
* name: 'my first webhook via rest',
1444+
* events: [],
1445+
* url: 'http://www.example.com/webhooks',
1446+
* filter: '',
1447+
* excludeIssueDetails: false,
1448+
* enabled: true,
1449+
* self: 'http://localhost:8090/rest/webhooks/1.0/webhook/5',
1450+
* lastUpdatedUser: 'user',
1451+
* lastUpdatedDisplayName: 'User Name',
1452+
* lastUpdated: 1383247225784
1453+
* }
1454+
*/
1455+
this.registerWebhook = function(webhook, callback) {
1456+
var options = {
1457+
rejectUnauthorized: this.strictSSL,
1458+
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
1459+
method: 'POST',
1460+
json: true,
1461+
body: webhook
1462+
};
1463+
1464+
this.request(options, function(error, response, body) {
1465+
1466+
if (error) {
1467+
callback(error, null);
1468+
return;
1469+
}
1470+
1471+
if (response.statusCode === 201) {
1472+
callback(null, body);
1473+
return;
1474+
}
1475+
1476+
callback(response.statusCode + ': Error while registering new webhook');
1477+
1478+
});
1479+
};
1480+
1481+
// ## List all registered webhooks ##
1482+
// ### Takes ###
1483+
//
1484+
// * callback: for when it's done
1485+
//
1486+
// ### Returns ###
1487+
// * error string
1488+
// * array of webhook objects
1489+
//
1490+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1491+
/*
1492+
* Webhook object in the format:
1493+
* {
1494+
* name: 'my first webhook via rest',
1495+
* events: [],
1496+
* url: 'http://www.example.com/webhooks',
1497+
* filter: '',
1498+
* excludeIssueDetails: false,
1499+
* enabled: true,
1500+
* self: 'http://localhost:8090/rest/webhooks/1.0/webhook/5',
1501+
* lastUpdatedUser: 'user',
1502+
* lastUpdatedDisplayName: 'User Name',
1503+
* lastUpdated: 1383247225784
1504+
* }
1505+
*/
1506+
this.listWebhooks = function(callback) {
1507+
var options = {
1508+
rejectUnauthorized: this.strictSSL,
1509+
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
1510+
method: 'GET',
1511+
json: true
1512+
};
1513+
1514+
this.request(options, function(error, response, body) {
1515+
1516+
if (error) {
1517+
callback(error, null);
1518+
return;
1519+
}
1520+
1521+
if (response.statusCode === 200) {
1522+
callback(null, body);
1523+
return;
1524+
}
1525+
1526+
callback(response.statusCode + ': Error while listing webhooks');
1527+
1528+
});
1529+
};
1530+
1531+
// ## Get a webhook by its ID ##
1532+
// ### Takes ###
1533+
//
1534+
// * webhookID: id of webhook to get
1535+
// * callback: for when it's done
1536+
//
1537+
// ### Returns ###
1538+
// * error string
1539+
// * webhook object
1540+
//
1541+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1542+
this.getWebhook = function(webhookID, callback) {
1543+
var options = {
1544+
rejectUnauthorized: this.strictSSL,
1545+
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
1546+
method: 'GET',
1547+
json: true
1548+
};
1549+
1550+
this.request(options, function(error, response, body) {
1551+
1552+
if (error) {
1553+
callback(error, null);
1554+
return;
1555+
}
1556+
1557+
if (response.statusCode === 200) {
1558+
callback(null, body);
1559+
return;
1560+
}
1561+
1562+
callback(response.statusCode + ': Error while getting webhook');
1563+
1564+
});
1565+
};
1566+
1567+
// ## Delete a registered webhook ##
1568+
// ### Takes ###
1569+
//
1570+
// * webhookID: id of the webhook to delete
1571+
// * callback: for when it's done
1572+
//
1573+
// ### Returns ###
1574+
// * error string
1575+
// * success string
1576+
//
1577+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1578+
this.deleteWebhook = function(webhookID, callback) {
1579+
var options = {
1580+
rejectUnauthorized: this.strictSSL,
1581+
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
1582+
method: 'DELETE',
1583+
json: true
1584+
};
1585+
1586+
this.request(options, function(error, response, body) {
1587+
1588+
if (error) {
1589+
callback(error, null);
1590+
return;
1591+
}
1592+
1593+
if (response.statusCode === 204) {
1594+
callback(null, "Success");
1595+
return;
1596+
}
1597+
1598+
callback(response.statusCode + ': Error while deleting webhook');
1599+
1600+
});
1601+
};
1602+
14241603
}).call(JiraApi.prototype);

0 commit comments

Comments
 (0)