0
1

More than 1 year has passed since last update.

RedmineのチケットにCHATGPTを実装(2)

Last updated at Posted at 2023-05-27

前回の検討では、いくつか不具合があった
・Fieldの更新でAPIをコール → 動作が分からない
・プロンプトの入力 → 指定フィールドのみで汎用性がない
・OPENAIトークンをソースに埋め込みたくない 
・他のissueフィールドもプロンプトの指示に使いたい

変更内容

redmine-view-customize-scriptsで、CHATGPTボタンを作って、別のAPIポイントで処理するようにした。

js.js
	
$(function() {
  // Note: Change the ID according to the custom field you want to target
  const chatField = $('#issue_custom_field_values_11');
  const chatButton = $('<button>').text('Click me!');
  const url = "http://127.0.0.1:8000/redmine";
  const form = document.getElementById("issue-form");
  
  chatButton.on('click', async (event) => {
      event.preventDefault(); // Stop the default form submission behavior

      try {
        const formData = new FormData(form);
        
        const response = await fetch(url, { method: "POST", body: formData });
        const res = await response.json();

        // Check for errors before accessing properties
        if ('text' in res) {
          chatField.val(res.text);
        } else {
          console.log('Error: response data not in expected format');
        }
      } catch (error) {
        console.log(error);
      }
  });

  $('#issue_custom_field_values_11').parent().append(chatButton); // Add the button to the "attributes" section of the ticket page
});

エンドポイント

py.py
@router.post("/redmine")
async def post_handler(request: Request):
    form = await request.form()

    for field in form:
        value = form[field]
        print(f"{field}: {value}")

    return {"text":"api called"}

こんな感じでISSUEフィールドを全体でパース出来ている
→ 後はPythonで好きな感じで、やりたいことを書けばいい。

INFO:     127.0.0.1:61906 - "POST /redmine HTTP/1.1" 200 OK
utf8: ✓
_method: patch
authenticity_token: mVVFSNv/1a4aBy29XRZIpEDT5k1u2+o2DkFXjub8XT1kha1D3V8BgLeSHTLYycvzqSz9P4FWqpxFFJYgxVHufQ==
form_update_triggered_by:
issue[is_private]: 0
issue[tracker_id]: 1
issue[subject]: CHATGP組み込み
issue[description]:
issue[status_id]: 1
was_default_status: 1
issue[priority_id]: 3
issue[assigned_to_id]:
issue[category_id]:
issue[parent_issue_id]: 179
issue[start_date]: 2023-05-27
issue[due_date]:
issue[estimated_hours]:
issue[done_ratio]: 0
issue[custom_field_values][11]: CHATGP組み込み、例を作って
issue[custom_field_values][13]:
time_entry[hours]:
time_entry[activity_id]:
time_entry[comments]:
time_entry[custom_field_values][5]: 1
time_entry[custom_field_values][14]:
time_entry[custom_field_values][15]:
issue[notes]:
issue[private_notes]: 0
attachments[dummy][file]: <starlette.datastructures.UploadFile object at 0x000001D6FB7121D0>
issue[lock_version]: 3
last_journal_id:
next_issue_id: 189
issue_position: 1
issue_count: 40

スクショ

image.png

結論

下記の検討はやーめた!!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1