OpenAI与JIRA集成POC开发求助:实现从JIRA取数并通过OpenAI分析的最小可运行方案
Hey there! Let's put together a minimal, working POC for this—since you just need it to run once, we can skip over complex frontend setup hurdles and go with a straightforward Node.js script that handles both JIRA data fetching and OpenAI analysis. Here's how to make it happen step by step:
1. Prep Your Credentials
First, grab the keys you'll need:
- JIRA: Get your API token (go to your JIRA profile > Security > Create and manage API tokens) and note your JIRA email + your JIRA domain (like
your-domain.atlassian.net). - OpenAI: Grab your API key from your OpenAI account settings.
2. Minimal Node.js Script (All-in-One)
We'll use two npm packages: axios for JIRA API calls, and openai for interacting with OpenAI. Create a new folder, set up the script, and install dependencies:
First, initialize the project and install packages:
npm init -y npm install axios openai dotenv
Then create a file jira-openai-poc.js with this code:
require('dotenv').config(); const axios = require('axios'); const { OpenAI } = require('openai'); // Initialize OpenAI client const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); // JIRA configuration const jiraConfig = { email: process.env.JIRA_EMAIL, token: process.env.JIRA_TOKEN, domain: process.env.JIRA_DOMAIN, }; // Function to fetch JIRA issues via JQL async function fetchJiraIssues(jql) { try { const response = await axios.get(`https://${jiraConfig.domain}/rest/api/3/search`, { auth: { username: jiraConfig.email, password: jiraConfig.token, }, params: { jql: jql, fields: 'summary,description,status', // Keep fields minimal for POC maxResults: 5, // Limit results to keep it simple }, }); return response.data.issues.map(issue => ({ key: issue.key, summary: issue.fields.summary, description: issue.fields.description || 'No description', status: issue.fields.status.name, })); } catch (error) { console.error('Error fetching JIRA issues:', error.response?.data || error.message); throw error; } } // Function to analyze issues with OpenAI async function analyzeWithOpenAI(issues) { try { const prompt = `Summarize the following JIRA issues and highlight the most common status and top 2 recurring themes:\n\n${JSON.stringify(issues, null, 2)}`; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: prompt }], }); return completion.choices[0].message.content; } catch (error) { console.error('Error analyzing with OpenAI:', error.message); throw error; } } // Main function to run the POC async function runPOC() { // Use a simple JQL query (adjust this to match your project/sprint if needed) const jql = 'project = "YOUR_PROJECT_KEY" AND sprint = "YOUR_SPRINT_NAME"'; console.log('Fetching JIRA issues...'); const issues = await fetchJiraIssues(jql); console.log('Fetched issues:', issues); console.log('\nAnalyzing with OpenAI...'); const analysis = await analyzeWithOpenAI(issues); console.log('\nOpenAI Analysis Result:\n', analysis); } // Run the POC runPOC();
3. Set Up Environment Variables
Create a .env file in your project folder with these values:
JIRA_EMAIL=your-jira-email@example.com JIRA_TOKEN=your-jira-api-token JIRA_DOMAIN=your-domain.atlassian.net OPENAI_API_KEY=your-openai-api-key
4. Run the Script
Just execute this command in your terminal:
node jira-openai-poc.js
Quick Notes on Your Frontend Script
If you really want to stick with the frontend form you started, you'll need to handle CORS issues (JIRA doesn't allow direct frontend calls by default). For a quick fix, you could use a proxy server, but the Node.js script above is way faster to get up and running for a minimal POC.
That's it—this will fetch a small set of JIRA issues, send them to OpenAI, and return a simple analysis. Adjust the JQL query or the OpenAI prompt to match what you want to analyze!
备注:内容来源于stack exchange,提问作者Cinthiya Singh

