Skip to content

Commit

Permalink
Arc 2643 audit log table entry (#2584)
Browse files Browse the repository at this point in the history
* chore: audit log service

* chore: audit log service

* ARC-2643 add api router to fetch audit log

* in progress

* chore: audit log service

* chore: audit log service

* chore: PR review comments

* chore: audit log service

* chore: revert unneccesary changes

* chore: revert unneccesary changes

* chore: revert unneccesary changes

* chore: work on PR commnets

* chore: pr comments work

* chore: pr comments work

* chore: pr comments work

* chore: add ff for audit log

* chore: add ff for audit log

* chore: resolve test cases failure

* chore: pr review changes

* chore: pr review changes

* chore: pr review changes

* chore: handle PR audit log entry

* ARC-2643 add backfill audit log

* ARC-2643 add tests for pull

* remove unneed

* remove backfilled in action

* fix

* ARC-2643 add build and deployment

* chore: log audit data for build update dd api call

* chore: log audit data for build update dd api call

* chore: PR coment

* chore: PR comment

* chore: PR comment

* chore: PR comment

* chore: work to log deloyment audit info

* chore: work to log deloyment audit info

* chore: work to log deloyment audit info

* chore: work to log deloyment audit info

* fix: testcases n pr comments

* chore: pr comment

* chore: increase test coverage

* fix: test cases

* chore: PR comment

---------

Co-authored-by: Gary Xue <[email protected]>
Co-authored-by: Gary Xue <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent 6545176 commit c1fb0d4
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/github/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const processDeployment = async (
const result: DeploymentsResult = await jiraClient.deployment.submit(
jiraPayload,
webhookPayload.repository.id,
webhookPayload.repository.full_name,
{
preventTransitions: false,
operationType: "NORMAL",
Expand Down
180 changes: 172 additions & 8 deletions src/jira/client/jira-client-audit-log-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { processBatchedBulkUpdateResp, processWorkflowSubmitResp, processDeploySubmitResp } from "./jira-client-audit-log-helper";
import { processBatchedBulkUpdateResp, processWorkflowSubmitResp, processDeploySubmitResp, processAuditLogsForDeploymentSubmit } from "./jira-client-audit-log-helper";
import { getLogger } from "config/logger";
import { JiraSubmitOptions } from "interfaces/jira";

Expand Down Expand Up @@ -213,6 +213,54 @@ describe("processBatchedBulkUpdateResp", () => {
}]
});
});
it("should not extract the commit as subscription id is missing", () => {
const reqRepoData = {
id: "691330555",
name: "KamaksheeSamant/react-cods-hub",
url: "https://github.com/KamaksheeSamant/react-cods-hub",
updateSequenceId: 1700453687210,
commits: [
{
hash: "e3fe8bf05f50f87c18611298e312217c4895747b",
message: "KAM-1 and KAM-2",
authorTimestamp: "2023-11-20T04:14:44Z",
displayId: "e3fe8b",
fileCount: 1,
id: "e3fe8bf05f50f87c18611298e312217c4895747b",
issueKeys: ["KAM-1", "KAM-2"],
url: "https://github.com/KamaksheeSamant/react-cods-hub/commit/e3fe8bf05f50f87c18611298e312217c4895747b",
updateSequenceId: 1700453687210
}
]
};
const response = {
status: 202,
data: {
acceptedDevinfoEntities: {
"691330555": {
branches: [],
commits: ["e3fe8bf05f50f87c18611298e312217c4895747b"],
pullRequests: []
}
},
failedDevinfoEntities: {},
unknownIssueKeys: []
}
};
const options = { preventTransitions:false, operationType: "WEBHOOK", entityAction: "COMMIT_PUSH", subscriptionId: undefined };

const result = processBatchedBulkUpdateResp({
reqRepoData,
response,
options,
logger: mockLogger
});

expect(result).toEqual({
isSuccess: true,
auditInfo:[]
});
});
it("should extract the commit with 1 issue keys linked - audit info for logging", () => {
const reqRepoData = {
id: "691330555",
Expand Down Expand Up @@ -648,6 +696,48 @@ describe("processWorkflowSubmitResp", () => {
});
});

describe("processAuditLogsForDeploymentSubmit", () => {
const mockLogger = getLogger("mock-logger");
it("should skip sending to audit log if options are undefined", () => {
// Arrange
const reqDeploymentDataArray = []; // Provide the necessary data for the test
const repoFullName = "example/repo";
const options = undefined;
mockLogger.debug = jest.fn();
const response = {
status: 202,
data: {
unknownIssueKeys: [],
acceptedDeployments: [
{
pipelineId: "deploy",
environmentId: "github-pages",
deploymentSequenceNumber: 1194529862
}
],
rejectedDeployments: [
{
pipelineId: "deploy",
environmentId: "github-pages",
deploymentSequenceNumber: 1188282781
}
]
}
};
// Act
processAuditLogsForDeploymentSubmit({
reqDeploymentDataArray,
repoFullName,
response,
options,
logger: mockLogger
});

// Assert
expect(mockLogger.debug).toHaveBeenCalled();
});
});

describe("processDeploySubmitResp", () => {
const mockLogger = getLogger("mock-logger");
it("should return isSuccess as false when status code is anything other than 202", () => {
Expand Down Expand Up @@ -694,9 +784,10 @@ describe("processDeploySubmitResp", () => {
}
};
const options: JiraSubmitOptions = { preventTransitions:false, operationType: "NORMAL", auditLogsource: "WEBHOOK", subscriptionId: 1122334455 };

const repoFullName = "react-code-hub";
const result = processDeploySubmitResp({
reqDeploymentDataArray,
repoFullName,
response,
options,
logger: mockLogger
Expand Down Expand Up @@ -750,10 +841,11 @@ describe("processDeploySubmitResp", () => {
}
};
const options: JiraSubmitOptions = { preventTransitions:false, operationType: "NORMAL", auditLogsource: "WEBHOOK", subscriptionId: 1122334455 };

const repoFullName = "react-code-hub";
const result = processDeploySubmitResp({
reqDeploymentDataArray,
response,
repoFullName,
options,
logger: mockLogger
});
Expand Down Expand Up @@ -818,10 +910,11 @@ describe("processDeploySubmitResp", () => {
}
};
const options: JiraSubmitOptions = { preventTransitions:false, operationType: "NORMAL", auditLogsource: "WEBHOOK", subscriptionId: 1122334455 };

const repoFullName = "react-code-hub";
const result = processDeploySubmitResp({
reqDeploymentDataArray,
response,
repoFullName,
options,
logger: mockLogger
});
Expand All @@ -832,7 +925,7 @@ describe("processDeploySubmitResp", () => {
{
createdAt: expect.anything(),
entityAction: "successful",
entityId: "1194529862",
entityId: "react-code-hub_1194529862",
entityType: "deployments",
issueKey: "KAM-6",
source: "WEBHOOK",
Expand Down Expand Up @@ -897,11 +990,12 @@ describe("processDeploySubmitResp", () => {
}
};
const options: JiraSubmitOptions = { preventTransitions:false, operationType: "NORMAL", auditLogsource: "WEBHOOK", subscriptionId: 1122334455 };

const repoFullName = "react-code-hub";
const result = processDeploySubmitResp({
reqDeploymentDataArray,
response,
options,
repoFullName,
logger: mockLogger
});

Expand All @@ -911,7 +1005,7 @@ describe("processDeploySubmitResp", () => {
{
createdAt: expect.anything(),
entityAction: "successful",
entityId: "1194529862",
entityId: "react-code-hub_1194529862",
entityType: "deployments",
issueKey: "KAM-6",
source: "WEBHOOK",
Expand All @@ -920,7 +1014,7 @@ describe("processDeploySubmitResp", () => {
{
createdAt: expect.anything(),
entityAction: "successful",
entityId: "1194529862",
entityId: "react-code-hub_1194529862",
entityType: "deployments",
issueKey: "KAM-5",
source: "WEBHOOK",
Expand All @@ -929,4 +1023,74 @@ describe("processDeploySubmitResp", () => {
]
});
});
it("should not extract the build as subscribtion id is missing", () => {
const reqDeploymentDataArray = [
{
schemaVersion: "1.0",
deploymentSequenceNumber: 1194529862,
updateSequenceNumber: 2835516764,
displayName: "Update manifest.json KAM-6",
url: "https://github.com/KamaksheeSamant/react-cods-hub/actions/runs/7014571070/job/19082502671",
description: "deploy",
lastUpdated: "2023-11-28T05:22:38.000Z",
state: "successful",
pipeline: {
id: "deploy",
displayName: "deploy",
url: "https://github.com/KamaksheeSamant/react-cods-hub/actions/runs/7014571070/job/19082502671"
},
environment: {
id: "github-pages",
displayName: "github-pages",
type: "unmapped"
},
associations: [
{ associationType: "issueIdOrKeys", values: ["KAM-6", "KAM-5"] },
{
associationType: "commit",
values: [
{
commitHash: "f4bc81f3ddb980ac34e4ae9acef108e34840567f",
repositoryId: "691330555"
}
]
}
]
}
];
const response = {
status: 202,
data: {
unknownIssueKeys: [],
acceptedDeployments: [
{
pipelineId: "deploy",
environmentId: "github-pages",
deploymentSequenceNumber: 1194529862
}
],
rejectedDeployments: [
{
pipelineId: "deploy",
environmentId: "github-pages",
deploymentSequenceNumber: 1188282781
}
]
}
};
const options = { preventTransitions:false, operationType: "NORMAL", auditLogsource: "WEBHOOK", subscriptionId: undefined };
const repoFullName = "react-code-hub";
const result = processDeploySubmitResp({
reqDeploymentDataArray,
response,
options,
repoFullName,
logger: mockLogger
});

expect(result).toEqual({
isSuccess: true,
auditInfo: []
});
});
});
11 changes: 6 additions & 5 deletions src/jira/client/jira-client-audit-log-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const processBatchedBulkUpdateResp = ({
export const processDeploySubmitResp = ({
reqDeploymentDataArray,
response,
repoFullName,
options,
logger
}): {
Expand All @@ -135,7 +136,7 @@ export const processDeploySubmitResp = ({
issueKeys.map((issueKey) => {
const obj: AuditInfo = {
createdAt,
entityId: reqDeploymentSqNo.toString(),
entityId: `${repoFullName}_${reqDeploymentSqNo.toString()}`,
entityType: "deployments",
issueKey,
subscriptionId: options.subscriptionId,
Expand Down Expand Up @@ -203,9 +204,7 @@ export const processWorkflowSubmitResp = ({
source: options.auditLogsource || "WEBHOOK",
entityAction: (reqBuildData.state || "").toUpperCase()
};
if (obj.subscriptionId && obj.entityId) {
auditInfo.push(obj);
}
auditInfo.push(obj);
});
}
});
Expand Down Expand Up @@ -276,8 +275,9 @@ export const processAuditLogsForWorkflowSubmit = (
};

export const processAuditLogsForDeploymentSubmit = (
{ reqDeploymentDataArray, response, options, logger }: {
{ reqDeploymentDataArray, response, options, repoFullName, logger }: {
reqDeploymentDataArray: JiraDeployment[],
repoFullName: string,
response: Response,
options: any,
logger: Logger
Expand All @@ -292,6 +292,7 @@ export const processAuditLogsForDeploymentSubmit = (
const { isSuccess, auditInfo } = processDeploySubmitResp({
reqDeploymentDataArray,
response,
repoFullName,
options: options,
logger
});
Expand Down
8 changes: 4 additions & 4 deletions src/jira/client/jira-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ describe("Test getting a jira client", () => {
jiraNock.post("/rest/deployments/0.1/bulk").reply(302, undefined, {
"Location": jiraHost + "/rest/deployments/0.1/bulk"
});

const repoFullName: string = "react-code-hub";
const response = await client.deployment.submit({
deployments: [{}]
} as JiraDeploymentBulkSubmitData, 1);
} as JiraDeploymentBulkSubmitData, 1, repoFullName);

expect(response).toEqual({
status: 200,
Expand Down Expand Up @@ -264,7 +264,7 @@ describe("Test getting a jira client", () => {
it("should truncate deployment issue keys if exceed limit", async () => {

jiraNock.post("/rest/deployments/0.1/bulk").reply(200);

const repoFullName: string = "react-code-hub";
await client.deployment.submit({
deployments: [
{
Expand Down Expand Up @@ -292,7 +292,7 @@ describe("Test getting a jira client", () => {
}]
}
]
}, 1, {
}, 1, repoFullName, {
auditLogsource: "WEBHOOK",
operationType: "NORMAL",
preventTransitions: false,
Expand Down
10 changes: 7 additions & 3 deletions src/jira/client/jira-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import { getLogger } from "config/logger";
import { jiraIssueKeyParser } from "utils/jira-utils";
import { uniq } from "lodash";
import { getCloudOrServerFromGitHubAppId } from "utils/get-cloud-or-server";
import { TransformedRepositoryId, transformRepositoryId } from "~/src/transforms/transform-repository-id";
import {
TransformedRepositoryId,
transformRepositoryId
} from "~/src/transforms/transform-repository-id";
import { getDeploymentDebugInfo } from "./jira-client-deployment-helper";
import {
processAuditLogsForDevInfoBulkUpdate,
Expand Down Expand Up @@ -84,6 +87,7 @@ export interface JiraClient {
submit: (
data: JiraDeploymentBulkSubmitData,
repositoryId: number,
repoFullName: string,
options?: JiraSubmitOptions
) => Promise<DeploymentsResult>;
},
Expand Down Expand Up @@ -426,7 +430,7 @@ export const getJiraClient = async (
}
},
deployment: {
submit: async (data: JiraDeploymentBulkSubmitData, repositoryId: number, options?: JiraSubmitOptions): Promise<DeploymentsResult> => {
submit: async (data: JiraDeploymentBulkSubmitData, repositoryId: number, repoFullName: string, options?: JiraSubmitOptions): Promise<DeploymentsResult> => {
updateIssueKeysFor(data.deployments, uniq);
if (!withinIssueKeyLimit(data.deployments)) {
logger.warn({
Expand Down Expand Up @@ -474,7 +478,7 @@ export const getJiraClient = async (
};
const reqDeploymentDataArray: JiraDeployment[] = data?.deployments || [];
if (await booleanFlag(BooleanFlags.USE_DYNAMODB_TO_PERSIST_AUDIT_LOG, jiraHost)) {
processAuditLogsForDeploymentSubmit({ reqDeploymentDataArray, response:responseData, options, logger });
processAuditLogsForDeploymentSubmit({ reqDeploymentDataArray, repoFullName, response:responseData, options, logger });
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/sync/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ describe("sync/deployments", () => {
await expect(processInstallation(mockBackfillQueueSendMessage)(data, sentry, getLogger("test"))).toResolve();
await verifyMessageSent(data);

expect(lastMockedDeploymentSubmitFn).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({
expect(lastMockedDeploymentSubmitFn).toHaveBeenCalledWith(expect.anything(), 1, "test-repo-name", expect.objectContaining({
auditLogsource: "BACKFILL",
operationType: "BACKFILL",
preventTransitions: true,
Expand Down
Loading

0 comments on commit c1fb0d4

Please sign in to comment.