【python】jiraAPI使用教程 自動創(chuàng)建jira問題單并置狀態(tài)為OPEN

  • 環(huán)境依賴:python庫 redis jira
    安裝命令:
pip install redis
pip install jira

redis服務(wù)安裝命令:

$sudo apt-get update
$sudo apt-get install redis-server

啟動 Redis :$redis-server
查看 redis 是否啟動乎婿?:$ redis-cli ping PONG

  • 自動創(chuàng)建代碼
#!/usr/bin/python
# -*- coding:utf-8 -*-
from jira import JIRA
import sys, getopt, redis, os

 class JiraTool:
    def __init__(self):
        self.server = 'https:/jira测僵。example.net'
        self.basic_auth = ('username', 'password')
        self.jiraClinet = None
 
    def login(self):
        self.jiraClinet = JIRA(server=self.server, basic_auth=self.basic_auth)
        if self.jiraClinet != None:
            return True
        else:
            return False
 
    def findIssueById(self, issueId):
        if issueId:
            if self.jiraClinet == None:
                self.login()
            return self.jiraClinet.issue(issueId)
        else:
            return 'Please input your issueId'
 
    def createIssue(self, description, assignee, project, parent, filepath):
 
        issue_dict = {
 
            'project': {'key': project},
            'issuetype': {'name': 'Sub-Task'},
            'parent': {'key': parent},
            'summary': '[xxx]Auto sync file %s from storage-cnn to storage'%(filepath),
            'description': description,
            'assignee': {'name': assignee},
            'customfield_12603': {'value': 'xxx'},
            'components': [{'name': 'SCM'}],
            'priority': {'name': 'Major'},
 
        }
        if self.jiraClinet == None:
            self.login()
        return self.jiraClinet.create_issue(issue_dict)
 
  if __name__ == '__main__':
    redisTool = redis.Redis(host='127.0.0.1', port=6379)
    jiraTool = JiraTool()
    jiraTool.login()
    description = ''
    assignee = ''
    watchers = None
    project = ''
    parent = ''
    openIssuekey = ''
    cnnStoragePath = 'xxxxx'
 
    opts, args = getopt.getopt(sys.argv[1:], 'd:a:w:p:o:')
    for op, value in opts:
        if op == '-d':
            filepath = value
        elif op == '-a':
            user = jiraTool.jiraClinet.search_users(user=value)
            if user:
                assignee = user[0].key
            else:
                print ('Please make sure that the user email is correct!')
                sys.exit(-1)
        elif op == '-p':
            if 'Austin' in value:
                project = 'xxxx'
                parent = 'xxxx'
                cnnStoragePath = os.path.join(cnnStoragePath,'xxxx')
            elif 'Ford' in value:
                project = 'xxxxx'
                parent = 'xxxxx'
                cnnStoragePath = os.path.join(cnnStoragePath,'xxxxx')
            else:
                print ('Please make sure that the project is correct!')
                sys.exit(-1)
        elif op == '-w':
            watchers = value.split(',')
        elif op == '-o':
            openIssuekey = value
            if not openIssuekey:
                print ('Please make sure that the openIssuekey is correct!')
                sys.exit(-1)
            break
 
    try:
        if openIssuekey:
            issuekeyTmp = redisTool.get(openIssuekey)
            issue = jiraTool.findIssueById(issuekeyTmp)
            print openIssuekey, '----', issuekeyTmp, '----', issue.fields.status
            if 'Screen' in str(issue.fields.status):
                print 'Update %s status screen ----> open ' % (issuekeyTmp)
                jiraTool.jiraClinet.transition_issue(issue, 151)
                redisTool.delete(openIssuekey)
            else:
                print 'Please make sure the issue status is not screen'
 
        else:
            if not filepath:
                print('Please make sure that the file path is correct!')
                sys.exit(-1)
            if not assignee:
                print('Please make sure that the assignee is correct!')
                sys.exit(-1)
            if not project:
                print ('Please make sure that the project is correct!')
                sys.exit(-1)
            print('Creating issue now')
            cnnStoraePath = os.path.join(cnnStoragePath,filepath)
            description = '[Files to sync]\n' + cnnStoragePath
            print "cnnStoragePath: " + cnnStoragePath
            print "description: " + description
            print "assignee: " + assignee
            issue = jiraTool.createIssue(description, assignee, project, parent,filepath)
            issueKey = issue.key
            redisTool.set(filepath, issueKey)
            print('issue key is ---->', issueKey)
            if watchers:
                for watcherItem in watchers:
                    watcher = jiraTool.jiraClinet.search_users(user=watcherItem)
                    if watcher:
                        watcherKey = watcher[0].key
                        jiraTool.jiraClinet.add_watcher(issue=issueKey, watcher=watcherKey)
                    else:
                        print('%s user does not exis' % (watcherItem))
                        jiraTool.jiraClinet.add_comment(issue=issueKey, body='%s user does not exis' % (watcherItem))
 
     except Exception as e:
        print e
        sys.exit(-1)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谢翎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子森逮,更是在濱河造成了極大的恐慌褒侧,老刑警劉巖拖吼,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件这吻,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機移怯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門香璃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舟误,你說我怎么就攤上這事葡秒。” “怎么了嵌溢?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵眯牧,是天一觀的道長。 經(jīng)常有香客問我赖草,道長学少,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任秧骑,我火速辦了婚禮版确,結(jié)果婚禮上扣囊,老公的妹妹穿的比我還像新娘。我一直安慰自己绒疗,他們只是感情好侵歇,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著吓蘑,像睡著了一般盒至。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上士修,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音樱衷,去河邊找鬼棋嘲。 笑死,一個胖子當(dāng)著我的面吹牛矩桂,可吹牛的內(nèi)容都是我干的沸移。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼侄榴,長吁一口氣:“原來是場噩夢啊……” “哼雹锣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起癞蚕,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蕊爵,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后桦山,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體攒射,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年恒水,在試婚紗的時候發(fā)現(xiàn)自己被綠了会放。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡钉凌,死狀恐怖咧最,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情御雕,我是刑警寧澤矢沿,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站饮笛,受9級特大地震影響咨察,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜福青,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一摄狱、第九天 我趴在偏房一處隱蔽的房頂上張望脓诡。 院中可真熱鬧,春花似錦媒役、人聲如沸祝谚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽交惯。三九已至,卻和暖如春穿仪,著一層夾襖步出監(jiān)牢的瞬間席爽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工啊片, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留只锻,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓紫谷,卻偏偏與公主長得像齐饮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子笤昨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容