Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole White committed Mar 6, 2024
1 parent f801ad1 commit bd956ed
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 273 deletions.
185 changes: 3 additions & 182 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"homepage": "https://github.com/autoblocksai/cli#readme",
"dependencies": {
"@actions/exec": "^1.1.1",
"@actions/github": "^6.0.0",
"@autoblocks/client": "^0.0.33",
"@hono/node-server": "^1.7.0",
"@hono/zod-validator": "^0.1.11",
Expand Down
81 changes: 52 additions & 29 deletions src/handlers/testing/exec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Hono, type Context } from 'hono';
import { z, type SafeParseReturnType } from 'zod';
import { renderTestProgress } from './components/progress';
import { EventName, emitter, type EventSchemas } from './emitter';
import { makeCIContext, type CIContext, commentOnPullRequest } from './util/ci';
import { makeCIContext, type CIContext } from './util/ci';
import { findAvailablePort } from './util/net';

type UncaughtError = EventSchemas[EventName.UNCAUGHT_ERROR];
Expand Down Expand Up @@ -71,6 +71,13 @@ class RunManager {
*/
private ciContext: CIContext | undefined;

/**
* Only relevant for CI runs. This is returned from our REST API
* when we POST to /builds at the beginning of a build on CI.
* It's used in subsequent requests to associate runs with the build.
*/
private ciBuildId: string | undefined;

constructor(args: { apiKey: string; runMessage: string | undefined }) {
this.apiKey = args.apiKey;
this.message = args.runMessage;
Expand Down Expand Up @@ -117,44 +124,67 @@ class RunManager {
level: 'error',
message: `Failed to get CI context: ${err}`,
});
throw err;
}
}
return this.ciContext;
}

async setup(): Promise<void> {
const ciContext = await this.makeCIContext();

if (!ciContext) {
return;
}

emitter.emit(EventName.CONSOLE_LOG, {
ctx: 'cli',
level: 'debug',
message: `Running in CI environment: ${JSON.stringify(ciContext, null, 2)}`,
});

const { id } = await this.post<{ id: string }>('/builds', {
provider: ciContext.provider,
repositoryExternalId: ciContext.repoId,
repositoryName: ciContext.repoName,
// TODO: figure out how to get internal branch ID from github
branchExternalId: ciContext.branchName,
branchName: ciContext.branchName,
isDefaultBranch: ciContext.branchName === ciContext.defaultBranchName,
buildHtmlUrl: ciContext.buildHtmlUrl,
commitSha: ciContext.commitSha,
commitHtmlUrl: ciContext.commitHtmlUrl,
commitMessage: ciContext.commitMessage,
commitComitterName: ciContext.commitCommitterName,
commitCommitterEmail: ciContext.commitCommitterEmail,
commitAuthorName: ciContext.commitAuthorName,
commitAuthorEmail: ciContext.commitAuthorEmail,
commitCommittedDate: ciContext.commitCommittedDate,
pullRequestNumber: ciContext.pullRequestNumber,
pullRequestTitle: ciContext.pullRequestTitle,
pullRequestHtmlUrl: ciContext.pullRequestHtmlUrl,
});

this.ciBuildId = id;
}

async handleStartRun(args: { testExternalId: string }): Promise<string> {
emitter.emit(EventName.RUN_STARTED, {
testExternalId: args.testExternalId,
});
const ciContext = await this.makeCIContext();
const payload = {

const { id } = await this.post<{ id: string }>('/runs', {
testExternalId: args.testExternalId,
message: this.message,
...ciContext,
};
const { id } = await this.post<{ id: string }>('/runs', payload);
buildId: this.ciBuildId,
});
this.testExternalIdToRunId[args.testExternalId] = id;
return id;
}

async handleEndRun(args: { testExternalId: string }): Promise<void> {
emitter.emit(EventName.RUN_ENDED, { testExternalId: args.testExternalId });

if (this.ciContext) {
try {
await commentOnPullRequest({
context: this.ciContext,
evaluations: this.evaluations,
});
} catch (err) {
emitter.emit(EventName.CONSOLE_LOG, {
ctx: 'cli',
level: 'error',
message: `Failed to comment on PR: ${err}`,
});
}
}

try {
const runId = this.currentRunId({ testExternalId: args.testExternalId });
await this.post(`/runs/${runId}/end`);
Expand Down Expand Up @@ -527,14 +557,7 @@ export async function exec(args: {
runMessage: args.runMessage,
});

const ciContext = await runManager.makeCIContext();
if (ciContext) {
emitter.emit(EventName.CONSOLE_LOG, {
ctx: 'cli',
level: 'debug',
message: `Running in CI environment: ${JSON.stringify(ciContext, null, 2)}`,
});
}
await runManager.setup();

let runningServer: ServerType | undefined = undefined;

Expand Down
Loading

0 comments on commit bd956ed

Please sign in to comment.