Skip to content

Commit

Permalink
feat: add addFilesByExtension method & allow leading slash in path (#694
Browse files Browse the repository at this point in the history
)
  • Loading branch information
phated committed Jan 11, 2021
1 parent 9f69f41 commit 0d63813
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
8 changes: 8 additions & 0 deletions __snapshots__/github.js

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions src/github.ts
Expand Up @@ -1010,14 +1010,41 @@ export class GitHub {
}
}

normalizePrefix(prefix: string) {
return prefix.replace(/^[/\\]/, '').replace(/[/\\]$/, '');
}

async findFilesByFilename(
filename: string,
prefix?: string
): Promise<string[]> {
let q = `filename:${filename}+repo:${this.owner}/${this.repo}`;
if (prefix) {
prefix = prefix.replace(/[/\\]$/, '');
prefix = prefix.replace(/^[/\\]/, '');
prefix = this.normalizePrefix(prefix);
q += `+path:${prefix}`;
}
const response: {data: FileSearchResponse} = await this.octokit.search.code(
{
q: q,
}
);
return response.data.items.map(file => {
let path = file.path;
if (prefix) {
const pfix = new RegExp(`^${prefix}[/\\\\]`);
path = path.replace(pfix, '');
}
return path;
});
}

async findFilesByExtension(
extension: string,
prefix?: string
): Promise<string[]> {
let q = `extension:${extension}+repo:${this.owner}/${this.repo}`;
if (prefix) {
prefix = this.normalizePrefix(prefix);
q += `+path:${prefix}`;
}
const response: {data: FileSearchResponse} = await this.octokit.search.code(
Expand Down
2 changes: 1 addition & 1 deletion src/release-pr.ts
Expand Up @@ -328,11 +328,11 @@ export class ReleasePR {
}

static addPathStatic(file: string, path?: string) {
file = file.replace(/^[/\\]/, '');
if (path === undefined) {
return file;
} else {
path = path.replace(/[/\\]$/, '');
file = file.replace(/^[/\\]/, '');
return `${path}/${file}`;
}
}
Expand Down
54 changes: 54 additions & 0 deletions test/github.ts
Expand Up @@ -66,6 +66,18 @@ describe('GitHub', () => {
});
});

describe('normalizePrefix', () => {
it('removes a leading slash', async () => {
expect(github.normalizePrefix('/test')).to.equal('test');
});
it('removes a trailing slash', async () => {
expect(github.normalizePrefix('test/')).to.equal('test');
});
it('removes a leading & trailing slash', async () => {
expect(github.normalizePrefix('/test/')).to.equal('test');
});
});

describe('findFilesByfilename', () => {
it('returns files matching the requested pattern', async () => {
const fileSearchResponse = JSON.parse(
Expand Down Expand Up @@ -108,6 +120,48 @@ describe('GitHub', () => {
});
});

describe('findFilesByExtension', () => {
it('returns files matching the requested pattern', async () => {
const fileSearchResponse = JSON.parse(
readFileSync(resolve(fixturesPath, 'pom-file-search.json'), 'utf8')
);
req
.get('/search/code?q=extension%3Axml+repo%3Afake%2Ffake')
.reply(200, fileSearchResponse);
const pomFiles = await github.findFilesByExtension('xml');
snapshot(pomFiles);
req.done();
});

const prefixes = [
'appengine',
'appengine/',
'/appengine',
'/appengine/',
'appengine\\',
'\\appengine',
'\\appengine\\',
];
prefixes.forEach(prefix => {
it(`scopes pattern matching files to prefix(${prefix})`, async () => {
const fileSearchResponse = JSON.parse(
readFileSync(
resolve(fixturesPath, 'pom-file-search-with-prefix.json'),
'utf8'
)
);
req
.get(
'/search/code?q=extension%3Axml+repo%3Afake%2Ffake+path%3Aappengine'
)
.reply(200, fileSearchResponse);
const pomFiles = await github.findFilesByExtension('xml', prefix);
req.done();
expect(pomFiles).to.deep.equal(['pom.xml', 'foo/pom.xml']);
});
});
});

describe('findOpenReleasePRs', () => {
it('returns PRs that have all release labels', async () => {
req.get('/repos/fake/fake/pulls?state=open&per_page=100').reply(200, [
Expand Down

0 comments on commit 0d63813

Please sign in to comment.