Skip to content

Commit

Permalink
Add integration with GitHub issues. Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jun 9, 2017
1 parent a493cf2 commit 80b7dc8
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ setting. By default this setting is not provided and the code uses
``jira.JIRA.DEFAULT_OPTIONS``.


Integration with GitHub Issues
------------------------------

When configuring a GitHub Issue Tracker you can leave the ``API URL`` and
``API username`` fields empty. The code uses the ``Base URL`` field to figure
out to which repository you want to communicate.

.. important::

GitHub authentication is performed via token which needs to be configured
in the ``API password or token`` field!

.. note::

GitHub does not support displaying multiple issues in a table format like
Bugzilla and JIRA do. This means that in Test Case Run Report view you will
see GitHub issues listed one by one and there will not be a link to open all
of them inside GitHub's interface!


Kerberos authentication
-----------------------

Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ django-uuslug==1.1.8
odfpy>=0.9.6
python-bugzilla
jira
PyGithub
django-pagination==1.0.7
django-tinymce==2.4.0
beautifulsoup4>=4.1.1
Expand Down
52 changes: 52 additions & 0 deletions tcms/issuetracker/github_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
Helper which facilitate actual communications with GitHub.
"""

import warnings
import threading

from django.conf import settings
from django.core.urlresolvers import reverse


class GitHubThread(threading.Thread):
"""
Execute GitHub RPC code in a thread!
Executed from the IssueTracker interface methods.
"""

def __init__(self, rpc, tracker, testcase, bug):
"""
@rpc - GitHub object
@tracker - TestCaseBugSystem object
@testcase - TestCase object
@bug - TestCaseBug object
"""

self.rpc = rpc
self.testcase = testcase
self.bug = bug

repo_id = tracker.base_url.strip().strip('/').lower()
repo_id = repo_id.replace('https://', '').replace('http://', '').replace('github.com/', '')
self.repo = self.rpc.get_repo(repo_id)

super(GitHubThread, self).__init__()

def run(self):
"""
Link the test case with the issue!
"""

try:
text = """---- Issue confirmed via test case ----
URL: %s
Summary: %s""" % (settings.KIWI_BASE_URL + reverse('testcases-get', args=[self.testcase.pk]),
self.testcase.summary)

self.repo.get_issue(int(self.bug.bug_id)).create_comment(text)
except Exception, err:
message = '%s: %s' % (err.__class__.__name__, err)
warnings.warn(message)
58 changes: 58 additions & 0 deletions tcms/issuetracker/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import bugzilla_integration
import jira
import jira_integration
import github
import github_integration
from django.conf import settings
from django.core.urlresolvers import reverse

Expand Down Expand Up @@ -241,3 +243,59 @@ def report_issue_from_test_case(self, caserun):
url += '/'

return url + '/secure/CreateIssueDetails!init.jspa?' + urllib.urlencode(args, True)


class GitHub(IssueTrackerType):
def __init__(self, tracker):
super(GitHub, self).__init__(tracker)

# NOTE: we use an access token so only the password field is required
self.rpc = github.Github(self.tracker.api_password)

def add_testcase_to_issue(self, testcases, issue):
for case in testcases:
github_integration.GitHubThread(self.rpc, self.tracker, case, issue).start()

def report_issue_from_test_case(self, caserun):
"""
GitHub only supports title and body parameters
"""
# because of circular dependencies
from tcms.testcases.models import TestCaseText

args = {
'title': 'Failed test: %s' % caserun.case.summary,
}

txt = caserun.get_text_with_version(case_text_version=caserun.case_text_version)

if txt and isinstance(txt, TestCaseText):
plain_txt = txt.get_plain_text()

setup = plain_txt.setup
action = plain_txt.action
effect = plain_txt.effect
else:
setup = 'None'
action = 'None'
effect = 'None'

caserun_url = settings.KIWI_BASE_URL + \
reverse('testruns-get', args=[caserun.run.pk])

comment = "Filed from caserun %s\n\n" % caserun_url
comment += "Product:\n%s\n\n" % caserun.run.plan.product.name
comment += "Component(s):\n%s\n\n" % caserun.case.component.values_list('name', flat=True)
comment += "Version-Release number of selected " \
"component (if applicable):\n"
comment += "%s\n\n" % caserun.build.name
comment += "Steps to Reproduce: \n%s\n%s\n\n" % (setup, action)
comment += "Actual results: \n<describe what happened>\n\n"
comment += "Expected results:\n%s\n\n" % effect
args['body'] = comment

url = self.tracker.base_url
if not url.endswith('/'):
url += '/'

return url + '/issues/new?' + urllib.urlencode(args, True)

0 comments on commit 80b7dc8

Please sign in to comment.