Home Jira to ChatGPT using Node.js
Post
Cancel

Jira to ChatGPT using Node.js

Overview

I used ChatGPT to create a node.js app that reads issues from the Jira API, uses the issue description as a prompt to the ChatGPT API, and posts it to that issue’s comments.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const axios = require('axios');
require('dotenv').config()

async function main() {
  const jiraApiKey = process.env.jiraApiKey;
  const jiraEmail = process.env.jiraEmail;
  const jiraUrl = process.env.jiraUrl;
  const jiraEmailApiKey = new Buffer.from(`${jiraEmail}:${jiraApiKey}`).toString('base64');
  const jiraProjectName = process.env.jiraProjectName;
  const openApiKey = process.env.openApiKey;
  try {
    const jiraResponse = await axios.get(`${jiraUrl}rest/api/2/search`, {
      params: {
        jql: `project=${jiraProjectName}`,
        fields: 'summary,description'
      },
      headers: {
        'Authorization': 'Basic ' + jiraEmailApiKey
      }
    });
    const issues = jiraResponse.data.issues;
    for (const issue of issues) {
      const prompt = issue.fields.summary + '. ' + issue.fields.description + '. Give me a code example if possible.';
      console.log(`prompt = ${prompt}`);
      const chatGptResponse = await axios.post('https://api.openai.com/v1/engines/davinci/completions', {
        prompt: prompt,
        temperature: 0.8,
        max_tokens: 2000
      }, {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + openApiKey
        }
      });
      const comment = chatGptResponse.data.choices[0].text;
      const commentResponse = await axios.post(`${jiraUrl}rest/api/2/issue/${issue.key}/comment`, {
        body: comment
      }, {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + jiraEmailApiKey
        }
      });
    }
    console.log('All done!');
  } catch (error) {
    console.error(error);
  }
}
main();
This post is licensed under CC BY 4.0 by the author.
Contents