Skip to content
Draft
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 e2e/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1115,9 +1115,9 @@ __metadata:
linkType: hard

"source-map-js@npm:^1.2.1":
version: 1.2.1
resolution: "source-map-js@npm:1.2.1"
checksum: 10c0/7bda1fc4c197e3c6ff17de1b8b2c20e60af81b63a52cb32ec5a5d67a20a7d42651e2cb34ebe93833c5a2a084377e17455854fee3e21e7925c64a51b6a52b0faf
version: 1.2.2
resolution: "source-map-js@npm:1.2.2"
checksum: 10c0/d5d6dec62266834f83dd4f8a2606a196fb1a006ca5d53b0ef80ee513b7eba2f4e2ae241f4bc129ae7e9b80dea24f6e3d92b446b7e6e96be7653cab8888168a94
languageName: node
linkType: hard

Expand Down Expand Up @@ -1336,9 +1336,9 @@ __metadata:
linkType: hard

"undici@npm:^8.4.1":
version: 8.10.0
resolution: "undici@npm:8.10.0"
checksum: 10c0/37ae2b1db8f65c3a003504e2040e96887b99f9db16ba07dcf49c37ed8b7f8c72f4452f2d46f52f7c9e49518fe8325e3b5e7e02ac3a2424886bd67d4b62a0ecab
version: 8.11.2
resolution: "undici@npm:8.11.2"
checksum: 10c0/106f6991178fb7562fd90cdec7993c5f807a9428682306091f0c4b27fc474ec291f868e29972c6252988bf061ad193df943b3b99e775be157a28d92bbb95699d
languageName: node
linkType: hard

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@aws-sdk/client-cloudwatch-logs": "^3.1107.0",
"@aws-sdk/client-lambda": "^3.1107.0",
"@datadog/datadog-ci-base": "^5.22.0",
"simple-git": "^3.36.0"
"simple-git": "^4.0.2"
},
"peerDependencies": {
"serverless": "4.x || 3.x || 2.x || 1.x"
Expand All @@ -44,6 +44,9 @@
"js-yaml": "^4.3.2",
"lodash": "^4.18.1",
"brace-expansion": "^5.0.9",
"undici": "^7.28.0"
"undici": "^7.29.1",
"simple-git": "^4.0.2",
"basic-ftp": "^6.2.2",
"oxfmt/tinypool": "2.1.2"
}
}
75 changes: 75 additions & 0 deletions src/git.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { getGitCommitInfo } from "@datadog/datadog-ci-base/commands/git-metadata/library";
import { newSimpleGit } from "./git";

describe("Git integration", () => {
let temporaryDirectory: string;
let repositoryDirectory: string;
let nestedDirectory: string;
let commitHash: string;

beforeEach(() => {
temporaryDirectory = realpathSync(mkdtempSync(join(tmpdir(), "datadog-git-")));
repositoryDirectory = join(temporaryDirectory, "repository");
nestedDirectory = join(repositoryDirectory, "nested");
mkdirSync(nestedDirectory, { recursive: true });

// Datadog may add safe.directory in CI. simple-git filters GIT_CONFIG_GLOBAL,
// so isolate the home directories as well to contain global Git config writes.
vi.stubEnv("HOME", temporaryDirectory);
vi.stubEnv("XDG_CONFIG_HOME", join(temporaryDirectory, "config"));
vi.stubEnv("GIT_CONFIG_GLOBAL", join(temporaryDirectory, "gitconfig"));
vi.stubEnv("GIT_CONFIG_NOSYSTEM", "1");

const git = (...args: string[]) => execFileSync("git", args, { cwd: repositoryDirectory, encoding: "utf8" });
git("init", "--quiet", "--initial-branch=main");
git("config", "user.name", "Git Integration Test");
git("config", "user.email", "git-test@example.com");
writeFileSync(join(repositoryDirectory, "root.ts"), "export const root = true;\n");
writeFileSync(join(nestedDirectory, "nested.ts"), "export const nested = true;\n");
git("add", ".");
git("commit", "--quiet", "--no-gpg-sign", "-m", "Initial test commit");
git("remote", "add", "origin", "https://github.com/example/security-upgrade.git");
commitHash = git("rev-parse", "HEAD").trim();
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
rmSync(temporaryDirectory, { recursive: true, force: true });
});

it("detects a Git repository and reads its current commit", async () => {
vi.spyOn(process, "cwd").mockReturnValue(repositoryDirectory);

const git = await newSimpleGit();

expect(git).toBeDefined();
expect(await git!.checkIsRepo()).toBe(true);
expect(await git!.revparse("HEAD")).toBe(commitHash);
});

it("uses the repository root when called from a nested directory", async () => {
vi.spyOn(process, "cwd").mockReturnValue(nestedDirectory);

const git = await newSimpleGit();

expect(git).toBeDefined();
expect(await git!.raw("ls-files")).toBe("nested/nested.ts\nroot.ts\n");
});

it("returns undefined outside a Git repository", async () => {
vi.spyOn(process, "cwd").mockReturnValue(temporaryDirectory);

expect(await newSimpleGit()).toBeUndefined();
});

it("reads Datadog commit metadata from a nested directory", async () => {
vi.spyOn(process, "cwd").mockReturnValue(nestedDirectory);

expect(await getGitCommitInfo()).toEqual(["github.com/example/security-upgrade.git", commitHash]);
});
});
8 changes: 4 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as simpleGit from "simple-git";
import { simpleGit, type SimpleGit } from "simple-git";

// Returns a configured SimpleGit.
export const newSimpleGit = async (): Promise<simpleGit.SimpleGit | undefined> => {
export const newSimpleGit = async (): Promise<SimpleGit | undefined> => {
const options = {
baseDir: process.cwd(),
binary: "git",
Expand All @@ -10,12 +10,12 @@ export const newSimpleGit = async (): Promise<simpleGit.SimpleGit | undefined> =
try {
// Attempt to set the baseDir to the root of the repository so the 'git ls-files' command
// returns the tracked files paths relative to the root of the repository.
const git = simpleGit.gitP(options);
const git = simpleGit(options);
const root = await git.revparse("--show-toplevel");
options.baseDir = root;
} catch {
return undefined;
}

return simpleGit.gitP(options);
return simpleGit(options);
};
Loading
Loading