Jira provides a complete RESTful API. If you don’t want to directly request the API interface, you can use the Python Jira library to operate Jira.
Jira Python documentation
installation method
pip install jira
Certification
Jira's access is authorized. When accessing the Jira project, you must first authenticate. The Jira Python library provides three authentication methods:
Only one authentication method needs to be selected. The following code uses Cookies for authentication.
form jira import JIRA
jira = JIRA('http://jira.***.com/', auth=('user name','login password')
The returned jira object can operate on Jira. The main operations include:
Project
jira.projects(): View a list of all projects
jira.project("Key of the project"): View a single project
The main attributes and methods of the project object are as follows:
Example
print(jira.projects()) # Print a list of all projects you have permission to access
project = jira.project('Key of a project')
print(project.key, project.name, project.lead)
Issue (Issue)
Issue is the core of Jira. The tasks, user stories, and bugs in Jira are essentially an issue.
A single issue object can be obtained through jira.issue ("the key of the issue"). The main attributes and methods of the issue are as follows:
Configure Fields (Fields)
The attributes in ields of general problems are divided into fixed attributes and custom attributes. The format of custom attributes is generally similar to customfield_10012. Fields of commonly used questions are:
Examples are as follows:
issue = jira.issue(‘JRA-1330’)
print(issue.key, issue.fields.summary, issue.fields.status)
Followers/comments/attachments
Examples are as follows:
issue = jira.issue('JRA-1330')print(jiaa.watchers(issue)) #All followers
jira.add_watcher(issue,'username') #Add followers
print(jira.comments(issue)) #All comments
comment = jira.comment(issue,'10234') #A comment
jira.add_comment(issue,'new comment') #Add comment
comment.update(body='update comment') #Update comment
comment.delete() #Delete this comment
print(issue.fields.attachment) #Question attachment
jira.add_attachment(issue=issue, attachment='/some/path/attachment.txt') #Add attachments
Create/Assign/Convert issues
Examples are as follows:
# Create question
issue_dict ={'project':{'id':123},'summary':'New issue from jira-python','description':'Look into this one','issuetype':{'name':'Bug'},}
new_issue = jira.create_issue(fields=issue_dict)
# Create questions in bulk
issue_list =[{'project':{'id':123},'summary':'First issue of many','description':'Look into this one','issuetype':{'name':'Bug'},},{'project':{'key':'FOO'},'summary':'Second issue','description':'Another one','issuetype':{'name':'Bug'},},{'project':{'name':'Bar'},'summary':'Last issue','description':'Final issue of batch.','issuetype':{'name':'Bug'},}]
issues = jira.create_issues(field_list=issue_list)
# Distribution problem
jira.assign_issue(issue,'newassignee')
# Conversion problem
jira.transition_issue(issue,'5', assignee={'name':'pm_user'}, resolution={'id':'3'})
search for
Jira's search is very powerful, and is equipped with a set of special search language called JQL (Jira Query Language). Jira's Python library is searched based on JQL syntax and returns a list of the searched questions.
The use statement is
jira.search_issues('JQL statement')
The default maximum number of results is not 1000, which can be configured through the maxResults parameter. When this parameter is -1, there is no limit to the number and all search results are returned.
jira.search_issues('project=PROJ and assignee = currentUser()', maxResults=-1)
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts