`

使用Java调用Mantis提供的webservice, 获得Mantis数据

阅读更多

写在前面

 

如果使用Mantis来管理bug, 项目比较小, 项目比较少的情况下,项目的bug状况还是一目了然,

 

但对于我们公司来说, 现在运行中的mantis有上百个项目, 1W余件bug,

在这种情况下, mantis提供管理功能就稍显薄弱,

比如, Mantis提供的能帮助管理层把握全局的功能比较少, 对bug数据的分析功能也比较少.

 

为此我们希望能够针对mantis做一些自己的工具,

一来,希望能够对上面的功能做一个补充,

二来,也希望透过这个工具, 让所有人都能够, 更方便的了解到他们应该知道的,以及他们想知道的bug信息.

 

我打了一个比方, 这个工具更像是我们的一个朋友, 或者一个很贴心的秘书.

针对bug的各种各样的问题, 我们都可以把答案交给他/她来办~

这些问题包括: 日常的, 例行的, 统计起来枯燥繁琐的, 还有一些靠我们自己不太好办到的....

 

 

明确了目标, 接下来是调查工作

 

为了办到上面的事情, 首先面临的第一步就是如何从mantis中,把bug数据取出来.

我们的做法正如标题那样:

使用Java调用Mantis提供的webservice, 获得Mantis数据

 

MantisBT Frequently Asked Questions 上面, 有下面这样的介绍.

写道
Does MantisBT provide a webservice interface?

See the MantisConnect project which provides a PHP webservice that can be used from any language that supports SOAP webservices. MantisConnect also includes client libraries for .NET, Java and Cocoa.

MantisConnect also includes sample applications that uses the provided client libraries to provide useful tools. This includes an Eclipse plug-in written in Java, a NAnt task to submit MantisBT issues written in .NET, and many others.

MantisConnect now comes as part of the standard MantisBT installation package and can be found under the ‘{$mantis_install_dir}/api/soap’ directory. To see what functionality is currently provided via MantisConnect, check the WSDL available here.

 

就是说,有一个项目叫做MantisConnect, 他可以在server端, 为我们提供webservice服务. 而且这个项目现在已经默认集成到了mantis的安装包中. 也就是这些webservices会随mantis安装自带. 另外这个项目还未我们提供了client端, 方便我们连接这些webservice借口, 包括java, .net, 还有cocoa的.

 

我们来到了MantisConnect 这个项目, 查看了他的许可 , 如下:

写道
There are three levels of license:

■Professional - This license allows distributing an application with MantisConnect client libraries as part of it. It also allows installation of MantisConnect web service on a single website.
■Standard - This license allows the use of MantisConnect client libraries and an installation of the web service on a single website. This license doesn't allow distributing MantisConnect outside the licensed company.
■Free for Open Source - This license allows free usage of MantisConnect client libraries and web service for Mantis instances that ONLY host open source or freeware applications.

 

看来在公司内部使用, 只要不发布他, 没有问题.

 

 

所以下面上来的就是demo

下载MantisConnect client的source包的时候, 里面有对应的单元测试文件.

这个demo及时我参照单元测试抽出来的.

 

 

import java.math.BigInteger;
import java.net.URL;

import org.mantisbt.connect.axis.AccountData;
import org.mantisbt.connect.axis.IssueData;
import org.mantisbt.connect.axis.IssueHeaderData;
import org.mantisbt.connect.axis.MantisConnectLocator;
import org.mantisbt.connect.axis.MantisConnectPortType;

/**
 * see the following links for the document of APIs.
 * http://www.mantisforge.org/dev/phpxref/api/soap/index.html
 * http://www.mantisforge.org/dev/phpxref/nav.html?api/soap/mc_project_api.php.html
 * http://www.mantisforge.org/dev/phpxref/nav.html?api/soap/mc_issue_api.php.html
 */
public class GetDataFromMantisDemo {

	public static void main(String[] args) throws Exception {
		String user = "administrator";
		String pwd = "mypassword";
		URL url = new URL("http://localhost/mantis/api/soap/mantisconnect.php");

		MantisConnectLocator mcl = new MantisConnectLocator();
		MantisConnectPortType portType = mcl.getMantisConnectPort(url);

		BigInteger project_id = new BigInteger("1030");

		// demo 01
		// get a bug by bug_id
		IssueData aIssueData = portType.mc_issue_get(user, pwd, new BigInteger("13547"));
		System.out.println(aIssueData.getId());
		System.out.println(aIssueData.getSummary());

		// demo 02
		// get the account of a project.
		AccountData[] users = portType.mc_project_get_users(user, pwd,project_id, new BigInteger("90"));
		for (AccountData ad : users) {
			System.out.println(ad.getName());
		}

		// demo 03
		// get the issue headers of a project.
		IssueHeaderData[] headers = portType.mc_project_get_issue_headers(user,pwd, project_id, BigInteger.valueOf(1), BigInteger.valueOf(50));
		for (IssueHeaderData header : headers) {
			System.out.println(header.getId() + "\t" + header.getSummary());
		}

		//demo 04
		// get the issues of a project.
		IssueData[] issues = portType.mc_project_get_issues(user, pwd, project_id, BigInteger.valueOf(1), BigInteger.valueOf(10));
		for (IssueData issueData : issues) {
			System.out.println(issueData.getId() + "\t"+ issueData.getStatus().getName() + "\t"+ issueData.getSummary());
		}
	}
}
 

 

最后上的是trouble shooting.

在调上面的程序的时候, 我的确遇到了很多trouble(都是些让人郁闷的问题), 下面说说怎么解决的.

 

首先要确定我们的用户名密码还有url写的没错.

关于url, 我下载的客户端中的build.properties.sample文件里是这样配置的:

http://localhost:8080/mantis-1.1.1/api/soap/mantisconnect.php

但是很多公司的端口可能不是8080,

使用的url名字也很可能不是mantis-1.1.1, 而是形如<mycompanyname><myprojectname>这样的名字.

 

接下来就要搞懂各个函数的参数的意义, 确保你给各个函数传递了正确的参数.

关于参数的意义, 比较遗憾的是: 下载的javadoc中不够详细.

不过还好, 我们可以求助下面的链接.

http://www.mantisforge.org/dev/phpxref/api/soap/index.html
http://www.mantisforge.org/dev/phpxref/nav.html?api/soap/mc_project_api.php.html
http://www.mantisforge.org/dev/phpxref/nav.html?api/soap/mc_issue_api.php.html

 

这些文档是webservices的server端php代码.

因为java client端在包装这些代码的时候使用了相同的方法名字,所以我们可以很容易确认到服务器端对应的php source.

比如下面这段:

写道
mc_project_get_issues( $p_username, $p_password, $p_project_id, $p_page_number, $p_per_page ) X-Ref
No description

从上面的信息一看, mc_project_get_issues的参数意义就一目了然了.

参数的类型就交给编译器,

通过上面方法可以找到你想要的函数, 如果需要上面函数的示例, 参见对应函数的单元测试代码.

 

通常情况下, 故事到这里就差不多该结束了.

因为一般情况下通过上面的步骤, client端的代码应该就没有问题,

而server端的代码是随安装过程自带的, 而且理论上你也应该没有动过他, 所以他也应该没有问题.

但是我的情况没有那么幸运,

因为我们的mantis已经被前仆后继的定制过了.

(如果一个mantis达到了我前面提到的规模, 很多重量级人物必然都知道了他的存在, 然后大家就会提出很多关怀性质的改善意见, 然后自己定制mantis的过程就开始了.....)

 

其中便有两处改动影响到了mc_project_get_issues和mc_project_get_issue_headers这两个方法.

使其不能正常运行, 抛出了很深的异常(如果我把它贴上来, 我想你一定会因为太长而感到郁闷 ^_^).

因为我用java调用mc_project_get_users和mc_issue_get这些方法都没有问题.

所以我确定,

一定是server端的mc_project_get_issues和mc_project_get_issue_headers这两个函数对应的代码出了问题.

 

 

我的解决办法是参照服务器端调试PHP程序 这篇blog, 配置好debug环境,

然后找个入口, 直接debug服务器端对应的php函数, 这些php函数调通了.

webservices调用的异常也自然跟着修正了.

 

 

 

 

分享到:
评论
2 楼 wjason 2012-06-12  
昨天有发现了一个问题,
当我把用于连接soap的账号的语言设置改成中文的时候,soap无法成功
改回英文,便又成功了.
继续把语言改成日语, 成功
再把语言改成bulgarian(据字典说这叫保加利亚语),依然成功

我们在定制mantis的时候,平时主要注重的是英语和日语,简体中文对应和确认都不全
其他语言从来没有对应过.

问题很明显, 出现在简体中文的语言文件上面,
把问题写在这里, 希望对做类似事情的人有所帮助.
1 楼 wjason 2012-06-07  
另外的链接
http://www.mantisbt.org/bugs/api/soap/mantisconnect.php#
http://www.mantisbt.org/bugs/api/soap/mantisconnect.php?wsdl

相关推荐

Global site tag (gtag.js) - Google Analytics