PowerShell调用jira rest api实现对个人提交bug数的统计

通过PowerShell的invoke-webrequest和net.client联合实现个人指定项目jira提交数的统计,其中涉及到了JSON对象的提交,代码如下:

$content = @{username='用户名';password='密码'}
$JSON = $content|convertto-JSON -Compress
$jiraUri = "http://jira.ms.netease.com"
$apiUri = $jiraUri+"/rest/auth/1/session"
$res = Invoke-WebRequest -Uri $apiUri -Method Post -Body $JSON -ContentType application/json
$webClient = new-object net.webclient
#Set encoding style here.
$webClient.Encoding = [System.Text.Encoding]::GetEncoding("utf-8")
$webClient.Headers.add("Cookie", $res.Headers["Set-Cookie"])
$keyWord = Read-Host "请输入搜索关键词(多个关键词请用空格隔开)"
$jql = "reporter in (用户名) AND text ~ '"+$keyWord+"'"
$JSON = @"
{
    "jql": "$jql",
    "startAt": 0,
    "maxResults": 1000,
    "fields": [
        "summary",
        "status",
        "assignee"
    ]
}
"@
$apiUri = "/rest/api/2/search"
$uri = $jiraUri+$apiUri
#Post json must added header.
$webClient.Headers.Add("Content-Type", "application/json");
$searchResult = $webClient.UploadString($uri,$JSON)
#Get issues.
$issues = ($searchResult|ConvertFrom-Json).issues
foreach($issue in $issues){
    $issue.fields.summary
}
Write-Host "与关键词相关的jira共有"$issues.Count"" -ForegroundColor Green
Read-Host

保存脚本到桌面右键PowerShell运行或者直接在ise中按F5反复运行都可以,效果如下:

原理:根据jql来实现通过标题关键字对jira的查找,也用到了jira rest api。

posted @ 2016-05-06 21:19  天外归云  阅读(771)  评论(0编辑  收藏  举报