PowerShell调用Jira REST API实现统计

通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟:

$content = @{username='用户名';password='密码'}
$JSON=$content|convertto-JSON -Compress
$res = Invoke-WebRequest -Uri "http://jira地址/rest/auth/1/session" -Method Post -Body $JSON -ContentType application/json
$webClient = new-object net.webclient
#Set encoding style here.
$webClient.Encoding=[System.Text.Encoding]::GetEncoding("utf-8")
<# Note that the response contains the Set-Cookie HTTP headers that must be honoured by the caller. If you are using a cookie-aware HTTP client then it will handle all Set-Cookie headers automatically. This is important because setting the JSESSIONID cookie alone may not be sufficient for the authentication to work. #> $webClient.Headers.add("Cookie", $res.Headers["Set-Cookie"]) #Write-Host "调用获取登录状态接口" -ForegroundColor Green #$webClient.DownloadString("http://jira地址/rest/auth/1/session") #Write-Host "调用退出登录接口" -ForegroundColor Green #$webClient.UploadString("http://jira地址/rest/auth/1/session","DELETE","") #Write-Host "调用获取登录状态接口" -ForegroundColor Green #$webClient.DownloadString("http://jira地址/rest/auth/1/session")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

然后查询所有分派给我的任务,并遍历每个任务取出想要的信息(例如:报告人、开发、前端、Jira创建时间等信息):

$jiraUri = "jira地址"
#查询所有分派给天外归云的任务
#Search using search request.通过查找接口用jql语句来进行查找(首先要创建一个JSON对象做为查找时post的body)
#在PowerShell中创建JSON对象.
$JSON = @"
{
    "jql": "分派给 = 天外归云",
    "startAt": 0,
    "maxResults": 1000,
    "fields": [
        "summary",
        "status",
        "assignee"
    ]
}
"@
$apiUri = "/rest/api/2/search"
$uri = $jiraUri+$apiUri
#Post json必须加的header.
$webClient.Headers.Add("Content-Type", "application/json");
$searchResult = $webClient.UploadString($uri,$JSON)
#获取所有的issues(分派给天外归云的)
$issues = ($searchResult|ConvertFrom-Json).issues
#判断有没有这种field
function NullOrNot($field){
    if(($field -ne $null) -and ($field -ne ""))
    {
       $field
    }else{
        $field="displayName : Null"
    }
}
#提取人员名单
function GetDisplayName($oName){
    $displayNames = $oName|findstr "displayName"
    if($displayNames.count -ne 1){
        foreach($displayName in $displayNames){
            $newDisplayName += $displayName.split(":")[1]
            $newDisplayName += " "
        }
        $newDisplayName
    }else{
        $displayNames.split(":")[1]
    }
}
#遍历jira issue
foreach($issue in $issues){
    $apiUri = $jiraUri+"/rest/api/2/issue/"+$issue.key
    $issueInfo = $webClient.DownloadString($apiUri)
    $issueInfo = $issueInfo|ConvertFrom-Json
    #$issueInfo.fields
    $reporter = GetDisplayName(NullOrNot($issueInfo.fields.reporter))
    Write-Host "报告人:"$reporter
    $productor = GetDisplayName(NullOrNot($issueInfo.fields.customfield_10206))
    Write-Host "产品人员:"$productor
    $qianDuan = GetDisplayName(NullOrNot($issueInfo.fields.customfield_10207))
    Write-Host "前端:"$qianDuan
    $developer = GetDisplayName(NullOrNot($issueInfo.fields.customfield_10208))
    Write-Host "开发:"$developer
    $fenPai = GetDisplayName(NullOrNot($issueInfo.fields.customfield_10002))
    Write-Host "分派给:"$fenPai
    $tiCeTime = $issueInfo.fields.created
    Write-Host "提测时间:"$tiCeTime
    Write-Host "用例数据:"$issueInfo.fields.customfield_11402 $issueInfo.fields.customfield_10400
    Write-Host "bug数:"$issueInfo.fields.customfield_10202
    Read-Host
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

以上过程中也包含了PowerShell应用于web接口测试的核心方法!

, ,
 
 
 
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空