Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(feedback): fixed feedback backend plugin crashing #1809

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions plugins/feedback-backend/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export interface Config {
* The hostname or URL of the JIRA organization.
* @visibility frontend
*/
host: string;
host?: string;
/**
* The access token for authenticating with JIRA.
* @visibility secret
*/
token: string;
token?: string;
/**
* what type of jira instance are you using, CLOUD/SERVER
*/
hostType: string;
hostType?: string;
}>;

/**
Expand All @@ -36,12 +36,12 @@ export interface Config {
/**
* The SMTP server's hostname or IP address.
*/
host: string;
host?: string;

/**
* The port number to use for the SMTP server.
*/
port: number;
port?: number;

/**
* Optional authentication settings for the SMTP server.
Expand All @@ -56,7 +56,7 @@ export interface Config {
* The password to use for SMTP server authentication.
* @visibility secret
*/
pass?: string;
password?: string;
};

/**
Expand Down
8 changes: 4 additions & 4 deletions plugins/feedback-backend/src/service/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import { readFileSync } from 'fs';

export class NodeMailer {
private readonly transportConfig: Transporter;
private readonly from: string;
private readonly from: string | undefined;

constructor(
config: Config,
private logger: LoggerService,
) {
const useSecure: boolean = config.getBoolean(
const useSecure = config.getOptionalBoolean(
'feedback.integrations.email.secure',
);
const caCertPath = config.getOptionalString(
'feedback.integrations.email.caCert',
);
const customCACert = caCertPath ? readFileSync(caCertPath) : undefined;

this.from = config.getString('feedback.integrations.email.from');
this.from = config.getOptionalString('feedback.integrations.email.from');
this.transportConfig = createTransport({
host: config.getString('feedback.integrations.email.host'),
host: config.getOptionalString('feedback.integrations.email.host'),
port: config.getOptionalNumber('feedback.integrations.email.port') ?? 587,
auth: {
user: config.getOptionalString('feedback.integrations.email.auth.user'),
Expand Down
32 changes: 20 additions & 12 deletions plugins/feedback-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@backstage/backend-common';
import { AuthService, LoggerService } from '@backstage/backend-plugin-api';
import { CatalogClient } from '@backstage/catalog-client';
import { Entity } from '@backstage/catalog-model';
import { Entity, UserEntityV1alpha1 } from '@backstage/catalog-model';
import { Config } from '@backstage/config';

import express from 'express';
Expand Down Expand Up @@ -38,6 +38,7 @@ export async function createRouter(
const catalogClient = new CatalogClient({ discoveryApi: discovery });

router.use(express.json());
logger.info('Feedback backend plugin is running');

router.post('/', (req, res) => {
(async () => {
Expand Down Expand Up @@ -108,31 +109,38 @@ export async function createRouter(
const type = annotations['feedback/type'];
const replyTo = annotations['feedback/email-to'];
const reporterEmail = (
(await catalogClient.getEntityByRef(reqData.createdBy!, { token }))
?.spec?.profile as { email: string }
).email;
(await catalogClient.getEntityByRef(reqData.createdBy!, {
token,
})) as UserEntityV1alpha1
).spec.profile?.email;
const appTitle = config.getString('app.title');

if (
type.toUpperCase() === 'JIRA' &&
!reqData.tag?.match(/(Excellent|Good)/g)
) {
let host = annotations['feedback/host'];
let serviceConfig: Config;
// if host is undefined then
// use the first host from config
const serviceConfig =
config
.getConfigArray('feedback.integrations.jira')
.find(hostConfig => host === hostConfig.getString('host')) ??
config.getConfigArray('feedback.integrations.jira')[0];
try {
serviceConfig =
config
.getConfigArray('feedback.integrations.jira')
.find(hostConfig => host === hostConfig.getString('host')) ??
config.getConfigArray('feedback.integrations.jira')[0];
} catch {
return logger.error('Jira integeration not found');
}
host = serviceConfig.getString('host');
const authToken = serviceConfig.getString('token');
const hostType = serviceConfig.getOptionalString('hostType');

const projectKey = entityRef.metadata.annotations['jira/project-key'];
const jiraService = new JiraApiService(host, authToken, hostType);
const jiraUsername =
await jiraService.getJiraUsernameByEmail(reporterEmail);
const jiraUsername = reporterEmail
? await jiraService.getJiraUsernameByEmail(reporterEmail)
invincibleJai marked this conversation as resolved.
Show resolved Hide resolved
: undefined;

// if jira id is not there for reporter, add reporter email in description
const jiraDescription = reqData.description!.concat(
Expand All @@ -159,7 +167,7 @@ export async function createRouter(

if (type.toUpperCase() === 'MAIL' || replyTo) {
mailer.sendMail({
to: reporterEmail,
to: reporterEmail ?? replyTo,
replyTo: replyTo,
subject: `${reqData.tag} - ${feedbackType} reported for ${
reqData.projectId?.split('/')[1]
Expand Down
Loading