使用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調用的異常也自然跟着修正了.

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章