JavaからRedmineの時間記録を行うソース
カッとなって書いた。
HTTPアクセスにはApache「HttpComponents」のHttpClient4.0-beta2を利用。
(元々、Jakarta Commons HTTP Clientだったヤツですね)
public void recordTime() throws IOException { DefaultHttpClient client = new DefaultHttpClient(); // ログイン処理 HttpPost post = new HttpPost("http://localhost:3000/login"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "admin")); nvps.add(new BasicNameValuePair("password", "admin")); post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = client.execute(post); response.getEntity().consumeContent(); // 時間記録 post = new HttpPost("http://localhost:3000/projects/test/time_entries/new"); nvps = new ArrayList<NameValuePair>(); // チケット# nvps.add(new BasicNameValuePair("time_entry[issue_id]", "1")); // 日付 nvps.add(new BasicNameValuePair("time_entry[spent_on]", "2009-02-01")); // 経過時間 nvps.add(new BasicNameValuePair("time_entry[hours]", "4")); // コメント nvps.add(new BasicNameValuePair("time_entry[comments]", "テスト")); // 活動 nvps.add(new BasicNameValuePair("time_entry[activity_id]", "9")); post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); response = client.execute(post); response.getEntity().consumeContent(); }
2009/02/01時点のtrunkから取ってきたRedmine(0.8.0? 0.8.1相当?)で動作確認済み。
RedmineってURLや、メソッド処理が素直なので、こんな叩き方もできるんですね。
もちろん、RESTfulなAPIで叩きたいところですが。