NAME

JIRA::REST - Thin wrapper around JIRA's REST API

VERSION

version 0.015

SYNOPSIS

    use JIRA::REST;

    my $jira = JIRA::REST->new('https://jira.example.net', 'myuser', 'mypass');

    # File a bug
    my $issue = $jira->POST('/issue', undef, {
        fields => {
            project   => { key => 'PRJ' },
            issuetype => { name => 'Bug' },
            summary   => 'Cannot login',
            description => 'Bla bla bla',
        },
    });

    # Get issue
    $issue = $jira->GET("/issue/TST-101");

    # Iterate on issues
    my $search = $jira->POST('/search', undef, {
        jql        => 'project = "TST" and status = "open"',
        startAt    => 0,
        maxResults => 16,
        fields     => [ qw/summary status assignee/ ],
    });

    foreach my $issue (@{$search->{issues}}) {
        print "Found issue $issue->{key}\n";
    }

    # Iterate using utility methods
    $jira->set_search_iterator({
        jql        => 'project = "TST" and status = "open"',
        maxResults => 16,
        fields     => [ qw/summary status assignee/ ],
    });

    while (my $issue = $jira->next_issue) {
        print "Found issue $issue->{key}\n";
    }

    # Attach files using an utility method
    $jira->attach_files('TST-123', '/path/to/doc.txt', 'image.png');

DESCRIPTION

JIRA is a proprietary bug tracking system from Atlassian.

This module implements a very thin wrapper around JIRA's REST APIs:

  • JIRA Core REST API

    This rich API superseded the old JIRA SOAP API which isn't supported anymore as of JIRA version 7.

    The endpoints of this API have a path prefix of /rest/api/VERSION.

  • JIRA Service Desk REST API

    This API deals with the objects of the JIRA Service Desk application. Its endpoints have a path prefix of /rest/servicedeskapi.

  • JIRA Software REST API

    This API deals with the objects of the JIRA Software application. Its endpoints have a path prefix of /rest/agile/VERSION.

CONSTRUCTOR

new URL, USERNAME, PASSWORD [, REST_CLIENT_CONFIG]

