Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

main

[Jira/Python] Jira 프로젝트 전체 첨부파일 다운로드 스크립트 본문

ETC

[Jira/Python] Jira 프로젝트 전체 첨부파일 다운로드 스크립트

1984 2024. 3. 5. 10:12

Jira 프로젝트의 전체 첨부파일을 이슈별로 다운로드하는 스크립트입니다.

import requests
from requests.auth import HTTPBasicAuth
import json
import os
import re

url = "JIRA_URL"

auth = HTTPBasicAuth("USERNAME", "AUTH_TOKEN")



headers = {
  "Accept": "application/json"
}

dir_path = "D:\\nas\\"

def download_file(url, save_path):
    response = requests.request(
    "GET",
    url,
    headers=headers,
    auth=auth)
    with open(save_path, 'wb') as file:
        file.write(response.content)
    print(f"File downloaded and saved at {save_path}")

def get_issue_key(url):
    response = requests.request(
    "GET",
    url,
    headers=headers,
    auth=auth)
    data = json.loads(response.text)
    return data["key"]

def get_issue_keySummary(url):
    response = requests.request(
        "GET",
        url,
        headers=headers,
        auth=auth
    )
    data = json.loads(response.text)
    issue_keySummary = "[" + data["key"] + "] " + data["fields"]["summary"]

    return clean_folder_name(issue_keySummary)

def clean_folder_name(folder_name):
    # 윈도우에서 사용할 수 없는 문자 패턴 정의
    invalid_chars = r'[<>:"/\\|?*]'
    # 유효하지 않은 문자를 빈 문자열로 치환
    clean_name = re.sub(invalid_chars, '', folder_name)
    # 문자열 앞뒤의 공백 제거
    clean_name = clean_name.strip()
    return clean_name

def create_directory(directory_path):
    try:
        os.makedirs(directory_path)
        print(f"Directory created: {directory_path}")
    except FileExistsError:
        print(f"Directory already exists: {directory_path}")
    except Exception as e:
        print(f"Error occurred while creating directory: {e}")


def search_issue_withAttachment():
    params = {
        "jql": "project = COM AND attachment is not EMPTY",
        "startAt": 0, # 시작 위치 설정
        "maxResults": 50
    }
    response = requests.request(
        "GET",
        url,
        params=params,
        headers=headers,
        auth=auth,
    )

    data = json.loads(response.text)
    count = 0

    for issue in data["issues"]:
        count += 1
        print("No. " + str(count))
        attachments = issue["fields"]["attachment"]
        if(len(attachments) == 0):
            continue
        attachment_save_path = dir_path + get_issue_keySummary(issue["self"])
        create_directory(attachment_save_path) # 이슈 키 + 요약(제목)으로 폴더명 생성
        for attachment in attachments:
            attachment_download_url = attachment["content"]
            download_file(attachment_download_url, attachment_save_path + "\\" + attachment["filename"])
            
search_issue_withAttachment()
728x90
Comments