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

Kk/support for branch history #64

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"repository": "codechecks/monorepo",
"author": "Chris Kaczor <chris@kaczor.io>",
"version": "0.1.5",
"version": "0.1.6-beta.6",
"main": "dist/index.js",
"bin": {
"codechecks": "dist/runner.js"
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,11 @@ export interface PrInfo {
};
head: {
sha: string;
branchName: string;
};
base: {
sha: string;
branchName: string;
};
files: FileStatuses;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/ci-providers/Local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export class LocalProvider implements CiProvider {
},
head: {
sha: await this.getCurrentSha(),
branchName: "feature-branch",
},
base: {
// @todo we should have heuristics to detect "main" branch, sometimes it's dev
sha: await this.getShaForRef("master"),
branchName: "master",
},
};
}
Expand Down
10 changes: 5 additions & 5 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ export class CodechecksClient {
}
}

public async getValue<T>(name: string): Promise<T | undefined> {
if (!this.context.pr) {
public async getValue<T>(name: string, scope?: string): Promise<T | undefined> {
if (!this.context.pr && !scope) {
throw new NotPrError();
}
return this.api.getValue<T>(name, this.context.pr.base.sha, this.getPublicProjectSlug());
return this.api.getValue<T>(name, scope || this.context.pr!.base.sha, this.getPublicProjectSlug());
}

public async saveValue(name: string, value: any): Promise<void> {
public async saveValue(name: string, value: any, scope?: string): Promise<void> {
if (this.context.isLocalMode) {
return;
}
return this.api.saveValue(name, value, this.context.currentSha, this.getPublicProjectSlug());
return this.api.saveValue(name, value, scope || this.context.currentSha, this.getPublicProjectSlug());
}

public async getFile(name: string, destinationPath: string): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion packages/client/src/getExecutionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { dirname } from "path";
import { LocalProvider } from "./ci-providers/Local";
import { CodeChecksSettings } from "./types";
import { getPrInfoForSpeculativeBranch } from "./speculativeBranchSelection";
import { getBranchName } from "./utils/git";

/**
* Better part of execution context stays the same for all codechecks files being executed so we just get it once.
*/
export async function getConstExecutionContext(
export async function getSharedExecutionContext(
api: Api,
ciProvider: CiProvider,
settings: CodeChecksSettings,
gitRepoRootPath: string,
): Promise<SharedExecutionContext> {
const currentSha = await ciProvider.getCurrentSha();
const currentBranchName = (await getBranchName(gitRepoRootPath)) || "master";
const isFork = await ciProvider.isFork();
const pr = await ciProvider.getPullRequestID();
const projectSlug = await ciProvider.getProjectSlug();
Expand Down Expand Up @@ -76,6 +78,7 @@ export async function getConstExecutionContext(
supportsPages: projectInfo.artifactsProxySupportsPages,
},
currentSha,
currentBranchName,
isLocalMode: localMode,
pr: prInfo,
isFork,
Expand All @@ -91,6 +94,7 @@ export async function getConstExecutionContext(
supportsPages: projectInfo.artifactsProxySupportsPages,
},
currentSha,
currentBranchName,
isLocalMode: localMode,
isFork,
isSpeculativePr: false,
Expand Down Expand Up @@ -131,6 +135,7 @@ export interface SharedExecutionContext {
};
isPrivate: boolean;
currentSha: string;
currentBranchName: string;
isPr: boolean;
pr?: PrInfo;
isLocalMode?: {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as program from "commander";
import ms = require("ms");

import { findProvider } from "./ci-providers";
import { getExecutionContext, getConstExecutionContext } from "./getExecutionContext";
import { getExecutionContext, getSharedExecutionContext } from "./getExecutionContext";
import { Api, getApiOptions } from "./api";
import { CodechecksClient } from "./client";
import { normalizePath, Path, maskSecrets } from "./utils";
Expand Down Expand Up @@ -35,7 +35,7 @@ async function main(project?: string, codecheckFiles: Path[] = findCodechecksFil
throw new Error("Couldn't find git project root!");
}
const settings = await loadCodechecksSettings(gitRoot);
const sharedExecutionCtx = await getConstExecutionContext(api, provider, settings, gitRoot);
const sharedExecutionCtx = await getSharedExecutionContext(api, provider, settings, gitRoot);
logger.debug({ sharedExecutionCtx });

(api as any).sharedCtx = sharedExecutionCtx;
Expand Down
18 changes: 4 additions & 14 deletions packages/client/src/speculativeBranchSelection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrInfo, FileStatuses } from "./api";
import { CodeChecksSettings } from "./types";
import { logger } from "./logger";
import execa = require("execa");
import { getHeadCommit, run } from "./utils/git";

const diffParser = require("./js/diff-parser/diff-parser.js").DiffParser;

Expand Down Expand Up @@ -36,11 +36,8 @@ export async function getPrInfoForSpeculativeBranch(
sha: baseCommit,
},
files: fileStatuses,
};
}

async function getHeadCommit(repoPath: string): Promise<string> {
return await run(repoPath, "git rev-parse HEAD");
} as any;
// @todo implement
}

async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: string[]): Promise<string | undefined> {
Expand All @@ -58,8 +55,7 @@ async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: strin
} catch (e) {
logger.debug(e);
logger.debug(`Failed to access origin/${baseBranchName}. Trying with: ${baseBranchName}`);
const { stdout: sha } = await execa(`git rev-parse ${baseBranchName}`, { shell: true, cwd: repoPath });
return sha;
return await run(repoPath, `git rev-parse ${baseBranchName}`);
}
}
}
Expand Down Expand Up @@ -104,9 +100,3 @@ async function getFileStatuses(repo: string, baseCommit: string, headCommit: str
removed,
};
}

export async function run(cwd: string, cmd: string): Promise<string> {
const { stdout } = await execa(cmd, { shell: true, cwd });

return stdout;
}
22 changes: 22 additions & 0 deletions packages/client/src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { existsSync } from "fs";
import { join } from "path";
import { logger } from "../logger";

import execa = require("execa");

export function findRootGitRepository(path: string): string | undefined {
const gitDirPath = join(path, ".git");
Expand All @@ -15,3 +18,22 @@ export function findRootGitRepository(path: string): string | undefined {
return findRootGitRepository(parentDir);
}
}

export async function getHeadCommit(repoPath: string): Promise<string> {
return await run(repoPath, "git rev-parse HEAD");
}

export async function getBranchName(repoPath: string): Promise<string | undefined> {
try {
return await run(repoPath, "git rev-parse --abbrev-ref HEAD");
} catch (e) {
logger.debug("Error in getBranchName ", e);
return undefined;
}
}

export async function run(cwd: string, cmd: string): Promise<string> {
const { stdout } = await execa(cmd, { shell: true, cwd });

return stdout;
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"interface-name": [true, "never-prefix"],
"no-commented-code": true,
"no-use-before-declare": false,
"await-promise": [true, "RequestPromise", "Bluebird"]
"await-promise": [true, "RequestPromise", "Bluebird"],
"no-useless-cast": false
}
}