|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +const https = require('https'); |
| 3 | + |
| 4 | +function createJiraTicket() { |
| 5 | + const options = { |
| 6 | + method: 'POST', |
| 7 | + host: process.env.JIRA_BASE_URL, |
| 8 | + port: process.env.JIRA_PORT, |
| 9 | + path: '/jira/rest/api/2/issue/', |
| 10 | + headers: { |
| 11 | + Authorization: `Bearer ${process.env.JIRA_API_TOKEN}`, |
| 12 | + Accept: 'application/json', |
| 13 | + 'Content-Type': 'application/json', |
| 14 | + }, |
| 15 | + }; |
| 16 | + const formattedTitle = `GraphKB Client ${process.env.PR_TITLE}`; |
| 17 | + let formattedDesc = ''; |
| 18 | + |
| 19 | + const body = process.env.PR_DESCRIPTION; |
| 20 | + const lines = body.split('\n').map((l) => l.trim()).filter(Boolean); |
| 21 | + |
| 22 | + let section = ''; |
| 23 | + const tables = {}; |
| 24 | + |
| 25 | + for (let line of lines) { |
| 26 | + if (line.startsWith('###')) { |
| 27 | + line = ''; |
| 28 | + } else if (line.startsWith('**')) { |
| 29 | + section = line.replace(/\*\*/g, '').trim(); |
| 30 | + tables[section] = []; |
| 31 | + } else if (line.startsWith('[[')) { |
| 32 | + const match = line.match(/\[\[(?<title>[A-Z]+-\d+)\]\([^)]+\)\]\s*-\s*(?<description>.+)/); |
| 33 | + |
| 34 | + if (match?.groups) { |
| 35 | + const { title, description } = match.groups; |
| 36 | + tables[section].push({ title, description }); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + for (const [sectionName, items] of Object.entries(tables)) { |
| 42 | + formattedDesc += `| *${sectionName}* | *Need Review* | *Status* | *Reviewer* | *Description* |\n`; |
| 43 | + |
| 44 | + for (const { title, description } of items) { |
| 45 | + formattedDesc += `| ${title} | | | | ${description} |\n`; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const issueData = JSON.stringify({ |
| 50 | + fields: { |
| 51 | + project: { |
| 52 | + key: process.env.JIRA_PROJECT_NAME, |
| 53 | + }, |
| 54 | + summary: formattedTitle, |
| 55 | + description: formattedDesc, |
| 56 | + issuetype: { |
| 57 | + name: process.env.JIRA_ISSUE_TYPE, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + console.log('Writing issue: \n', issueData); |
| 63 | + |
| 64 | + const req = https.request(options, (res) => { |
| 65 | + res.setEncoding('utf8'); |
| 66 | + res.on('data', (body) => { |
| 67 | + console.log('Body:', body); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + req.on('error', (e) => { |
| 72 | + console.error('problem with request:', e.message); |
| 73 | + }); |
| 74 | + |
| 75 | + req.write(issueData); |
| 76 | + req.end(); |
| 77 | +} |
| 78 | + |
| 79 | +createJiraTicket(); |
0 commit comments