-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachments.py
executable file
·128 lines (100 loc) · 4.94 KB
/
attachments.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
import os
import tempfile
import urllib
import shutil
from github import Github
import utility
from arij import JiraProject
class Attachments:
"""
Client in charge to fetch attachments locally from JIRA for a given project
and then to push retrieved files on a Github repository
"""
def __init__(self, jira_url,
github_organization_name,
github_repository_name, working_dir=None):
"""
:param jira_url: JIRA endpoint used to fetch issues
:param github_organization_name: organization name where to create repository for attachments
:param github_repository_name: name of the repository to create for attachments
:param working_dir: local space where to save attachments temporarily
"""
self.jira_url = jira_url
self.github_organization_name = github_organization_name
self.github_repository_name = github_repository_name
if working_dir is None:
self.working_dir = tempfile.TemporaryDirectory(
suffix='-backup-attachments').name
print("Working directory set to '{}'".format(self.working_dir))
else:
self.working_dir = working_dir
if not os.path.exists(working_dir):
os.makedirs(working_dir)
def fetch_from_jira(self, jira_project_key):
jira_project = JiraProject(self.jira_url, jira_project_key)
info = jira_project.get_attachment_information()
for (issue_key, attachment_id) in info:
attachment = jira_project.get_attachment(attachment_id)
issue_id = issue_key[issue_key.index('-') + 1:]
attachment_folder = self.working_dir + '/' + jira_project.project_key.lower() + '/' + issue_id + '/'
if not os.path.exists(attachment_folder):
os.makedirs(attachment_folder)
with urllib.request.urlopen(attachment.content) as response, open(
attachment_folder + '/' + attachment.filename,
'wb') as out_file:
shutil.copyfileobj(response, out_file)
print("Retrieved attachment '{}' for {}".format(
attachment.filename, issue_key))
def push_on_github(self, github_authentication_token):
self._create_git_repository(github_authentication_token)
if utility.execute_command(
'cd {} && git init && git checkout --orphan gh-pages && {}'.format(
self.working_dir,
'git remote add origin [email protected]:{}/{}.git'.format(
self.github_organization_name,
self.github_repository_name))) is 0:
print("Local git repository and branch created")
else:
print("Error while creating local git repository and branch")
if utility.execute_command(
'cd {} && git add -A && git commit -m "Import JIRA attachments for selected projects" && git push origin gh-pages'.format(
self.working_dir)) is 0:
print("JIRA attachments backup pushed on Github")
else:
print("Error while pushing JIRA attachments backup on Github")
def _create_git_repository(self, github_authentication_token):
github = Github(github_authentication_token)
github_organization = github.get_organization(
self.github_organization_name)
github_organization.create_repo(
self.github_repository_name, has_wiki=False, has_issues=False,
has_downloads=False)
def delete_github_repository(self, github_authentication_token):
github = Github(github_authentication_token)
github_organization = github.get_organization(
self.github_organization_name)
try:
github_organization.get_repo(self.github_repository_name).delete()
except:
pass
def delete_working_dir(self):
shutil.rmtree(self.working_dir, ignore_errors=True)
@staticmethod
def get_attachment_url(github_attachments_organization_name,
github_attachments_repository_name,
jira_issue_key, attachment):
(project_key, issue_key_id) = jira_issue_key.split('-')
return 'https://github.com/{}/{}/blob/gh-pages/{}/{}/{}'.format(
github_attachments_organization_name, github_attachments_repository_name,
project_key.lower(), issue_key_id, attachment.filename)
if __name__ == '__main__':
attachments = Attachments('https://jira.activeeon.com',
"ow2-github-migration",
'attachments-jira')
github_token = utility.get_env_var("GITHUB_TOKEN")
attachments.delete_working_dir()
attachments.delete_github_repository(github_token)
attachments.fetch_from_jira('SCHEDULING')
attachments.push_on_github(github_token)
attachments.delete_working_dir()