Skip to content

Commit

Permalink
#52: Add special logic to handle github repository separately from az…
Browse files Browse the repository at this point in the history
…ure (#53)

* #52: Add special logic to handle github repository separately from azure.

* Create ten-jokes-boil.md

* Improve INCORRECT_REPOSITORY_FIELD support for azure. Add tests.

* Only validate repo field check for supported hosts

Co-Authored-By: Mitchell Hamilton <[email protected]>

Co-authored-by: Nate Radebaugh <[email protected]>
Co-authored-by: Mitchell Hamilton <[email protected]>
  • Loading branch information
3 people committed Mar 12, 2020
1 parent 8c7fdbb commit 503f242
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-jokes-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/cli": patch
---

Add special logic for `INCORRECT_REPOSITORY_FIELD` check to handle github repository separately from azure
28 changes: 21 additions & 7 deletions packages/cli/src/checks/INCORRECT_REPOSITORY_FIELD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export default makeCheck<ErrorType>({

if (typeof rootRepositoryField === "string") {
let result = parseGithubUrl(rootRepositoryField);
if (result !== null) {
if (result !== null && (result.host === 'github.com' || result.host === 'dev.azure.com')) {
let baseRepositoryUrl = "";
if (result.host === "github.com") {
baseRepositoryUrl = `${result.protocol}//${result.host}/${result.owner}/${result.name}`;
} else if (result.host === "dev.azure.com") {
baseRepositoryUrl = `${result.protocol}//${result.host}/${result.owner}/${result.name}/_git/${result.filepath}`;
}

if (workspace === rootWorkspace) {
let correctRepositoryField = `https://github.com/${result.owner}/${result.name}`;
let correctRepositoryField = baseRepositoryUrl;
if (rootRepositoryField !== correctRepositoryField) {
return [
{
Expand All @@ -32,11 +39,18 @@ export default makeCheck<ErrorType>({
}
} else {
// TODO: handle default branches that aren't master
let correctRepositoryField = `https://github.com/${result.owner}/${
result.name
}/tree/master/${normalizePath(
path.relative(rootWorkspace.dir, workspace.dir)
)}`;
let correctRepositoryField = "";

if (result.host === "github.com") {
correctRepositoryField = `${baseRepositoryUrl}/tree/master/${normalizePath(
path.relative(rootWorkspace.dir, workspace.dir)
)}`;
} else if (result.host === "dev.azure.com") {
correctRepositoryField = `${baseRepositoryUrl}?path=${normalizePath(
path.relative(rootWorkspace.dir, workspace.dir)
)}&version=GBmaster&_a=contents`;
}

let currentRepositoryField = (workspace.config as any).repository;
if (correctRepositoryField !== currentRepositoryField) {
return [
Expand Down
218 changes: 163 additions & 55 deletions packages/cli/src/checks/__tests__/INCORRECT_REPOSITORY_FIELD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,169 @@ import check from "../INCORRECT_REPOSITORY_FIELD";
import { getWS, getFakeWS } from "../../test-helpers";

describe("incorrect repository field", () => {
it("should work", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://github.com/Thinkmill/manypkg";
rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(workspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x)).toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://github.com/Thinkmill/manypkg/tree/master/packages/no-repository-field",
"currentRepositoryField": undefined,
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((workspace.config as any).repository).toBe(
"https://github.com/Thinkmill/manypkg/tree/master/packages/no-repository-field"
);
describe("github", () => {
it("should work", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://github.com/Thinkmill/manypkg";
rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(workspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x))
.toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://github.com/Thinkmill/manypkg/tree/master/packages/no-repository-field",
"currentRepositoryField": undefined,
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((workspace.config as any).repository).toBe(
"https://github.com/Thinkmill/manypkg/tree/master/packages/no-repository-field"
);
});
it("should fix root in a different format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://github.com/Thinkmill/manypkg.git";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x))
.toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://github.com/Thinkmill/manypkg",
"currentRepositoryField": "https://github.com/Thinkmill/manypkg.git",
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((rootWorkspace.config as any).repository).toBe(
"https://github.com/Thinkmill/manypkg"
);
});
it("should do nothing if already in good format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://github.com/Thinkmill/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x)).toMatchInlineSnapshot(
`Array []`
);

expect((rootWorkspace.config as any).repository).toBe(
"https://github.com/Thinkmill/manypkg"
);
});
});
it("should fix root in a different format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://github.com/Thinkmill/manypkg.git";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x)).toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://github.com/Thinkmill/manypkg",
"currentRepositoryField": "https://github.com/Thinkmill/manypkg.git",
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((rootWorkspace.config as any).repository).toBe(
"https://github.com/Thinkmill/manypkg"
);

describe("azure devops", () => {
it("should work", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg";
rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(workspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x))
.toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://dev.azure.com/Thinkmill/monorepos/_git/manypkg?path=packages/no-repository-field&version=GBmaster&_a=contents",
"currentRepositoryField": undefined,
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((workspace.config as any).repository).toBe(
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg?path=packages/no-repository-field&version=GBmaster&_a=contents"
);
});
it("should fix root in a different format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://[email protected]/Thinkmill/monorepos/_git/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x))
.toMatchInlineSnapshot(`
Array [
Object {
"correctRepositoryField": "https://dev.azure.com/Thinkmill/monorepos/_git/manypkg",
"currentRepositoryField": "https://[email protected]/Thinkmill/monorepos/_git/manypkg",
"type": "INCORRECT_REPOSITORY_FIELD",
},
]
`);

check.fix(errors[0]);

expect((rootWorkspace.config as any).repository).toBe(
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg"
);
});
it("should do nothing if already in good format", () => {
let ws = getWS();
let rootWorkspace = getFakeWS("root");

(rootWorkspace.config as any).repository =
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg";

rootWorkspace.dir = __dirname;
let workspace = getFakeWS("no-repository-field");
workspace.dir = path.join(__dirname, "packages/no-repository-field");
ws.set("depends-on-one", workspace);
ws.set("root", rootWorkspace);
let errors = check.validate(rootWorkspace, ws, rootWorkspace);
expect(errors.map(({ workspace, ...x }: any) => x)).toMatchInlineSnapshot(
`Array []`
);

expect((rootWorkspace.config as any).repository).toBe(
"https://dev.azure.com/Thinkmill/monorepos/_git/manypkg"
);
});
});
});

0 comments on commit 503f242

Please sign in to comment.