From 8aa3265a68339d53f96dc20c4924197da60859a9 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:11:20 +0200 Subject: [PATCH 1/7] add baseName to api response --- packages/client/src/api.ts | 2 ++ packages/client/src/ci-providers/Local.ts | 2 ++ packages/client/src/speculativeBranchSelection.ts | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) 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/speculativeBranchSelection.ts b/packages/client/src/speculativeBranchSelection.ts index ea6d6ae..4a23e66 100644 --- a/packages/client/src/speculativeBranchSelection.ts +++ b/packages/client/src/speculativeBranchSelection.ts @@ -36,7 +36,8 @@ export async function getPrInfoForSpeculativeBranch( sha: baseCommit, }, files: fileStatuses, - }; + } as any; + // @todo implement } async function getHeadCommit(repoPath: string): Promise { From 20d9cd89e65cf7857361bdfc9ef8403afcf3b5f6 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:16:21 +0200 Subject: [PATCH 2/7] add ability to override scope while saving JSON values --- packages/client/src/client.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 9de90f2..56f723e 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 { + public async getValue(name: string, scope?: string): Promise { if (!this.context.pr) { 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 { From f5d0ef9dad78edb2cd10d67044832f8df8f8207a Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:23:40 +0200 Subject: [PATCH 3/7] bump version --- packages/client/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/package.json b/packages/client/package.json index 984a31d..7ff792c 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.3", "main": "dist/index.js", "bin": { "codechecks": "dist/runner.js" From 0041ba1398bd6758e720d80faf0aacd789057ff5 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:34:49 +0200 Subject: [PATCH 4/7] rename getConstExecutionContext to getSharedExecutionContext --- packages/client/src/getExecutionContext.ts | 2 +- packages/client/src/runner.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/client/src/getExecutionContext.ts b/packages/client/src/getExecutionContext.ts index de29dbc..b00f6ba 100644 --- a/packages/client/src/getExecutionContext.ts +++ b/packages/client/src/getExecutionContext.ts @@ -10,7 +10,7 @@ import { getPrInfoForSpeculativeBranch } from "./speculativeBranchSelection"; /** * 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, 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; From aa47bc03358fec89b17ec406b5ff7f8a1b2b4451 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:56:45 +0200 Subject: [PATCH 5/7] get branch name for current commit --- packages/client/src/getExecutionContext.ts | 5 +++++ .../client/src/speculativeBranchSelection.ts | 15 ++----------- packages/client/src/utils/git.ts | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/client/src/getExecutionContext.ts b/packages/client/src/getExecutionContext.ts index b00f6ba..cbe3f32 100644 --- a/packages/client/src/getExecutionContext.ts +++ b/packages/client/src/getExecutionContext.ts @@ -6,6 +6,7 @@ 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. @@ -17,6 +18,7 @@ export async function getSharedExecutionContext( 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 getSharedExecutionContext( supportsPages: projectInfo.artifactsProxySupportsPages, }, currentSha, + currentBranchName, isLocalMode: localMode, pr: prInfo, isFork, @@ -91,6 +94,7 @@ export async function getSharedExecutionContext( 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/speculativeBranchSelection.ts b/packages/client/src/speculativeBranchSelection.ts index 4a23e66..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; @@ -40,10 +40,6 @@ export async function getPrInfoForSpeculativeBranch( // @todo implement } -async function getHeadCommit(repoPath: string): Promise { - return await run(repoPath, "git rev-parse HEAD"); -} - async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: string[]): Promise { const headBranch = await run(repoPath, `git rev-parse --abbrev-ref HEAD`); const baseBranchName = findSpeculativeBaseBranch(headBranch, speculativeBranchesInOrder); @@ -59,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}`); } } } @@ -105,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; +} From b30123729d0c03801b4d343a64ced1983fc7c7dd Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Wed, 7 Aug 2019 23:57:20 +0200 Subject: [PATCH 6/7] bump version --- packages/client/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/package.json b/packages/client/package.json index 7ff792c..114a3f0 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -13,7 +13,7 @@ ], "repository": "codechecks/monorepo", "author": "Chris Kaczor ", - "version": "0.1.6-beta.3", + "version": "0.1.6-beta.4", "main": "dist/index.js", "bin": { "codechecks": "dist/runner.js" From 1d65b077a7ac2147fe8ed9b009d753b7ebef5c97 Mon Sep 17 00:00:00 2001 From: Krzysztof Kaczor Date: Sat, 17 Aug 2019 12:44:34 +0200 Subject: [PATCH 7/7] fix big in custom scopes for saving files --- packages/client/package.json | 2 +- packages/client/src/client.ts | 4 ++-- tslint.json | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/client/package.json b/packages/client/package.json index 114a3f0..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.6-beta.4", + "version": "0.1.6-beta.6", "main": "dist/index.js", "bin": { "codechecks": "dist/runner.js" diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 56f723e..60ebfc8 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -24,10 +24,10 @@ export class CodechecksClient { } public async getValue(name: string, scope?: string): Promise { - if (!this.context.pr) { + if (!this.context.pr && !scope) { throw new NotPrError(); } - return this.api.getValue(name, scope || 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, scope?: string): Promise { 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 } }