JIRA ISSUE INFORMATION COLLECTION BY USING REQUESTS MODULE(PYTHON)
Published: 2024-02-01 10:14:59

Hey Reader,

My name is Seenevasaraj, and I am working as Software Developer at Luxoft India. The various project at Luxoft, I am implementing automation in all viable way. Through this article I desired to provide an explanation for about automation which I have accomplished to generate excel document of JIRA issues by using python script.

**JIRA**
- Issue Tracking: it's miles usually used for issue monitoring competencies. It lets in employer to create, tune, and manage problems, responsibilities, bugs, and other styles of work objects.

- Project Management: JIRA helps venture control by means of permitting groups to devise, tune progress, and launch software program or complete tasks.

**Script utilization**
- Management not required to check all of the tasks manually to track productiveness.
- By this automation, we can make graph and tune progress of all troubles
- Management can make report of worker contribution and project deliveries through this automation.

**The configuration, required, within the Jira software program device:**
- 	Create a Jira person account.
- 	Create a site name, upload a assignment, and, document a few troubles, or, bugs. Our mission is to fetch, troubles records, the use of Python code.
- 	We want to have, a valid token, for authentication, which can be acquired, from link  https://identity.Atlassian.Com/manage/api-tokens.

**Module required in python:**
- requests

**requests module utilization:**
- The **requests** is inbuild Python package for making HTTP requests to URL. Whether or not it's REST APIs or Web Scraping, requests are a should to be discovered for proceeding similarly with these technology. When one makes a request to URI, it returns a response. This package provide functionalities for coping with both the request and reaction

## Establishing connection with JIRA the usage of requests module in python script:

<u>prerequisite</u>
- Get api token from JIRA for authentication cause at the same time as making request. 
  **Procedure to create the API token**
   - Click Profile at the right-hand aspect pinnacle nook, pick Personal Authentication Token option.
   - Click Create token button.  
   - On the API Token Creation Result pop-up, word down the token for destiny use and click on Close.

<u>Making reference to JIRA</u>
- Import requests module 
- Start new consultation via initializing session class in requests module
   **Session class**
     - A Session object is created the use of requests.Session().
     - Using a consultation is in particular beneficial when making multiple requests to the equal server, because it allows you to persist certain parameters (inclusive of cookies) across those requests.   
- Add your API token in session headers
   **Session headers importance**
     - User Agent Information: Many internet servers use the User-Agent header to perceive the purchaser (browser or utility) making the request. Some servers might also respond differently based totally at the consumer agent, and imparting a user agent facilitates mimic a specific browser or customer.
     - Content Negotiation: The Accept header within the request may be used to suggest the preferred format of the reaction (e.G., JSON, XML, HTML). Servers may additionally respond with special content sorts based on the Accept header.
     - Authentication: If the server requires authentication, you may need to include an Authorization header with the appropriate credentials.
     - Cookies: Session headers often consist of cookies, allowing you to persist the session throughout more than one requests. The requests library robotically handles cookies when the usage of classes.
     - Custom Headers: You may want to encompass custom headers primarily based at the requirements of the API or server you're interacting with.

     **session.headers.update({'Authorization': f'Bearer {api_token}'})**

- Once our session is up to date with authentication token, we will make URL requests to acquire data from JIRA
  **response = session.get('https://jira.Devops.Jlr-apps.Com/relaxation/api/2/search',params= {'jql': "task=project_name"},)**

- Check response object to confirm whether we are getting right output or not.
  **print(response.status_code)**
  **Some of crucial HTTP reaction code**
     - 2 hundred OK: Standard response for successful HTTP requests.
     - 400 Bad Request: The server can't or will no longer process the request because of 
     - 401 Unauthorized: Similar to 403 Forbidden, but mainly for authentication.
     - 500 Internal Server Error: A frequent error message indicating that the server has encountered an sudden situation.
     - 503 Service Unavailable: The server is not geared up to handle the request.
     - 429 Too Many Requests: This fame code indicates that the consumer has despatched too many requests in a given amount of time

## Reading records from response
- At every request, consumer can get maximum one thousand issue info from JIRA server. So if assignment have extra than 1000 troubles, person wishes to make request to server with extra parameter.
    
 **params = {
            'jql': "task=project_name",
            'startAt':starting_position, #assign fee according to new starting point after reading a thousand troubles
            'maxResults': -1,}
        
        response = session.get('https://jira.Devops.Jlr-apps.Com/relaxation/api/2/search', params=params)**

- User can access the issue from response with the aid of calling json method
   **data = response.json()**
- User can acquire data of issue via **data['issues']** 
- Iterate issue listing and gather data based JIRA on attribute names
  <u>example</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caa1xeho4mv5awbicrxm.png)



**Conclusion:**
In this Automation I have used requests module, but another third party module called jira is available in pip. But jira package has some limitation, I will publish another article about jira module and it's limitation compared to requests module.