82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
import requests
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
# -------- CONFIGURATION --------
|
|
JIRA_URL = 'https://jira.datpool.net'
|
|
PAT_TOKEN = 'Mjg4NzY0ODM0ODMyOpb3S+TAsI0qUAwBbeP8k5rjhvLb'
|
|
USERNAME = 'de58076'
|
|
|
|
|
|
|
|
# Optional: JQL filter by user/date/project
|
|
JQL_QUERY = 'worklogAuthor = currentUser() AND worklogDate = "{}"'
|
|
# --------------------------------
|
|
|
|
HEADERS = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {PAT_TOKEN}"
|
|
}
|
|
|
|
def get_issues(date_str):
|
|
url = f"{JIRA_URL}/rest/api/2/search"
|
|
jql = JQL_QUERY.format(date_str)
|
|
params = {
|
|
"jql": jql,
|
|
"fields": "summary", # We don't need worklog field here anymore
|
|
"maxResults": 1000
|
|
}
|
|
|
|
response = requests.get(url, headers=HEADERS, params=params)
|
|
response.raise_for_status()
|
|
return response.json()["issues"]
|
|
|
|
def get_all_worklogs(issue_key):
|
|
url = f"{JIRA_URL}/rest/api/2/issue/{issue_key}/worklog"
|
|
response = requests.get(url, headers=HEADERS)
|
|
response.raise_for_status()
|
|
return response.json().get("worklogs", [])
|
|
|
|
def sum_worklogs(issues, target_date):
|
|
total_seconds = 0
|
|
|
|
for issue in issues:
|
|
issue_key = issue["key"]
|
|
worklogs = get_all_worklogs(issue_key)
|
|
for log in worklogs:
|
|
author = log.get("author", {}).get("name") or log.get("author", {}).get("accountId")
|
|
if author != USERNAME:
|
|
continue
|
|
started = log.get("started")
|
|
if started:
|
|
log_date = datetime.strptime(started, "%Y-%m-%dT%H:%M:%S.%f%z").date()
|
|
if log_date.isoformat() == target_date:
|
|
total_seconds += log.get("timeSpentSeconds", 0)
|
|
|
|
return total_seconds
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python jira_worklog_summary.py YYYY-MM-DD")
|
|
sys.exit(1)
|
|
|
|
target_date = sys.argv[1]
|
|
try:
|
|
datetime.strptime(target_date, "%Y-%m-%d")
|
|
except ValueError:
|
|
print("Invalid date format. Use YYYY-MM-DD.")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
issues = get_issues(target_date)
|
|
total_seconds = sum_worklogs(issues, target_date)
|
|
total_hours = total_seconds // 3600
|
|
total_minutes = (total_seconds % 3600) // 60
|
|
|
|
print(f"Total time logged on {target_date} by {USERNAME}: {total_hours} hours and {total_minutes} minutes")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |