floralvikings / jira-connector

NodeJS Wrapper for the Jira REST API
http://floralvikings.github.io/jira-connector/
MIT License
373 stars 180 forks source link

Thanks All!

I no longer have the time, energy or interest to maintain this project, so I'm going to be archiving it.

As an alternative to jira-connector, try: https://github.com/MrRefactoring/jira.js

JavaScript Jira API Wrapper for NodeJS

Node.JS module which provides easy-to-use access to the Jira REST API.

Important Change in Version 1.0.0

Starting with version 1.0.0, all functions and parameters utilize xxxId instead of xxxID. Prior to this update, this naming convention was inconsistent and easily confused.

Please update your code accordingly.

Installation

Install with npm:

    $ npm install jira-connector

Install via Git clone:

    $ git clone https://github.com/floralvikings/jira-connector.git
    $ cd jira-connector
    $ npm install

Documentation

The documentation for jira-connector can be found in the source; If you'd like to view the source as an HTML document, you can use JSDoc to generate these pages. Simply run:

    $ jsdoc -c jsdoc.json

In the jira-connector source directory. This will create a docs directory, containing the HTML markup for the docs.

Also, the official Jira API docs are very useful; many of the functions in jira-connector use the exact same format as the request bodies of these endpoints.

Example

Retrieve an issue from Jira, and print its summary to the console.

// ES5
var JiraClient = require("jira-connector");

// ES6 or Typescript
import JiraClient from "jira-connector";

// Initialize
var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  strictSSL: true // One of optional parameters
});

// ES5
jira.issue.getIssue(
  {
    issueKey: "JWR-19"
  },
  function(error, issue) {
    console.log(issue.fields.summary);
  }
);

// ES6
jira.issue
  .getIssue((issueKey: "JWR-19"))
  .then(issue => {
    console.log(issue.fields.summary);
  })
  .catch(error => throw error);

// ES8 or Typescript. NodeJS 7.6.0 or higher
const issue = await jira.issue.getIssue({ issueKey: "JWR-19" });

First, the JiraApi class is imported from the jira-connector module. This class provides access to the Jira REST endpoints, organized into related classes.

The issue property of the JiraApi class is used to retrieve and modify Jira Issues.

All of the methods in the jira-connector API classes take two arguments; the opts and callback.

Authentication

Depending on the Jira instance to which you are connecting, authentication may or may not be required for various API calls.

jira-connector supports two forms of authentication:

Basic Authentication (Deprecated)

Deprecated. It will require you to provide a username and password each time you connect to the Jira instance. However, jira-connector supports it for users who are unable to use OAuth.

Example:

var JiraClient = require("jira-connector");

var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  basic_auth: {
    username: "SirUserOfName",
    password: "Password123"
  }
});

Basic Authentication With API Token

This is not recommended; it will require you to provide a email and api_token each time you connect to the Jira instance. However, jira-connector supports it for users who are unable to use OAuth.

Example:

var JiraClient = require("jira-connector");

var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  basic_auth: {
    email: "email@email.com",
    api_token: "api-token"
  }
});

Basic Authentication (Base64)

Also not recommended, but slightly better than the above; it will require you to provide a Base64 encoded username and password or email and api_token (a Base64 encoding in the format of "username:password", or "email:api_token") each time you connect to the Jira instance.

More examples here.

Example:

var JiraClient = require("jira-connector");

var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  basic_auth: {
    base64: "U2lyVXNlck9mTmFtZTpQYXNzd29yZDEyMw=="
  }
});

// Base64 encoding of 'SirUserOfName:Password123' (for legacy server version) or 'email:api_token' (jira cloud)

OAuth Authentication

This should be the preferred method of authentication; it is more secure and does not require disclosing your password.

However, setting up OAuth access in Jira can be somewhat complicated; first the Jira administrator must create an Application Link; for instructions on how to do this, see Linking to Another Application.

This example may also be helpful in configuring OAuth Access.

Once the Application Link has been created, you will need the private key that corresponds to the public key used to create the link, and the consumer key that was provided when the link was created.

Once you have this data, you will need to generate an OAuth token and secret for your account; jira-connector provides helper functions for exactly this purpose:

var JiraClient = require("jira-connector");

JiraClient.oauth_util.getAuthorizeURL(
  {
    host: "jenjinstudios.atlassian.net",
    oauth: {
      consumer_key: "your-consumer-key",
      private_key: "-----BEGIN RSA PRIVATE KEY-----\n" + "SomePrivateKeyHere\n" + "-----END RSA PRIVATE KEY-----"
    }
  },
  function(error, oauth) {
    console.log(oauth);
  }
);

This will output something similar to the following:

{
    url: 'https://jenjinstudios.atlassian.net/plugins/servlet/oauth/authorize?oauth_token=some-token-here',
    token: 'some-token-here',
    token_secret: 'some-secret-here'
}

You can then visit the specified URL, which will display a page asking you to allow or deny the request for access. Allowing access will display a verifier code. Once you have this code, you can swap out your current OAuth token for an Access Token with all the permissions of your account; jira-connector provides a function to help with this:

var JiraClient = require("jira-connector");

JiraClient.oauth_util.swapRequestTokenWithAccessToken(
  {
    host: "jenjinstudios.atlassian.net",
    oauth: {
      token: "your-oauth-token",
      token_secret: "your-token-secret",
      oauth_verifier: "verifier-code-from-jira",
      consumer_key: "your-consumer-key",
      private_key: "-----BEGIN RSA PRIVATE KEY-----\n" + "SomePrivateKeyHere\n" + "-----END RSA PRIVATE KEY-----"
    }
  },
  function(error, accessToken) {
    console.log(accessToken);
  }
);

This will query Jira for an Access Token, which will then be printed to the screen. Finally, you're ready to access Jira with OAuth!

var JiraClient = require("jira-connector");

var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  oauth: {
    consumer_key: "your-consumer-key",
    private_key: "-----BEGIN RSA PRIVATE KEY-----\n" + "SomePrivateKey\n" + "-----END RSA PRIVATE KEY-----",
    token: "your-access-token",
    token_secret: "your-token-secret"
  }
});

// Jira is now authenticated with your account!

JWT Authentication

JWT (JSON Web Token) for authenticate apps.

Example:

const JiraClient = require("jira-connector");

const jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  jwt: {
    iss: "atlassian-connect-app",
    secret: "your-jwt-secret"
  }
});

Cookie Jar

You can also use a Cookie Jar for your request. It could be an easier way to prompt for a login only once, without the pain of setting up an OAuth method.

For example, using tough-cookie-filestore:

var JiraClient = require("jira-connector");
var FileCookieStore = require("tough-cookie-filestore");

var request = require("request");
var path = require("path");

var jar = request.jar(new FileCookieStore(path.join(__dirname, "cookies.json")));

// For the first connection
var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  basic_auth: {
    username: "SirUserOfName",
    password: "Password123"
  },
  cookie_jar: jar
});

// For the following connections
var jira = new JiraClient({
  host: "jenjinstudios.atlassian.net",
  cookie_jar: jar
});

In this example, all your cookies are save in a file, cookies.json. Currently, the file MUST exist, it's a limitation from tough-cookie-filestore...

You can now only use the Cookie Jar for all the following request, as long as the file exists and the cookie is still valid!

Supported API Calls

License

MIT license