Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
fix: netlify commit hash
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Dec 6, 2023
1 parent 12cd2c7 commit 8c74da7
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
5 changes: 2 additions & 3 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[Settings]
ID = "d2668848-c2a6-411f-b884-004faee1942f"
[build]
functions = "./.netlify/functions"
# Specify is-core-module/package.json as an external dependency
ignore = "is-core-module/package.json"
functions = "./.netlify/functions"
command = "echo 'export const COMMIT_HASH = \"${COMMIT_REF}\";' > src/commit-hash.ts && yarn tsc"
6 changes: 2 additions & 4 deletions src/adapters/supabase/helpers/tables/logs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// This is disabled because logs should be able to log any type of data
// Normally this is forbidden
// TODO: break this apart into smaller files.

import { SupabaseClient } from "@supabase/supabase-js";
import { execSync } from "child_process";
import { Context as ProbotContext } from "probot";
import Runtime from "../../../../bindings/bot-runtime";
import { env } from "../../../../bindings/env";
import { COMMIT_HASH } from "../../../../commit-hash";
import { Database } from "../../types/database";

import { LogLevel, PrettyLogs } from "../pretty-logs";
Expand Down Expand Up @@ -107,7 +105,7 @@ export class Logs {
}
}

const gitCommit = env.COMMIT_REF || execSync("git rev-parse --short HEAD").toString().trim();
const gitCommit = COMMIT_HASH?.substring(0, 7) ?? null;
metadata.revision = gitCommit;

return metadata;
Expand Down
8 changes: 8 additions & 0 deletions src/commit-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { execSync } from "child_process";
export let COMMIT_HASH: null | string = null; // "0000000000000000000000000000000000000000";

try {
COMMIT_HASH = execSync("git rev-parse --short HEAD").toString().trim();
} catch (e) {
// netlify has no git binary
}
5 changes: 2 additions & 3 deletions src/handlers/shared/structured-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { execSync } from "child_process";
import { env } from "../../bindings/env";
import { COMMIT_HASH } from "../../commit-hash";

function createStructuredMetadata(className: string, metadata: unknown) {
const jsonString = JSON.stringify(metadata, null, 2);
const stackLine = new Error().stack?.split("\n")[2] ?? "";
const caller = stackLine.match(/at (\S+)/)?.[1] ?? "";
const revision = env.COMMIT_REF || execSync("git rev-parse --short HEAD").toString().trim();
const revision = COMMIT_HASH?.substring(0, 7) ?? null;
return [`<!-- Ubiquity - ${className} - ${caller} - ${revision}`, jsonString, "-->"].join("\n");
}

Expand Down
5 changes: 2 additions & 3 deletions src/tests/commands-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { describe } from "@jest/globals";
import { execSync } from "child_process";
import "dotenv/config";
import { Octokit } from "octokit";
import { Server } from "probot";
import { env } from "../bindings/env";
import { COMMIT_HASH } from "../commit-hash";
import { afterAllHandler } from "./after-all-handler";
import { beforeAllHandler } from "./before-all-handler";
import { testSuite } from "./test-suite";
Expand All @@ -14,7 +13,7 @@ export const TEST_PRIORITY_LABEL = "Priority: 1 (Normal)";
export const SIX_HOURS = 6 * 60 * 60 * 1000; // 6 hours

// return the current 7 character git commit hash using git rev-parse
export const GIT_COMMIT_HASH = env.COMMIT_REF || execSync("git rev-parse --short HEAD").toString().trim();
export const GIT_COMMIT_HASH = COMMIT_HASH?.substring(0, 7) ?? null;

export const owner = process.env.TEST_ORGANIZATION_NAME || "ubiquibot";
export const repo = process.env.TEST_REPOSITORY_NAME || "staging";
Expand Down
1 change: 0 additions & 1 deletion src/types/configuration-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const envConfigSchema = T.Object({
X25519_PRIVATE_KEY: T.String(),
PRIVATE_KEY: T.String(),
APP_ID: T.Number(),
COMMIT_REF: T.Optional(T.String()), // automatically set in netlify
});

export const validateEnvConfig = ajv.compile(envConfigSchema);
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,7 @@

"@types/dotenv@^8.2.0":
version "8.2.0"
resolved "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-8.2.0.tgz#5cd64710c3c98e82d9d15844375a33bf1b45d053"
integrity sha512-ylSC9GhfRH7m1EUXBXofhgx4lUWmFeQDINW5oLuS+gxWdfUeW4zJdeVTYVkexEW+e2VUvlZR2kGnGGipAWR7kw==
dependencies:
dotenv "*"
Expand Down

0 comments on commit 8c74da7

Please sign in to comment.