|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import * as core from '@actions/core'; |
| 16 | +import request from 'request'; |
| 17 | +import { mergeWith, isArray } from 'lodash'; |
| 18 | + |
| 19 | +export interface JiraIssue { |
| 20 | + fields: { |
| 21 | + project: { key: string }; |
| 22 | + summary: string; |
| 23 | + description?: string; |
| 24 | + issuetype?: { name: string }; |
| 25 | + assignee?: { name: string }; |
| 26 | + labels?: string[]; |
| 27 | + components?: { name: string }[]; |
| 28 | + [key: string]: any; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +function buildRequestBody(projectKey: string, summary: string, description: string, issuetype: string, labels: string[], components: string[], assignee: string, extraData?: { [key: string]: any }): JiraIssue { |
| 33 | + const body: JiraIssue = { fields: { project: { key: projectKey }, summary: summary } }; |
| 34 | + if (description != "") { |
| 35 | + body.fields.description = description; |
| 36 | + } |
| 37 | + if (issuetype != "") { |
| 38 | + body.fields.issuetype = { name: issuetype }; |
| 39 | + } |
| 40 | + if (assignee != "") { |
| 41 | + body.fields.assignee = { name: assignee }; |
| 42 | + } |
| 43 | + if (labels.length != 0) { |
| 44 | + body.fields.labels = labels; |
| 45 | + } |
| 46 | + if (components.length != 0) { |
| 47 | + body.fields.components = components.map(value => { |
| 48 | + return { name: value }; |
| 49 | + }); |
| 50 | + } |
| 51 | + mergeWith(body, extraData, (dst, src) => { |
| 52 | + if (isArray(dst)) { |
| 53 | + return dst.concat(src); |
| 54 | + } |
| 55 | + }); |
| 56 | + return body; |
| 57 | +} |
| 58 | + |
| 59 | +async function createJiraIssue(token: string, apiBase: string, projectKey: string, summary: string, description: string, issuetype: string, labels: string[], components: string[], assignee: string, extraData?: { [key: string]: any }): Promise<{ response: request.Response, responseBody: any }> { |
| 60 | + const body = buildRequestBody(projectKey, summary, description, issuetype, labels, components, assignee, extraData); |
| 61 | + |
| 62 | + return await new Promise((resolve, reject) => { |
| 63 | + request({ |
| 64 | + url: `${apiBase}/rest/api/2/issue`, |
| 65 | + method: "POST", |
| 66 | + headers: { |
| 67 | + "Content-Type": "application/json", |
| 68 | + Authorization: "Bearer " + token, |
| 69 | + }, |
| 70 | + body: body, |
| 71 | + json: true |
| 72 | + }, (err: any, response: request.Response, responseBody: any) => { |
| 73 | + if (err != null) { |
| 74 | + reject(err); |
| 75 | + return; |
| 76 | + } |
| 77 | + if (response.statusCode >= 400) { |
| 78 | + reject(new Error(response.statusCode + " " + response.statusMessage + "\n" + JSON.stringify(responseBody))); |
| 79 | + return; |
| 80 | + } |
| 81 | + resolve({ |
| 82 | + response, responseBody |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +export async function main() { |
| 89 | + try { |
| 90 | + const token = core.getInput('token'); |
| 91 | + const projectKey = core.getInput('project-key'); |
| 92 | + const summary = core.getInput('summary'); |
| 93 | + const description = core.getInput('description'); |
| 94 | + const issuetype = core.getInput('issuetype'); |
| 95 | + const labels = core.getInput('labels'); |
| 96 | + const components = core.getInput('components'); |
| 97 | + const assignee = core.getInput('assignee'); |
| 98 | + const extraData = core.getInput('extra-data'); |
| 99 | + const apiBase = core.getInput('api-base'); |
| 100 | + const labelList = labels.split(",").filter(value => value != ""); |
| 101 | + const componentList = components.split(",").filter(value => value != ""); |
| 102 | + |
| 103 | + let extraDataObject: { [key: string]: any } | undefined = undefined; |
| 104 | + if (extraData != "") { |
| 105 | + try { |
| 106 | + extraDataObject = JSON.parse(extraData); |
| 107 | + } catch (error) { |
| 108 | + throw new Error("Error parsing extra-data: " + error); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const { responseBody } = await createJiraIssue(token, apiBase, projectKey, summary, description, issuetype, labelList, componentList, assignee, extraDataObject); |
| 113 | + |
| 114 | + if (!responseBody.key) { |
| 115 | + throw new Error("No issue key found in response: " + JSON.stringify(responseBody)); |
| 116 | + } |
| 117 | + |
| 118 | + core.setOutput("issue-key", responseBody.key); |
| 119 | + } catch (error) { |
| 120 | + if (error instanceof Error) { |
| 121 | + core.setFailed(error); |
| 122 | + } else { |
| 123 | + core.setFailed(JSON.stringify(error)); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments