pycontribs-jira

repository·main·Indexed 24 days ago

https://github.com/pycontribs/jira

A Python library providing an interface for interacting with the Jira REST API, supporting Jira Cloud and Jira Server/Data Center. It includes modules for client interaction, configuration, exception handling, and resilient session management, as well as a jirashell CLI. The library distinguishes between Resource objects for server-owned entities and Properties objects for freeform metadata.

Tokens
14.5K
Snippets
17
Records
106
Agent score
78%

What's inside pycontribs-jira

  1. Understand the difference between Resource objects and properties objects

    main

    The library distinguishes between two types of data returned from the Jira REST API:

    1. Resource objects: These represent REST entities that represent the current state of a server-owned concept (e.g., an Issue, a User, or a Project).

      • They are instances of the Resource class or its subclasses.
      • They always contain a self property (a root-level link to the resource's URL).
      • They can be connected to other resources. The client automatically converts nested resource data into proper Resource subclasses (e.g., issue.fields.assignee will be a User Resource object, not just a dictionary).
      • They are typically obtained using the find() method.
    2. Properties objects: These are freeform collections of values returned by Jira for queries that do not represent a specific entity state.

      • They are modeled as standard Python dict objects.
      • They are used for metadata or configuration-driven data, such as the output of createmeta, which informs you which fields are required to create an issue.
  2. Quickstart with the JIRA class

    main

    To interact with Jira, import the JIRA class, instantiate it with your Jira instance URL, and use the .issue() method to retrieve issue objects. You can access issue data through the .fields attribute.

    from jira import JIRA
    
    jira = JIRA('https://jira.atlassian.com')
    
    issue = jira.issue('JRA-9')
    print(issue.fields.project.key)            # 'JRA'
    print(issue.fields.issuetype.name)         # 'New Feature'
    print(issue.fields.reporter.displayName)   # 'Mike Cannon-Brookes [Atlassian]'
  3. Install and use the jirashell CLI

    main

    The jirashell script is a specialized Python interpreter (built on IPython) designed for active exploration of the Jira REST API. It automatically builds a JIRA client object and stores it in a variable named jira for immediate use.

    Installation

    Install the jira package with the [cli] extra to include the shell:

    pip install jira[cli]

    Running the Shell

    Launch the shell by providing your Jira server URL with the -s flag:

    jirashell -s https://jira.atlassian.com

    Once active, you can interact with your Jira instance using standard Python code. Press Ctrl-D to exit.

    pip install jira[cli]
    
    jirashell -s https://jira.atlassian.com
  4. Explore Jira resources using jirashell

    main

    In jirashell, resources (like issues) are returned as Resource objects. These objects map the server's JSON response directly into Python objects with attribute access. This allows you to discover available properties and methods using tab completion.

    Example: Inspecting an Issue

    1. Fetch an issue using jira.issue('ISSUE_KEY').
    2. Use the . operator and hit TAB to see available methods (e.g., update, delete, expand).
    3. Access fields via the .fields attribute. Because fields are mapped directly, you can access custom fields using their specific names (e.g., issue.fields.customfield_11531).
    # Fetch an issue
    issue = jira.issue('JRA-1330')
    
    # Access standard fields
    print(issue.fields.summary)
    print(issue.fields.status)
    
    # Access custom fields
    print(issue.fields.customfield_11531)
    issue = jira.issue('JRA-1330')
  5. Install the jira-python library

    main

    The recommended way to install jira-python is via pip.

    To install the library along with the dependencies required for the jirashell CLI binary, use the [cli] extra. If you only need the library for programmatic use, you can omit the [cli] suffix.

    pip install 'jira[cli]'
  6. Set up a development environment using VS Code Dev Containers

    main

    The project uses Docker to generate a test Jira Server instance. You can automate the setup using VS Code Dev Containers.

    Prerequisites:

    1. Ensure Docker is running.
    2. Install the following VS Code extensions:
      • ms-azuretools.vscode-docker
      • ms-vscode-remote.remote-containers

    Steps:

    1. Open the repository folder in VS Code.
    2. Open the Command Palette (View >> Command Palette or equivalent).
    3. Search for and select: Remote-containers: Rebuild and Reopen in container.

    Note: The Jira server build takes time. You can monitor progress via the Docker extension. Once the server is up, it will be reachable at http://localhost:2990/jira.

  7. Run the Jira test image

    main

    Run the Jira test image using docker run.

    Important Requirements:

    • You must use the -dit flags (detached, interactive, and TTY) because the AMPS process exits if it does not have a controlling TTY.
    • Port mapping -p 2990:2990 is used to expose the service.
    • Default credentials are admin/admin.
    • Note on startup time: The first responsive request typically takes approximately 3-5 minutes on a cold network.

    Standard Run Command:

    docker run -dit -p 2990:2990 --name jira pycontribs/jira-test-image:8.17.1
  8. Override the Jira version in the test image

    main

    You can override the Jira version for different 8.x patch versions by using the JIRA_VERSION environment variable during docker run.

    Limitations:

    • Only Jira 8.x patch versions are supported.
    • Jira 9+ and 11+ are not supported because the upstream addono pom.xml is anchored to the 8.x dependency graph and will fail Maven resolution for newer major versions.
    docker run -dit -p 2990:2990 -e JIRA_VERSION=8.17.0 --name jira \
      pycontribs/jira-test-image:8.17.1
  9. Authenticate with JIRA

    main

    The JIRA client supports several authentication methods. Note that Cookie-based authentication and HTTP BASIC are no longer supported on Jira Cloud. For Jira Cloud, use API Tokens via basic_auth.

    # Jira Cloud: Use Email and API Token
    auth_jira = JIRA(basic_auth=('email', 'API token'))
    
    # Jira Self-Hosted: Use Personal Access Tokens (PATs)
    auth_jira = JIRA(token_auth='API token')
    
    # OAuth 1.0a
    هاoauth_dict = {
        'access_token': 'foo',
        'access_token_secret': 'bar',
        'consumer_key': 'jira-oauth-consumer',
        'key_cert': key_cert_data
    }
    auth_jira = JIRA(oauth=oauth_dict)
    
    # Kerberos
    auth_jira = JIRA(kerberos=True, kerberos_options={'mutual_authentication': 'DISABLED'})
  10. Initialize the JIRA client

    main

    All interactions with Jira are performed through the jira.client.JIRA object. By default, calling JIRA() attempts to connect to a local Jira instance at http://localhost:2990/jira (the default for the Atlassian Plugin SDK). You can specify a custom server URL during initialization.

    from jira import JIRA
    
    # Connect to default local instance
    jira = JIRA()
    
    # Connect to a specific server
    jira = JIRA('https://jira.atlassian.com')