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

feat: webhook UI #362

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "0.5.8",
"version": "0.5.8-beta-3",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down
21 changes: 20 additions & 1 deletion src/Common/Common.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import moment from 'moment'
import { RuntimeParamsAPIResponseType, RuntimeParamsListItemType } from '@Shared/types'
import { getIsManualApprovalSpecific, sanitizeUserApprovalConfig, stringComparatorBySortOrder } from '@Shared/Helpers'
import { get, post } from './Api'
import { ROUTES } from './Constants'
import { GitProviderType, ROUTES } from './Constants'
import { getUrlWithSearchParams, sortCallback } from './Helper'
import {
TeamList,
Expand Down Expand Up @@ -511,3 +511,22 @@ export function getWebhookEventsForEventId(eventId: string | number) {
const URL = `${ROUTES.GIT_HOST_EVENT}/${eventId}`
return get(URL)
}

export const getGitBranchUrl = (gitUrl: string, branchName: string): string | null => {
if (!gitUrl) return null

const trimmedGitUrl = gitUrl.trim().replace(/\/$/, '') // Remove any trailing slash

switch (true) {
RohitRaj011 marked this conversation as resolved.
Show resolved Hide resolved
case trimmedGitUrl.includes(GitProviderType.GITLAB):
return `${trimmedGitUrl}/-/tree/${branchName}`
case trimmedGitUrl.includes(GitProviderType.GITHUB):
return `${trimmedGitUrl}/tree/${branchName}`
case trimmedGitUrl.includes(GitProviderType.BITBUCKET):
return `${trimmedGitUrl}/branch/${branchName}`
case trimmedGitUrl.includes(GitProviderType.AZURE):
return `${trimmedGitUrl}/src/branch/${branchName}`
default:
return null
}
}
17 changes: 17 additions & 0 deletions src/Common/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,20 @@ export const VULNERABILITIES_SORT_PRIORITY = {

// TODO: might not work need to verify
export const IS_PLATFORM_MAC_OS = window.navigator.userAgent.toUpperCase().includes('MAC')

/**
* Git provider types
*/

export enum GitProviderType {
GITHUB = 'github',
GITLAB = 'gitlab',
BITBUCKET = 'bitbucket',
AZURE = 'azure',
GITEA = 'gitea',
}

/**
* Formats the schema removing any irregularity in the existing schema
*/
export const getFormattedSchema = (schema?: string) => JSON.stringify(JSON.parse(schema ?? '{}'), null, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const CiPipelineSourceConfig = ({
</>
)}
{baseText && (
<span className="cursor" style={{ borderBottom: '1px solid #3b444c' }}>
<span className="cursor dc__border-dashed--n3-bottom fw-6 lh-20 fs-13">
{baseText}
</span>
)}
Expand Down
268 changes: 109 additions & 159 deletions src/Shared/Components/GitCommitInfoGeneric/GitCommitInfoGeneric.tsx

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Shared/Components/KeyValueTable/KeyValueTable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
gap: 1px;

&.two-columns-top-row {
grid-template-columns: 20% 1fr;
grid-template-columns: 30% 1fr;
}

&.two-columns {
grid-template-columns: 20% 1fr;
grid-template-columns: 30% 1fr;

.key-value-table__row:last-of-type {
.key-value-table__cell:first-child,
Expand All @@ -41,7 +41,7 @@
}

&.three-columns {
grid-template-columns: 20% 1fr 32px;
grid-template-columns: 30% 1fr 32px;

.key-value-table__row:last-of-type {
.key-value-table__cell:first-child,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

import { getWebhookDate } from '@Shared/Helpers'
import { MaterialHistoryType } from '@Shared/Services'
import { useMemo } from 'react'
import { SourceTypeMap } from '../../../Common'
import { GitCommitInfoGeneric } from '../GitCommitInfoGeneric'
import { MaterialHistoryProps } from './types'
Expand All @@ -32,22 +35,23 @@ const MaterialHistory = ({
}
}

const getMaterialHistoryMapWithTime = () => {
const historyTimeMap = {}

material.history?.forEach((history) => {
const newDate = history.date.substring(0, 16)

if (!historyTimeMap[newDate]) {
historyTimeMap[newDate] = []
}
historyTimeMap[newDate].push(history)
})
const materialHistoryMapWithTime = useMemo(
() =>
material.history.reduce<Record<string, MaterialHistoryType[]>>((acc, historyElem: MaterialHistoryType) => {
const isWebhook = material.type === SourceTypeMap.WEBHOOK
const newDate = isWebhook
? getWebhookDate(material.type, historyElem).substring(0, 16)
: historyElem.date.substring(0, 16)
if (!acc[newDate]) {
acc[newDate] = []
}
acc[newDate].push(historyElem)
return acc
}, {}),
[material.history, material.type],
)

return historyTimeMap
}
// Retrieve the history map
const materialHistoryMapWithTime = getMaterialHistoryMapWithTime()
// Retrieve the keys of the history map
const dateKeys = Object.keys(materialHistoryMapWithTime)

Expand All @@ -58,8 +62,8 @@ const MaterialHistory = ({
{dateKeys.map((date) => {
const historyList = materialHistoryMapWithTime[date]
return (
<>
{!isCommitInfoModal && material.type !== SourceTypeMap.WEBHOOK && (
<div key={date} className="flexbox-col dc__gap-12 py-12 px-16">
{!isCommitInfoModal && (
<div className="flex left dc__gap-8">
<span className="fs-12 lh-18 cn-7 fw-6 w-130">{date}</span>
<div className="h-1 bcn-2 w-100" />
Expand Down Expand Up @@ -87,13 +91,12 @@ const MaterialHistory = ({
materialSourceType={material.type}
selectedCommitInfo={selectCommit}
materialSourceValue={material.value}
canTriggerBuild={!history.excluded}
isExcluded={history.excluded}
/>
</div>
)
})}
</>
</div>
)
})}
</>
Expand Down
40 changes: 40 additions & 0 deletions src/Shared/Helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useEffect, useRef, useState, ReactElement } from 'react'
import Tippy from '@tippyjs/react'
import { Pair } from 'yaml'
import moment from 'moment'
import { MaterialHistoryType } from '@Shared/Services/app.types'
import {
handleUTCTime,
ManualApprovalType,
Expand All @@ -30,6 +31,8 @@ import {
PATTERNS,
ZERO_TIME_STRING,
noop,
SourceTypeMap,
DATE_TIME_FORMATS,
} from '../Common'
import {
AggregationKeys,
Expand Down Expand Up @@ -852,3 +855,40 @@ export const groupArrayByObjectKey = <T extends Record<string, any>, K extends k
},
{} as Record<string, T[]>,
)

/**
* @description - Function to get the lower case object
* @param input - The input object
* @returns Record<string, any>
*/
export const getLowerCaseObject = (input): Record<string, any> => {
if (!input || typeof input !== 'object') {
return input
}
return Object.keys(input).reduce((acc, key) => {
const modifiedKey = key.toLowerCase()
const value = input[key]
if (value && typeof value === 'object') {
acc[modifiedKey] = getLowerCaseObject(value)
} else {
acc[modifiedKey] = value
}
return acc
}, {})
}

/**
* @description - Function to get the webhook date
* @param materialSourceType - The type of material source (e.g., WEBHOOK)
* @param history - The history object containing commit information
* @returns - Formatted webhook date if available, otherwise an empty string
*/

export const getWebhookDate = (materialSourceType: string, history: MaterialHistoryType): string => {
const lowerCaseCommitInfo = getLowerCaseObject(history)
const isWebhook = materialSourceType === SourceTypeMap.WEBHOOK || lowerCaseCommitInfo?.webhookdata?.id !== 0
const webhookData = isWebhook ? lowerCaseCommitInfo.webhookdata : {}

const _moment = moment(webhookData.data.date, 'YYYY-MM-DDTHH:mm:ssZ')
return _moment.isValid() ? _moment.format(DATE_TIME_FORMATS.TWELVE_HOURS_FORMAT) : webhookData.data.date
}
3 changes: 2 additions & 1 deletion src/Shared/Services/app.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface MaterialHistoryDTO {
WebhookData: WebhookDataType
}

interface MaterialHistoryType {
export interface MaterialHistoryType {
commitURL: string
commit: MaterialHistoryDTO['Commit']
author: MaterialHistoryDTO['Author']
Expand Down Expand Up @@ -84,6 +84,7 @@ export interface CIMaterialType
gitURL: CIMaterialDTO['url']
history: MaterialHistoryType[]
isSelected: boolean
gitMaterialUrl?: string
}

interface ImageCommentDTO {
Expand Down