Skip to content

Commit

Permalink
Merge pull request #489 from kubeshop/f1ames/fix/git-url-parsing
Browse files Browse the repository at this point in the history
fix(synchronizer): improve git url parsing
  • Loading branch information
f1ames authored Aug 23, 2023
2 parents a9c4dac + 9b6ba48 commit bf8798a
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-fireants-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/synchronizer": patch
---

Fixed invalid git URL parsing
125 changes: 124 additions & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion packages/synchronizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const policy = await authenticator.getPolicy('/home/kubeshope/...');

// By repo data
const policy = await authenticator.getPolicy({
provider: 'github',
provider: 'github.com',
remote: 'origin',
owner: 'kubeshop',
name: 'monokle-core',
Expand Down
3 changes: 3 additions & 0 deletions packages/synchronizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@
"dependencies": {
"@monokle/validation": "*",
"env-paths": "^3.0.0",
"git-url-parse": "^13.1.0",
"mkdirp": "^3.0.1",
"node-fetch": "^3.3.2",
"normalize-url": "^8.0.0",
"openid-client": "^5.4.3",
"simple-git": "^3.19.1",
"slugify": "^1.6.6",
"yaml": "^2.3.1"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/git-url-parse": "^9.0.1",
"@types/mocha": "^10.0.1",
"@types/sinon": "^10.0.16",
"c8": "^8.0.1",
Expand Down
20 changes: 10 additions & 10 deletions packages/synchronizer/src/__tests__/synchronizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Synchronizer Tests', () => {
const synchronizer = createDefaultMonokleSynchronizer(new StorageHandlerPolicy(storagePath));

const policy = await synchronizer.getPolicy({
provider: 'github',
provider: 'github.com',
remote: 'origin',
owner: 'kubeshop',
name: 'monokle-core',
Expand All @@ -55,24 +55,24 @@ describe('Synchronizer Tests', () => {
});

it('returns policy if there is policy file (from path)', async () => {
const storagePath = await createTmpConfigDir('github-kubeshop-monokle-core.policy.yaml');
const storagePath = await createTmpConfigDir('githubcom-kubeshop-monokle-core.policy.yaml');
const synchronizer = createDefaultMonokleSynchronizer(new StorageHandlerPolicy(storagePath));

const policy = await synchronizer.getPolicy(storagePath);

assert.isObject(policy);
assert.isTrue(policy.valid);
assert.isNotEmpty(policy.path);
assert.match(policy.path, /github-kubeshop-monokle-core.policy.yaml$/);
assert.match(policy.path, /githubcom-kubeshop-monokle-core.policy.yaml$/);
assert.isNotEmpty(policy.policy);
});

it('returns policy if there is policy file (from git data)', async () => {
const storagePath = await createTmpConfigDir('github-kubeshop-monokle-core.policy.yaml');
const storagePath = await createTmpConfigDir('githubcom-kubeshop-monokle-core.policy.yaml');
const synchronizer = createDefaultMonokleSynchronizer(new StorageHandlerPolicy(storagePath));

const policy = await synchronizer.getPolicy({
provider: 'github',
provider: 'github.com',
remote: 'origin',
owner: 'kubeshop',
name: 'monokle-core',
Expand All @@ -81,7 +81,7 @@ describe('Synchronizer Tests', () => {
assert.isObject(policy);
assert.isTrue(policy.valid);
assert.isNotEmpty(policy.path);
assert.match(policy.path, /github-kubeshop-monokle-core.policy.yaml$/);
assert.match(policy.path, /githubcom-kubeshop-monokle-core.policy.yaml$/);
assert.isNotEmpty(policy.policy);
assert.isNotEmpty(policy.policy.plugins);
});
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('Synchronizer Tests', () => {
stubs.push(queryApiStub);

const repoData = {
provider: 'github',
provider: 'github.com',
remote: 'origin',
owner: 'kubeshop',
name: 'monokle-core',
Expand All @@ -187,7 +187,7 @@ describe('Synchronizer Tests', () => {
assert.isObject(newPolicy);
assert.isTrue(newPolicy.valid);
assert.isNotEmpty(newPolicy.path);
assert.match(newPolicy.path, /github-kubeshop-monokle-core.policy.yaml$/);
assert.match(newPolicy.path, /githubcom-kubeshop-monokle-core.policy.yaml$/);
assert.isNotEmpty(newPolicy.policy);
assert.isNotEmpty(newPolicy.policy.plugins);
assert.isNotEmpty(newPolicy.policy.rules);
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('Synchronizer Tests', () => {
stubs.push(queryApiStub);

const repoData = {
provider: 'github',
provider: 'github.com',
remote: 'origin',
owner: 'kubeshop',
name: 'monokle-core',
Expand All @@ -283,7 +283,7 @@ describe('Synchronizer Tests', () => {
assert.isObject(policy);
assert.isTrue(policy.valid);
assert.isNotEmpty(policy.path);
assert.match(policy.path, /github-kubeshop-monokle-core.policy.yaml$/);
assert.match(policy.path, /githubcom-kubeshop-monokle-core.policy.yaml$/);
assert.isNotEmpty(policy.policy);
assert.isNotEmpty(policy.policy.plugins);
assert.isNotEmpty(policy.policy.rules);
Expand Down
21 changes: 11 additions & 10 deletions packages/synchronizer/src/handlers/gitHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gitUrlParse from 'git-url-parse';
import {simpleGit} from 'simple-git';
import type {RemoteWithRefs} from 'simple-git';

Expand All @@ -20,18 +21,18 @@ export class GitHandler {
}

const url = remote.refs.push;
// With generic git support in Cloud, this should also become generic. The same for 'provider' field.
const match = url.match(/github\.com(\/|:)([^\/]+)\/([^\/]+)\.git/);
if (!match) {
try {
const urlParts = gitUrlParse(url);

return {
provider: urlParts.source,
remote: remote.name,
owner: urlParts.owner,
name: urlParts.name,
};
} catch (err: any) {
return undefined;
}

return {
provider: 'github',
remote: remote.name,
owner: match[2],
name: match[3],
};
}

async isGitRepo(folderPath: string) {
Expand Down
Loading

0 comments on commit bf8798a

Please sign in to comment.