在Python實作Bugzilla Web Service功能(一)中我們實作了http Get/Put/Post功能。本篇介紹基于http Get/Put/Post功能實作對Bugzilla的一些基本操作,如Bug查詢、Bug曆史、建立Bug、更新Bug等實作。
file: bugs.py
#encoding=utf-8
from [common](https://blog.csdn.net/leenuxcore/article/details/93499330) import *
@RequestGetDecorator
def Get_Bug(id_alias, url_base, api_key, include_fields=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#get-bug
'''
req = url_base + "/rest/bug/" + str(id_alias)
params ={"api_key":api_key}
if include_fields:
params.update({"include_fields":include_fields})
return req,params
@RequestGetDecorator
def Bug_History(id, url_base, api_key, new_since=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#bug-history
new_since=YYYY-MM-DD
'''
req = "{url_base}/rest/bug/{id}/history".format(url_base=url_base,id=str(id))
params = {"api_key":api_key}
if new_since:
params.update({"new_since" :new_since})
return req,params
@RequestGetDecorator
def Search_Bugs(url_base, api_key, *args, **kwargs):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#search-bugs
quicksearch:
https://www.squarefree.com/bugzilla/quicksearch-help.html
http://eigen.tuxfamily.org/bz/page.cgi?id=quicksearch.html
'''
options = ["alias","assigned_to", "component","creation_time","creator","id",
"last_change_time","limit","offset","op_sys","platform","priority",
"Product","resolution","severity","status","summary","tags",
"target_milestone","qa_contact","url","version","whiteboard","quicksearch",
"include_fields",'url_base', 'api_key'
]
params = {"api_key": api_key}
if kwargs:
for key in kwargs.keys():
if key not in options:
err = "\'" + key + "\' is not supported!"
raise Exception(err)
params.update(kwargs)
else:
raise Exception("one of {} must be set!".format(options))
req = url_base + "/rest/bug"
return req,params
@RequestPostDecorator
def Create_Bug(url_base, api_key, *args, **kwargs):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#create-bug
'''
options_in_need = ['product', 'component', 'version','cf_change_type', 'cf_probability',"cf_cusprj", 'summary']
options = [
"description","op_sys","target_milestone","flags",
"platform","priority","severity","alias","assigned_to","cc",
"comment_is_private","groups","qa_contact","status","resolution"
]
req = url_base + "/rest/bug"
params = {"api_key":api_key}
if kwargs:
for opt in options_in_need:
if opt not in params.keys():
raise Exception(opt+" must be set")
for key in kwargs.keys():
if key not in options:
err = "\'" + key + "\' is not supported!"
raise Exception(err)
params = kwargs.copy()
else:
raise Exception("{} must be set".format(",".join(options_in_need)))
return req, params
@RequestPutDecorator
def Update_Bug(url_base, api_key, id, **kwargs):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/bug.html#update-bug
'''
options = [
"alias","assigned_to","blocks","depends_on","cc","is_cc_accessible",
"comment","comment_is_private","component","deadline","dupe_of","estimated_time",
"flags","groups","keywords","op_sys","platform","priority",
"product","qa_contact","is_creator_accessible","remaining_time","reset_assigned_to","reset_qa_contact",
"reset_qa_contact","resolution","see_also","severity","status","summary",
"target_milestone","url","version","whiteboard","work_time"
]
params={"api_key":api_key}
if not kwargs:
raise Exception("kwargs cannot be empty!")
else:
for key in kwargs.keys():
if key not in options:
err = "\'" + key + "\' is not supported!"
raise Exception(err)
req = url_base + "/rest/bug/" + str(id)
params.update(kwargs)
return req, params
@RequestGetDecorator
def Bug_Fields(url_base, api_key, field=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/field.html#fields
'''
params={"api_key":api_key}
req = url_base + "/rest/field/bug"
if field:
req = req + "/" +field
return req, params
@RequestGetDecorator
def Field_values(url_base, api_key, field, product_id=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/field.html#legal-values
'''
params = {"api_key":api_key}
req = url_base + "/rest/field/bug/{field}".format(field=field)
if product_id:
req = req + "/{product_id}".format(product_id=product_id)
req = req + "/values"
return req, params
#comments.py
#encoding=utf-8
from common import *
@RequestGetDecorator
def Get_Comments(url_base, api_key, id=None, comment_id=None, new_since=None, include_fields=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/comment.html#get-comments
parameters:
id: A single integer bug ID
comment_id: A single integer comment ID
new_since: datetime, YYYY-MM-DD
include_fields: filt fields you want
useages:
case 1: Get_Comments(id), get all comments of BUG[d]
case 2: Get_Comments(id, new_since) get all comments of BUG[id] since [new_since]
case 3: Get_Comments(comment_id) get specified comment by [comment_id]
'''
params = {"api_key":api_key}
if comment_id:
req = url_base + "/rest/bug/comment/{comment_id}".format(comment_id=comment_id)
elif id:
req = url_base + "/rest/bug/{id}/comment".format(id=id)
if new_since:
params.update({"new_since":new_since})
else:
raise Exception("parameter error! param 'id' or comment_id must 'be' set!")
if include_fields:
params.update({"include_fields":include_fields})
return req, params
@RequestPostDecorator
def Create_Comments(url_base, api_key, id, comment, is_private=False, work_time=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/comment.html#create-comments
This allows you to add a comment to a bug in Bugzilla.
parameters:
id, int, bug id.
comment, string, bug comment to be added.
is_private, boolean, comment is private or not.
work_time, double, Adds this many hours to the “Hours Worked” on the bug. If you are not in the time tracking group, this value will be ignored.
'''
params = {
"api_key":api_key,
"comment":comment,
"is_private":is_private
}
if work_time:
params.update({"work_time":work_time})
req = url_base + "/rest/bug/{id}/comment".format(id=id)
return req, params
@RequestGetDecorator
def Search_Comments_Tags(url_base, api_key, query):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/comment.html#search-comment-tags
Searches for tags which contain the provided substring.
'''
params={"api_key":api_key}
req = url_base + "/rest/bug/comment/tags/{query}".format(query=query)
return req, params
@RequestPutDecorator
def Update_Comments_Tags(url_base, api_key, comment_id, add, tags):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/comment.html#update-comment-tags
Adds or removes tags from a comment.
parameters:
comment_id,
add, True for add and False for remove
tags, tags to be added in list format
'''
params = {
"comment_id":comment_id,
"api_key":api_key
}
req = url_base + "/rest/bug/comment/{comment_id}/tags".format(comment_id=comment_id)
if type(tags) != list:
raise Exception("type error, type of \'tags\' must be a list")
if add:
params.update({"add":tags})
else:
params.update({"remove":tags})
return req, params
file: attachments.py
#encoding=utf-8
import requests
import json
from common import *
from comments import Get_Comments
@RequestGetDecorator
def Get_Attachments(url_base, api_key, bug_id=None, attachment_id=None):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/attachment.html#get-attachment
'''
params={"api_key":api_key}
if bug_id:
req = url_base +"/rest/bug/{bug_id}/attachment".format(bug_id=bug_id)
elif attachment_id:
req = url_base +"/rest/bug/attachment/{attachment_id}".format(attachment_id=attachment_id)
else:
raise Exception("parameter error!")
return req, params
def Get_CQ_Attachment_Path(url_base, api_key, id):
bSuccess, comments = Get_Comments(url_base, api_key, id)
if not bSuccess:
return None
comment0 = comments['bugs'][str(id)]['comments'][0]
text = comment0['text']
key = "Attachments Shared Path and FTP Path:"
start = text[text.find(key) + len(key):].split("\n")
attachment_path = start[1]
return attachment_path
@RequestPostDecorator
def Create_Attachment(url_base, api_key, bug_id, *args, **kwargs):
'''
https://bugzilla.readthedocs.io/en/5.0/api/core/v1/attachment.html#create-attachment
'''
req = url_base + "/rest/bug/{bug_id}/attachment".format(bug_id=bug_id)
options = [
"ids","data","file_name","summary","content_type",
"comment","is_patch","is_private","flags"
]
params={"api_key":api_key}
if kwargs:
for param in params_in_need:
if param not in kwargs.keys():
err = "param \'{}\' muset be set!".format(param)
raise Exception(err)
for key in kwargs.keys():
if key not in options:
err = "\'" + key + "\' is not supported!"
raise Exception(err)
params.update(kwargs)
else:
raise Exception("param error!")
return req, params
上一篇: Python實作Bugzilla Web Service功能(一)
下一篇: Python實作Bugzilla Web Service功能(三)