All files / src/Connector JiraCreateIssueConnector.ts

96.15% Statements 25/26
75% Branches 3/4
100% Functions 4/4
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106  5x 5x   5x 5x   5x   5x   5x     5x       1x                     1x   1x 1x 1x                                                       1x               1x 1x   1x       1x 1x           1x 1x                               5x 5x 5x 5x    
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import AConnector from '@orchesty/nodejs-sdk/dist/lib/Connector/AConnector';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import { checkParams } from '@orchesty/nodejs-sdk/dist/lib/Utils/Validations';
import JiraApplication, { BUG_TYPE, ISSUE_TYPE_FROM, STORY_TYPE, TASK_TYPE } from '../JiraApplication';
 
const JIRA_CREATE_ISSUE_ENDPOINT = '/rest/api/3/issue';
 
export const NAME = 'jira-create-issue';
 
export default class JiraCreateIssueConnector extends AConnector {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: ProcessDto<IJiraIssue>): Promise<ProcessDto> {
        checkParams(
            dto.getJsonData(),
            ['description', 'summary', 'projectKey', 'issueType', 'labels'],
        );
 
        const {
            description,
            summary,
            projectKey,
            issueType,
            labels,
        } = dto.getJsonData();
 
        const application = this.getApplication<JiraApplication>();
        const applicationInstall = await this.getApplicationInstallFromProcess(dto);
        const data = {
            fields: {
                project: {
                    key: projectKey,
                },
                summary,
                description: {
                    type: 'doc',
                    version: 1,
                    content: [
                        {
                            type: 'paragraph',
                            content: [
                                {
                                    type: 'text',
                                    text: description,
                                },
                            ],
                        },
                    ],
                },
                issuetype: {
                    id: this.getIssueTypeId(applicationInstall, issueType),
                },
                labels,
            },
        };
 
        const request = await application.getRequestDto(
            dto,
            applicationInstall,
            HttpMethods.POST,
            JIRA_CREATE_ISSUE_ENDPOINT,
            JSON.stringify(data),
        );
 
        const response = await this.getSender().send(request, [201]);
        dto.setData(response.getBody());
 
        return dto;
    }
 
    private getIssueTypeId(applicationInstall: ApplicationInstall, issueType: IssueTypeEnum): number {
        const issueTypeForm = applicationInstall.getSettings()[ISSUE_TYPE_FROM];
        const types = [
            issueTypeForm?.[BUG_TYPE],
            issueTypeForm?.[TASK_TYPE],
            issueTypeForm?.[STORY_TYPE],
        ];
 
        Eif (types[issueType]) {
            return types[issueType];
        }
 
        throw new Error(`Connector [${this.getName()}] doesn't have correct issueType.`);
    }
 
}
 
export interface IJiraIssue {
    description: string;
    summary: string;
    projectKey: string;
    issueType: IssueTypeEnum;
    labels: string[];
}
 
export enum IssueTypeEnum {
    BUG = 0,
    TASK = 1,
    STORY = 2,
}