diff --git a/packages/client/package.json b/packages/client/package.json index 984a31d..d402d83 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -13,7 +13,7 @@ ], "repository": "codechecks/monorepo", "author": "Chris Kaczor ", - "version": "0.1.5", + "version": "0.1.6-beta.6", "main": "dist/index.js", "bin": { "codechecks": "dist/runner.js" diff --git a/packages/client/src/api.ts b/packages/client/src/api.ts index 225fcda..88737a7 100644 --- a/packages/client/src/api.ts +++ b/packages/client/src/api.ts @@ -384,9 +384,11 @@ export interface PrInfo { }; head: { sha: string; + branchName: string; }; base: { sha: string; + branchName: string; }; files: FileStatuses; } diff --git a/packages/client/src/ci-providers/Local.ts b/packages/client/src/ci-providers/Local.ts index 560e8eb..1338357 100644 --- a/packages/client/src/ci-providers/Local.ts +++ b/packages/client/src/ci-providers/Local.ts @@ -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", }, }; } diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 9de90f2..60ebfc8 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -23,18 +23,18 @@ export class CodechecksClient { } } - public async getValue(name: string): Promise { - if (!this.context.pr) { + public async getValue(name: string, scope?: string): Promise { + if (!this.context.pr && !scope) { throw new NotPrError(); } - return this.api.getValue(name, this.context.pr.base.sha, this.getPublicProjectSlug()); + return this.api.getValue(name, scope || this.context.pr!.base.sha, this.getPublicProjectSlug()); } - public async saveValue(name: string, value: any): Promise { + public async saveValue(name: string, value: any, scope?: string): Promise { 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 { diff --git a/packages/client/src/getExecutionContext.ts b/packages/client/src/getExecutionContext.ts index de29dbc..cbe3f32 100644 --- a/packages/client/src/getExecutionContext.ts +++ b/packages/client/src/getExecutionContext.ts @@ -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 { 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(); @@ -76,6 +78,7 @@ export async function getConstExecutionContext( supportsPages: projectInfo.artifactsProxySupportsPages, }, currentSha, + currentBranchName, isLocalMode: localMode, pr: prInfo, isFork, @@ -91,6 +94,7 @@ export async function getConstExecutionContext( supportsPages: projectInfo.artifactsProxySupportsPages, }, currentSha, + currentBranchName, isLocalMode: localMode, isFork, isSpeculativePr: false, @@ -131,6 +135,7 @@ export interface SharedExecutionContext { }; isPrivate: boolean; currentSha: string; + currentBranchName: string; isPr: boolean; pr?: PrInfo; isLocalMode?: { diff --git a/packages/client/src/runner.ts b/packages/client/src/runner.ts index ea2a66b..48a0049 100644 --- a/packages/client/src/runner.ts +++ b/packages/client/src/runner.ts @@ -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"; @@ -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; diff --git a/packages/client/src/speculativeBranchSelection.ts b/packages/client/src/speculativeBranchSelection.ts index ea6d6ae..fd8ecb4 100644 --- a/packages/client/src/speculativeBranchSelection.ts +++ b/packages/client/src/speculativeBranchSelection.ts @@ -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; @@ -36,11 +36,8 @@ export async function getPrInfoForSpeculativeBranch( sha: baseCommit, }, files: fileStatuses, - }; -} - -async function getHeadCommit(repoPath: string): Promise { - return await run(repoPath, "git rev-parse HEAD"); + } as any; + // @todo implement } async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: string[]): Promise { @@ -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}`); } } } @@ -104,9 +100,3 @@ async function getFileStatuses(repo: string, baseCommit: string, headCommit: str removed, }; } - -export async function run(cwd: string, cmd: string): Promise { - const { stdout } = await execa(cmd, { shell: true, cwd }); - - return stdout; -} diff --git a/packages/client/src/utils/git.ts b/packages/client/src/utils/git.ts index 40e6988..2197a4c 100644 --- a/packages/client/src/utils/git.ts +++ b/packages/client/src/utils/git.ts @@ -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"); @@ -15,3 +18,22 @@ export function findRootGitRepository(path: string): string | undefined { return findRootGitRepository(parentDir); } } + +export async function getHeadCommit(repoPath: string): Promise { + return await run(repoPath, "git rev-parse HEAD"); +} + +export async function getBranchName(repoPath: string): Promise { + 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 { + const { stdout } = await execa(cmd, { shell: true, cwd }); + + return stdout; +} diff --git a/tslint.json b/tslint.json index edd9f90..6dd73cf 100644 --- a/tslint.json +++ b/tslint.json @@ -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 } }