Skip to content

Instantly share code, notes, and snippets.

@mvisonneau
Created August 11, 2017 15:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mvisonneau/4a76401c2f980943cb08c0c8bd1132e1 to your computer and use it in GitHub Desktop.
Save mvisonneau/4a76401c2f980943cb08c0c8bd1132e1 to your computer and use it in GitHub Desktop.
GitLab to Jira issue sync
#!/usr/bin/env python
import gitlab
from jira import JIRA
import urllib3
## Disable urllib3 ssl checks warnings
urllib3.disable_warnings( urllib3.exceptions.InsecureRequestWarning )
## GitLab config
GITLAB_PROJECT = ''
GITLAB_TOKEN = ''
GITLAB_URL = 'https://gitlab.example.com'
## JIRA config
JIRA_URL = 'https://jira.example.com/'
JIRA_PROJECT = ''
JIRA_USER = ''
JIRA_PASSWORD = ''
JIRA_LABELS = []
## Connect onto GitLab
gitlab = gitlab.Gitlab( GITLAB_URL, GITLAB_TOKEN, api_version=4 )
## Connect onto JIRA
jira = JIRA( JIRA_URL, basic_auth=( JIRA_USER, JIRA_PASSWORD ), options={ 'verify': False } )
## Get GitLab project
gl_project = gitlab.projects.get( GITLAB_PROJECT )
## Lookup available transitions for an issue
#print jira.transitions( jira.issue( 'PROJECT-ISSUEID' ) )
## List project issues
for issue in gl_project.issues.list( all=True ):
found_issues = jira.search_issues( 'project='+JIRA_PROJECT+' AND summary ~ \''+ issue.title.replace( "'", "\\'" ) +'\'')
if len( found_issues ) == 0:
print '-> Creating issue : %s' % issue.title
issue_dict = {
'project': JIRA_PROJECT,
'summary': issue.title,
'description': "Created by " + issue.author.name + " at " + issue.created_at + " :\n" + str(issue.description),
'issuetype': { 'name': 'Task' }
}
if len(issue.assignees) > 0:
if 'username' in issue.assignees[0]:
issue_dict['assignee'] = { 'name': issue.assignees[0]['username'] }
jira_issue = jira.issue( jira.create_issue( fields = issue_dict ) )
jira_issue.update( fields = { "labels": JIRA_LABELS } )
print '--> Created issue in Jira : %s' % str(jira_issue)
if issue.state != 'opened':
print '--> Closing issue in Jira'
jira.transition_issue( jira_issue, '31' )
for note in issue.notes.list( all=True ):
if not note.system:
comment = jira.add_comment( jira_issue, "Comment from " + note.author.name + " at " + note.created_at + " :\n" + note.body )
print '--> Added comment from %s on issue' % note.author.name
else:
print '--> Found JIRA issue ' + str(found_issues[0]) + ' for GL issue : ' + issue.title
@prangenberg
Copy link

Hi @mvisonneau

thanks a lot for this script.
Unfortunately I get an exception:
$ python gitlab_to_jira.py -> Creating issue : Stop merge auch bei verschiedenen Postleitzahlen zulassen Traceback (most recent call last): File "gitlab_to_jira.py", line 40, in <module> 'description': "Created by " + issue.author.name + " at " + issue.created_at + " :\n" + str(issue.description), AttributeError: 'dict' object has no attribute 'name'

Maybe you have an idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment