Skip to content

Commit 77dcb66

Browse files
author
Steven Surowiec
committed
Merge pull request #39 from toehio/feature/user_info
Support for webhooks and getting current user info
2 parents 028b24f + 045c181 commit 77dcb66

File tree

1 file changed

+233
-2
lines changed

1 file changed

+233
-2
lines changed

lib/jira.js

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

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

153+
var apiVersion = this.apiVersion;
154+
if (altApiVersion != null) {
155+
apiVersion = altApiVersion;
156+
}
157+
153158
var uri = url.format({
154159
protocol: this.protocol,
155160
hostname: this.host,
156161
auth: this.username + ':' + this.password,
157162
port: this.port,
158-
pathname: basePath + this.apiVersion + pathname
163+
pathname: basePath + apiVersion + pathname
159164
});
160165
return decodeURIComponent(uri);
161166
};
@@ -1423,4 +1428,230 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
14231428
});
14241429
};
14251430

1431+
// ## Register a webhook ##
1432+
// ### Takes ###
1433+
//
1434+
// * webhook: properly formatted webhook
1435+
// * callback: for when it's done
1436+
//
1437+
// ### Returns ###
1438+
// * error string
1439+
// * success object
1440+
//
1441+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1442+
/*
1443+
* Success object in the format:
1444+
* {
1445+
* name: 'my first webhook via rest',
1446+
* events: [],
1447+
* url: 'http://www.example.com/webhooks',
1448+
* filter: '',
1449+
* excludeIssueDetails: false,
1450+
* enabled: true,
1451+
* self: 'http://localhost:8090/rest/webhooks/1.0/webhook/5',
1452+
* lastUpdatedUser: 'user',
1453+
* lastUpdatedDisplayName: 'User Name',
1454+
* lastUpdated: 1383247225784
1455+
* }
1456+
*/
1457+
this.registerWebhook = function(webhook, callback) {
1458+
var options = {
1459+
rejectUnauthorized: this.strictSSL,
1460+
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
1461+
method: 'POST',
1462+
json: true,
1463+
body: webhook
1464+
};
1465+
1466+
this.request(options, function(error, response, body) {
1467+
1468+
if (error) {
1469+
callback(error, null);
1470+
return;
1471+
}
1472+
1473+
if (response.statusCode === 201) {
1474+
callback(null, body);
1475+
return;
1476+
}
1477+
1478+
callback(response.statusCode + ': Error while registering new webhook');
1479+
1480+
});
1481+
};
1482+
1483+
// ## List all registered webhooks ##
1484+
// ### Takes ###
1485+
//
1486+
// * callback: for when it's done
1487+
//
1488+
// ### Returns ###
1489+
// * error string
1490+
// * array of webhook objects
1491+
//
1492+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1493+
/*
1494+
* Webhook object in the format:
1495+
* {
1496+
* name: 'my first webhook via rest',
1497+
* events: [],
1498+
* url: 'http://www.example.com/webhooks',
1499+
* filter: '',
1500+
* excludeIssueDetails: false,
1501+
* enabled: true,
1502+
* self: 'http://localhost:8090/rest/webhooks/1.0/webhook/5',
1503+
* lastUpdatedUser: 'user',
1504+
* lastUpdatedDisplayName: 'User Name',
1505+
* lastUpdated: 1383247225784
1506+
* }
1507+
*/
1508+
this.listWebhooks = function(callback) {
1509+
var options = {
1510+
rejectUnauthorized: this.strictSSL,
1511+
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
1512+
method: 'GET',
1513+
json: true
1514+
};
1515+
1516+
this.request(options, function(error, response, body) {
1517+
1518+
if (error) {
1519+
callback(error, null);
1520+
return;
1521+
}
1522+
1523+
if (response.statusCode === 200) {
1524+
callback(null, body);
1525+
return;
1526+
}
1527+
1528+
callback(response.statusCode + ': Error while listing webhooks');
1529+
1530+
});
1531+
};
1532+
1533+
// ## Get a webhook by its ID ##
1534+
// ### Takes ###
1535+
//
1536+
// * webhookID: id of webhook to get
1537+
// * callback: for when it's done
1538+
//
1539+
// ### Returns ###
1540+
// * error string
1541+
// * webhook object
1542+
//
1543+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1544+
this.getWebhook = function(webhookID, callback) {
1545+
var options = {
1546+
rejectUnauthorized: this.strictSSL,
1547+
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
1548+
method: 'GET',
1549+
json: true
1550+
};
1551+
1552+
this.request(options, function(error, response, body) {
1553+
1554+
if (error) {
1555+
callback(error, null);
1556+
return;
1557+
}
1558+
1559+
if (response.statusCode === 200) {
1560+
callback(null, body);
1561+
return;
1562+
}
1563+
1564+
callback(response.statusCode + ': Error while getting webhook');
1565+
1566+
});
1567+
};
1568+
1569+
// ## Delete a registered webhook ##
1570+
// ### Takes ###
1571+
//
1572+
// * webhookID: id of the webhook to delete
1573+
// * callback: for when it's done
1574+
//
1575+
// ### Returns ###
1576+
// * error string
1577+
// * success string
1578+
//
1579+
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
1580+
this.deleteWebhook = function(webhookID, callback) {
1581+
var options = {
1582+
rejectUnauthorized: this.strictSSL,
1583+
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
1584+
method: 'DELETE',
1585+
json: true
1586+
};
1587+
1588+
this.request(options, function(error, response, body) {
1589+
1590+
if (error) {
1591+
callback(error, null);
1592+
return;
1593+
}
1594+
1595+
if (response.statusCode === 204) {
1596+
callback(null, "Success");
1597+
return;
1598+
}
1599+
1600+
callback(response.statusCode + ': Error while deleting webhook');
1601+
1602+
});
1603+
};
1604+
1605+
1606+
// ## Describe the currently authenticated user ##
1607+
// ### Takes ###
1608+
//
1609+
// * callback: for when it's done
1610+
//
1611+
// ### Returns ###
1612+
// * error string
1613+
// * user object
1614+
//
1615+
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id2e865)
1616+
/*
1617+
* User object in the format:
1618+
* {
1619+
* self: 'http://localhost:8090/rest/api/latest/user?username=user',
1620+
* name: 'user',
1621+
* loginInfo:
1622+
* {
1623+
* failedLoginCount: 2,
1624+
* loginCount: 114,
1625+
* lastFailedLoginTime: '2013-10-29T13:33:26.702+0000',
1626+
* previousLoginTime: '2013-10-31T20:30:51.924+0000'
1627+
* }
1628+
* }
1629+
*/
1630+
1631+
this.getCurrentUser = function(callback) {
1632+
var options = {
1633+
rejectUnauthorized: this.strictSSL,
1634+
uri: this.makeUri('/session', 'rest/auth/', '1'),
1635+
method: 'GET',
1636+
json: true
1637+
};
1638+
1639+
this.request(options, function(error, response, body) {
1640+
1641+
if (error) {
1642+
callback(error, null);
1643+
return;
1644+
}
1645+
1646+
if (response.statusCode === 200) {
1647+
callback(null, body);
1648+
return;
1649+
}
1650+
1651+
callback(response.statusCode + ': Error while getting current user');
1652+
1653+
});
1654+
};
1655+
1656+
14261657
}).call(JiraApi.prototype);

0 commit comments

Comments
 (0)