51Testing软件测试论坛
标题:
jira调用api实例
[打印本页]
作者:
听海——sky
时间:
2018-4-9 16:33
标题:
jira调用api实例
服务器安装目录为:XX/atlassian/jira/atlassian-jira/WEB-INF/web.xml
问题:
/api/2/issuesecurityschemes,对于issuesecurityschemes这个只有jira 7版本以上才有,注意
如果添加项目:对应的
Create project POST /rest/api/2/project
其中:
{
"key": "EX",
"name": "Example",
"projectTypeKey": "business",
"projectTemplateKey": "com.atlassian.jira-core-project-templates:jira-core-project-management",
"description": "Example Project description",
"lead": "Charlie",
"url": "http://atlassian.com",
"assigneeType": "PROJECT_LEAD",
"avatarId": 10200,
"issueSecurityScheme": 10001,
"permissionScheme": 10011,
"notificationScheme": 10021,
"categoryId": 10120
}
如果为:
"projectTypeKey": "Software",
则:projectTemplateKey可以为:
com.pyxis.greenhopper.jira:gh-scrum-template
com.pyxis.greenhopper.jira:gh-kanban-template
com.pyxis.greenhopper.jira:basic-software-development-template
如果"projectTypeKey": "business",
则:projectTemplateKey可以为:
com.atlassian.jira-core-project-templates:jira-core-project-management
com.atlassian.jira-core-project-templates:jira-core-task-management
com.atlassian.jira-core-project-templates:jira-core-process-management
选定不同的模板。
(一) jira rest api直接调用
如果jira整合了cas,则需要用配置规则,让 url不被cas截获,输入如下:
<filter>
<filter-name>CAS Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>http://ip/cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://ip</param-value>
</init-param>
<!--忽略cas对api接口调用 start -->
<init-param>
<param-name>ignorePattern</param-name>
<param-value>^http:\/\/ip\/issues\/rest\/api\/.*</param-value>
</init-param>
<!--忽略cas对api接口调用 end -->
</filter>
<filter-mapping>
<filter-name>CAS Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
同时如果出现问题:
java.lang.NoSuchMethodError: com.atlassian.jira.user.preferences.UserPreferencesManager.clearCacheByKey(Ljava/lang/String;)V
java.lang.NoSuchMethodError: com.atlassian.jira.user.preferences.UserPreferencesManager.clearCacheByKey(Ljava/lang/String;)V
at com.atlassian.jira.user.preferences.UserPreferencesResetInterceptor.afterLogin(UserPreferencesResetInterceptor.java:24)
at com.atlassian.seraph.filter.PasswordBasedLoginFilter.runAfterLoginInterceptors(PasswordBasedLoginFilter.java:163)
at com.atlassian.seraph.filter.PasswordBasedLoginFilter.login(PasswordBasedLoginFilter.java:91)
at com.atlassian.seraph.filter.BaseLoginFilter.doFilter(BaseLoginFilter.java:134)
at com.atlassian.jira.web.filters.JiraLoginFilter.doFilter(JiraLoginFilter.java:70)
则需要修改/install/atlassian/jira/atlassian-jira/WEB-INF/classes下的seraph-config.xml文件中,最后的 ***************************************慎重
<interceptors>
<interceptor class="com.atlassian.jira.user.preferences.UserPreferencesResetInterceptor"/>
</interceptors>
修改为:
<interceptors>
<interceptor class="com.atlassian.jira.web.filters.JiraLoginInterceptor" />
</interceptors>
httpclient为:
String auth = jiraUserName + ":" + jiraPassword;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("UTF-8")));
String authHeader = "Basic " + new String(encodedAuth);
if(method.equals("get")){//get方法
//HttpGet httpGet = new HttpGet(url);
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
response = httpclient.execute(httpGet);
}
}
if (response.getStatusLine().getStatusCode() == status) {
result = IOUtils.toString(response.getEntity().getContent());
} else {
System.out.println("请求未成功,返回代码:" + response.getStatusLine().getStatusCode() + "; 错误信息:"
+ response.getStatusLine().getReasonPhrase());
}
httpclient.close();
return result; //返回
(二)jira客户端程序调取api
注:只需添加对应的jar,对于还需要其他的依赖,jar会自动去仓库寻找
项目需要依赖jar:项目中,只需添加如下:
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client</artifactId>
<!-- <version>2.0.0-m2</version> -->
<version>1.2-m01</version>
</dependency>
</dependencies>
jar需要依赖的仓库如下:
...
<repositories>
<repository>
<id>atlassian-public</id>
<url>https://m2proxy.atlassian.com/repository/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
</repositories>
...
<pluginRepositories>
<pluginRepository>
<id>atlassian-public</id>
<url>https://m2proxy.atlassian.com/repository/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
...
注:如果出现
JerseyJiraRestClientFactory doesn't exist
则是jira-rest-java-client采用的版本太高,降低版本即可。
如低版本的
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client</artifactId>
<!-- <version>2.0.0-m2</version> -->
<version>1.2-m01</version>
</dependency>
</dependencies>
完整如下:pom.xml
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.cmri.test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<name>test Portlet</name>
<version>1</version>
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client</artifactId>
<!-- <version>2.0.0-m2</version> -->
<version>1.2-m01</version>
</dependency>
</dependencies>
<!--
<repositories>
<repository>
<id>atlassian-public</id>
<url>https://maven.atlassian.com/repository/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
</repositories>
-->
<repositories>
<repository>
<id>atlassian-public</id>
<url>https://m2proxy.atlassian.com/repository/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>atlassian-public</id>
<url>https://m2proxy.atlassian.com/repository/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
复制代码
问题说明:
如果出现用户其他信息的问题,例如添加Basic Authentication认证,如果出现各种错误,可能原因是你的用户
名和密码不正确,
此系统中,通过liferay单点进入jira,jira通过cas认证,其中,对于Jira中如果在Liferay中认证如果通过,则直
接进入Jira,导致直接通过Basic Authentication认证时,其用户名和密码有可能与Jira系统中不一致,故需要真
正的Jira账号和你们去测试,对于Jira api的调用,官网上有明确的文档,查看即可。
对于cas的拦截,如
输入http://ip/issues/ ,跳转到登录界面,则说明cas进行了拦截,否则,没有进行拦截。
即直接在浏览器上输入ip,如果跳转则说明cas进行了拦截,否则,没有进行拦截。
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2