python jira 库

REST API :https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/issue-getIssue

实例1

  • 监控某个 project 下的所有具有 issuelink 的上级 issue 的变化
    • 不区分 link 的关联方式
    • 不关注下级 issue 的变化
  • 当 issuelink 的上级 issue 发生变化,将改变写入下级 issuelink 的 comment 里, 由 jira 触发邮件通知给 Assignee 、Reporter.
#!/usr/bin/env python3.8
# coding=utf-8


import datetime as dt

from jira import JIRA


class GetJiraIssueDetail:
    def __init__(self):
        self.jira_basic_auth = ('$jira_username', '$jira_password')
        self.jira_server = JIRA(basic_auth=self.jira_basic_auth, options={'server': 'http://jira.test.cn'})

    
    """
       查询 project 下 issuetype 为 "Story" 且 resolution 为 "Unresolved" 且 status 为 "To Do" 和 "In Progress"
       maxResults=-1 表示返回全部的结果,默认搜索返回前50个结果
    """
    def get_issue_from_project(self, project_key):
        """
        Get issue from project
        :param project_key:  jira project key
        :return: list[Issue Info]
        """
        try:
            issues = self.jira_server.search_issues(('project = "%s" AND issuetype = "Story" AND resolution = "Unresolved" AND status in ("To Do","In Progress") ORDER BY priority DESC, updated DESC') % (project_key), maxResults=-1)
            return issues
        except Exception as getIssueError:
            return getIssueError

    def get_issue_comments(self, issue_key):
        """
        Get issue comments
        :param issue_key: issue key
        :return: List[Comment Info]
        """

        try:
            issue_comment = self.jira_server.comments(issue_key)
            return issue_comment
        except Exception as getCommentError:
            return getCommentError

    """
       获取 issue 的 history,issue 所有的改动不包括添加 comment ,都会记录到 changelog 中
    """
    def get_issue_changelog(self, issue_key, expand=None):
        """
        Get issue change log
        :param issue_key: issue key
        :return:
        """
        try:
            issue_changelog = self.jira_server.issue(issue_key, expand=expand)
            return issue_changelog
        except Exception as getChangelogError:
            return getChangelogError
    
    """
       获取过去 24 小时的 issue changelog 和 comment,将更改写入 issuelink 的 comment 里
    """
    def update_issuelinks_comment(self, issue_key):
        """
        Gets issue changes made in the last 24 hours, Write the changes to Issuelink comment
        :param issue_key:
        :return:
        """
        now_time = dt.datetime.now()
        given_time = now_time - dt.timedelta(hours=24)
        issue_changelog = self.get_issue_changelog(issue_key, expand="changelog")

        if issue_changelog.fields.issuelinks:
            for issuelink in issue_changelog.fields.issuelinks:
                try:
                    print(issue_key, issuelink.outwardIssue.key)
                    # 判断下 issuelink 是否为 outwardIssue,也就是是否为下级 issuelink,如果是 inwardIssue,说明该 issue 是另外一个 issue 的 link,这里我们只关注上级 issue 的变化
                    if issuelink.outwardIssue.fields.status:
                        for history in issue_changelog.changelog.histories:
                            changelog_update_time = dt.datetime.strptime(history.created, '%Y-%m-%dT%H:%M:%S.%f+0800')
                            # Get issue change log in the last 24 hours
                            if changelog_update_time > given_time:
                                for item in history.items:
                                    changelog_comment_text = "The parent link %s changes the %s at %s, Change From: %s , Modified contents: %s" % (issue_changelog.key, item.field, changelog_update_time, history.author, item.toString)
                                    print(changelog_comment_text)

                                    # Write the parent link's changes to the child link's comments
                                    self.jira_server.add_comment(issuelink.outwardIssue.key, changelog_comment_text)

                        # Get issue comments in the last 24 hours
                        issue_comments = self.get_issue_comments(issue_key)
                        for comment in issue_comments:
                            comment_create_time = dt.datetime.strptime(comment.created, "%Y-%m-%dT%H:%M:%S.%f+0800")
                            if comment_create_time > given_time:
                                comment_text = "The parent link %s added a comment at %s, Comment from: %s Content of comments: %s" % (issue_changelog.key, comment_create_time, comment.author.displayName, comment.body)
                                print(comment_text)

                                # Write the parent link's changes to the child link's comments
                                self.jira_server.add_comment(issuelink.outwardIssue.key, comment_text)

                except Exception as error:
                    return error


if __name__ == '__main__':
    issueLinkNotify = GetJiraIssueDetail()
    issues = issueLinkNotify.get_issue_from_project(project_key="DEV")

    for issue in issues:
        if issue.fields.issuelinks:
            issueLinkNotify.update_issuelinks_comment(issue_key=issue.key)

Jira中最常用的JQL搜索语句

Jira Query Language简称JQL,它是Jira中最有⽤的功能之⼀。 Jira中积累了⽇常⼯作中记录的很多数据,要想从中找到想要的内容,就必须掌握搜索技能,也就是要能熟练使⽤JQL。 image.png

JQL 语法:

官方文档

JQL 语法简介

JIRA 的 JQL 语法由以下几个元素组成

field (字段) : 就是要搜索的JIRA Issue 的各个字段 operator(运算符或者也叫操作符):如 =, < , > , in 等 value(值):具体要查询的字段匹配的值 keyword(关键字): keyword这个字面上理解可能会带来些歧义, 主要有以下作用 连接两个表达式,即通常我们所说的逻辑运算符:AND, OR, NOT 排序运算符:ORDERBY 还有一部分就是表示空的关键字: NULL 和 EMPTY,这两个貌似才是通常意义上的关键字 function(方法):即JIRA提供的一些方法,如 now()表示当前时间,currentUser()表示当前用户等

语法的示例如图: image.png

image.png

参考博客:

https://doc.devpod.cn/jira/jql-3244098.html https://www.cnblogs.com/snailgirl/p/9417642.html https://blog.csdn.net/alice_tl/article/details/101784046 https://wenku.baidu.com/view/1ad23f59a16925c52cc58bd63186bceb19e8edc3.html https://wenku.baidu.com/view/058af929a5c30c22590102020740be1e650eccee.html https://jqlsearchextensions.atlassian.net/wiki/spaces/SEARCH/pages/2823684101/Getting+started+with+JQL+Search+Extensions

未完待续...