PowerShell调用jira rest api实现jira统计自动化

简介: 通过调用JIRA Rest web api实现统计自动化,首先进行登录模拟: $content = @{username='用户名';password='密码'} $JSON=$content|convertto-JSON -Compress $res = Invoke-WebReques...

通过调用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")

然后查询所有分派给我的任务,并遍历每个任务取出想要的信息(例如:报告人、开发、前端、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
}

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

相关文章
|
7天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
27 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
29天前
|
前端开发 JavaScript API
基于React的简易REST API客户端设计与实现
基于React的简易REST API客户端设计与实现
21 3
|
2月前
|
JSON 关系型数据库 测试技术
Eolink神技之五、API自动化——定时任务
Eolink神技之五、API自动化——定时任务
44 0
|
3月前
|
前端开发 测试技术 API
UI自动化与API自动化已经开始互斥了吗?
UI自动化与API自动化已经开始互斥了吗?
|
3天前
|
测试技术 API 网络架构
Python的api自动化测试 编写测试用例
【4月更文挑战第18天】使用Python进行API自动化测试,可以结合`requests`库发送HTTP请求和`unittest`(或`pytest`)编写测试用例。以下示例: 1. 安装必要库:`pip install requests unittest` 2. 创建`test_api.py`,导入库,定义基础URL。 3. 创建继承自`unittest.TestCase`的测试类,包含`setUp`和`tearDown`方法。 4. 编写测试用例,如`test_get_users`,检查响应状态码和内容。 5. 运行测试:`python -m unittest test_api.py`
12 2
|
3天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
9 3
|
1月前
|
JSON 测试技术 API
Postman Newman 实现 API 自动化测试的快速指南
Newman 是一款专为 Postman 打造的命令行工具,旨在通过自动运行 Postman 集合和环境,实现 API 测试的自动化。它使得开发者无需打开 Postman 图形界面,即可直接在命令行中执行测试用例。
|
2月前
|
存储 关系型数据库 API
Python 任务自动化工具:nox 的配置与 API
Python 任务自动化工具:nox 的配置与 API
22 0
|
2月前
|
数据采集 数据挖掘 API
通过API接口实现自动化数据同步
在当今数字化的世界中,API(应用程序编程接口)作为数据交换的桥梁,对于电商企业来说尤为重要。它们允许企业从丰富的数据源中提取必要的信息,为商业决策提供数据支持。本文将围绕如何高效地利用API进行数据采集展开讨论,并提供一些实用的代码示例。
|
3月前
|
JSON 缓存 API
title: 深入理解REST API设计的最佳实践
title: 深入理解REST API设计的最佳实践
36 0