The constructor needs up to four arguments:

  • URL

    A string or a URI object denoting the base URL of the JIRA server. This is a required argument.

    The REST methods described below all accept as a first argument the endpoint's path of the specific API method to call. In general you can pass the complete path, beginning with the prefix denoting the particular API to use (/rest/api/VERSION, /rest/servicedeskapi, or /rest/agile/VERSION). However, to make it easier to invoke JIRA's Core API if you pass a path not starting with /rest/ it will be prefixed with /rest/api/latest or with this URL's path if it has one. This way you can choose a specific version of the JIRA Core API to use instead of the latest one. For example:

        my $jira = JIRA::REST->new('https://jira.example.net/rest/api/1', 'myuser', 'mypass');
  • USERNAME

    The username of a JIRA user.

    It can be undefined if PASSWORD is also undefined. In such a case the user credentials are looked up in the .netrc file or via Config::Identity (which allows gpg encrypted credentials).

    Config::Identity will look for ~/.jira-identity or ~/.jira. You can change the filename stub from jira to a custom stub with the JIRA_REST_IDENTITY environment variable.

  • PASSWORD

    The HTTP password of the user. (This is the password the user uses to log in to JIRA's web interface.)

    It can be undefined, in which case the user credentials are looked up in the .netrc file or via Config::Identity.

  • REST_CLIENT_CONFIG

    A JIRA::REST object uses a REST::Client object to make the REST invocations. This optional argument must be a hash-ref that can be fed to the REST::Client constructor. Note that the URL argument overwrites any value associated with the host key in this hash.

    To use a network proxy please set the 'proxy' argument to the string or URI object describing the fully qualified (including port) URL to your network proxy. This is an extension to the REST::Client configuration and will be removed from the hash before passing it on to the REST::Client constructor.

REST METHODS

JIRA's REST API documentation lists dozens of "resources" which can be operated via the standard HTTP requests: GET, DELETE, PUT, and POST. JIRA::REST objects implement four methods called GET, DELETE, PUT, and POST to make it easier to invoke and get results from JIRA's REST endpoints.

All four methods need two arguments:

  • RESOURCE

    This is the resource's 'path'. For example, in order to GET the list of all fields, you pass /rest/api/latest/field, and in order to get SLA information about an issue you pass /rest/servicedeskapi/request/$key/sla.

    If you're using a method form JIRA Core REST API you may ommit the prefix /rest/api/VERSION. For example, to GET the list of all fields you may pass just /field.

    This argument is required.

  • QUERY

    Some resource methods require or admit parameters which are passed as a query-string appended to the resource's path. You may construct the query string and append it to the RESOURCE argument yourself, but it's easier and safer to pass the arguments in a hash. This way the query string is constructed for you and its values are properly percent-encoded to avoid errors.

    This argument is optional for GET and DELETE. For PUT and POST it must be passed explicitly as undef if not needed.

The PUT and POST methods accept two more arguments:

  • VALUE

    This is the "entity" being PUT or POSTed. It can be any value, but usually is a hash-ref. The value is encoded as a JSON string using the JSON::encode method and sent with a Content-Type of application/json.

    It's usually easy to infer from the JIRA REST API documentation which kind of value you should pass to each resource.

    This argument is required.

  • HEADERS

    This optional argument allows you to specify extra HTTP headers that should be sent with the request. Each header is specified as a key/value pair in a hash.

All four methods return the value returned by the associated resource's method, as specified in the documentation, decoded according to its content type as follows:

  • application/json

    The majority of the API's resources return JSON values. Those are decoded using the decode method of a JSON object. Most of the endpoints return hashes, which are returned as a Perl hash-ref.

  • text/plain

    Those values are returned as simple strings.

Some endpoints don't return anything. In those cases, the methods return undef. The methods croak if they get any other type of values in return.

In case of errors (i.e., if the underlying HTTP method return an error code different from 2xx) the methods croak with a multi-line string like this:

    ERROR: <CODE> - <MESSAGE>
    <CONTENT-TYPE>
    <CONTENT>

So, in order to treat errors you must invoke the methods in an eval block or use any of the exception handling Perl modules, such as Try::Tiny and Try::Catch.

GET RESOURCE [, QUERY]

Returns the RESOURCE as a Perl data structure.

DELETE RESOURCE [, QUERY]

Deletes the RESOURCE.

PUT RESOURCE, QUERY, VALUE [, HEADERS]

Creates RESOURCE based on VALUE.

POST RESOURCE, QUERY, VALUE [, HEADERS]

Updates RESOURCE based on VALUE.

UTILITY METHODS

This module provides a few utility methods.

set_search_iterator PARAMS

Sets up an iterator for the search specified by the hash-ref PARAMS. It must be called before calls to next_issue.

PARAMS must conform with the query parameters allowed for the /rest/api/2/search JIRA REST endpoint.

next_issue

This must be called after a call to set_search_iterator. Each call returns a reference to the next issue from the filter. When there are no more issues it returns undef.

Using the set_search_iterator/next_issue utility methods you can iterate through large sets of issues without worrying about the startAt/total/offset attributes in the response from the /search REST endpoint. These methods implement the "paging" algorithm needed to work with those attributes.

attach_files ISSUE FILE...

The /issue/KEY/attachments REST endpoint, used to attach files to issues, requires a specific content type encoding which is difficult to come up with just the REST::Client interface. This utility method offers an easier interface to attach files to issues.

SEE ALSO

  • REST::Client

    JIRA::REST uses a REST::Client object to perform the low-level interactions.

  • JIRA::Client::REST

    This is another module implementing JIRA's REST API using SPORE. I got a message from the author saying that he doesn't intend to keep it going.

REPOSITORY

https://github.com/gnustavo/JIRA-REST

AUTHOR

Gustavo L. de M. Chaves <gnustavo@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by CPqD <www.cpqd.com.br>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.