Delete all work log entries from your issues (Jira Cloud) with Python

 

Hi Atlassian Community!

I'm back with another simple Python script designed to facilitate some of your Jira Admin tasks.
This time, I'm sharing a script that automates the deletion of worklog entries from specified Jira issues AND also clears the remaining estimate time, which is often left behind when worklogs are deleted manually.

The solution

The script uses the Jira REST API (for example this and this) to retrieve all the existing workloads from a user-defined list of issues, delete such work log entries, and reset the remaining estimate time.

Key Features

  • Automated worklog deletion

  • Clear remaining estimate

  • Customizable issue list

Preparation

Python Environment: Install the necessary library by running:

pip install requests

Prepare your Jira Cloud site URL (your-domain.atlassian.net), email address with admin rights, and API token. You can generate an API token from Atlassian's account security page.

Open the script and edit the list of issues that you would like to take action on

The script


import requests
import json
from requests.auth import HTTPBasicAuth
from getpass import getpass

email = input("Enter your Jira email: ")
api_token = getpass("Enter your Jira API token: ")
domain = input("Enter your Jira domain (e.g., mydomain.atlassian.net): ")

auth = HTTPBasicAuth(email, api_token)

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

# List of issue keys to delete worklogs from
issue_keys = [
    "PROJ1-001",
    "PROJ1-002",
    "PROJ1-003"
]

def delete_worklogs(issue_key):
    worklog_url = f"https://{domain}/rest/api/3/issue/{issue_key}/worklog"

    response = requests.get(worklog_url, headers=headers, auth=auth)
    if response.status_code != 200:
        print(f"Failed to retrieve worklogs for {issue_key}. Status code: {response.status_code}")
        return

    worklogs = response.json().get("worklogs", [])
    print(f"Found {len(worklogs)} worklogs for issue {issue_key}.")

    for worklog in worklogs:
        worklog_id = worklog["id"]
        delete_url = f"{worklog_url}/{worklog_id}"
        delete_response = requests.delete(delete_url, headers=headers, auth=auth)
        
        if delete_response.status_code == 204:
            print(f"Deleted worklog {worklog_id} from issue {issue_key}.")
        else:
            print(f"Failed to delete worklog {worklog_id} from issue {issue_key}. Status code: {delete_response.status_code}")

def clear_remaining_estimate(issue_key):
    issue_url = f"https://{domain}/rest/api/3/issue/{issue_key}"

    payload = json.dumps({
        "fields": {
            "timetracking": {
                "remainingEstimate": ""
            }
        }
    })

    response = requests.put(issue_url, headers=headers, data=payload, auth=auth)
    if response.status_code == 204:
        print(f"Cleared remaining estimate for issue {issue_key}.")
    else:
        print(f"Failed to clear remaining estimate for issue {issue_key}. Status code: {response.status_code}")

for issue_key in issue_keys:
    delete_worklogs(issue_key)
    clear_remaining_estimate(issue_key)

print("All worklogs and remaining estimates from all specified issues have been processed.")

Sneak peek

Zight 2024-09-05 at 4.26.14 PM.jpg.jpg

Considerations

  • Project Permissions
    The script requires "Delete All Worklogs" project permission (See)

  • Time Tracking Field
    Ensure the "Time Tracking" field is present on the issue edit screen

  • Customization
    If you prefer to not clear the remaining estimate, modify the script accordingly

Disclaimer:

While this script is designed to facilitate certain interactions with JIRA Software Cloud as a convenience, it is essential to understand that its functionality is subject to change due to updates to JIRA Software Cloud’s API or other conditions that could affect its operation.

Please note that this script is provided on an "as is" and "as available" basis without any warranties of any kind. This script is not officially supported or endorsed by Atlassian, and its use is at your own discretion and risk.

Cheers!~

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events