-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple packages #72
base: main
Are you sure you want to change the base?
Changes from 10 commits
f5facbe
8a81d7a
31e12b6
4513f31
f0a7caf
3f26736
6531663
34301c8
7e81fc4
36d9774
f98a76d
3986572
c307ec0
03234f8
fa40cba
d253172
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {mockPackagesQueryResponse} from './graphql.mock' | ||
import { | ||
getRepoPackages as _getRepoPackages, | ||
QueryInfo | ||
} from '../../src/packages' | ||
import {Observable} from 'rxjs' | ||
|
||
describe.skip('get versions tests -- call graphql', () => { | ||
it('getRepoPackages -- succeeds', done => { | ||
const numPackages = 1 | ||
getRepoPackages({numPackages}).subscribe(result => { | ||
expect(result.packages.length).toBe(numPackages) | ||
done() | ||
}) | ||
}) | ||
|
||
it('getRepoPackages -- fails for invalid repo', done => { | ||
getRepoPackages({repo: 'actions-testin'}).subscribe({ | ||
error: err => { | ||
expect(err).toBeTruthy() | ||
done() | ||
}, | ||
complete: async () => done.fail('no error thrown') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('get versions tests -- mock graphql', () => { | ||
it('getRepoPackages -- success', done => { | ||
const numPackages = 5 | ||
mockPackagesQueryResponse(numPackages) | ||
|
||
getRepoPackages({numPackages}).subscribe(result => { | ||
expect(result.packages.length).toBe(numPackages) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
interface Params { | ||
owner?: string | ||
repo?: string | ||
numPackages?: number | ||
startCursor?: string | ||
token?: string | ||
} | ||
|
||
const defaultParams = { | ||
owner: 'namratajha', | ||
repo: 'test-repo', | ||
packageName: 'test-repo', | ||
numPackages: 1, | ||
startCursor: '', | ||
token: process.env.GITHUB_TOKEN as string | ||
} | ||
|
||
function getRepoPackages(params?: Params): Observable<QueryInfo> { | ||
const p: Required<Params> = {...defaultParams, ...params} | ||
return _getRepoPackages( | ||
p.owner, | ||
p.repo, | ||
p.numPackages, | ||
p.startCursor, | ||
p.token | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
GraphQlQueryResponseData, | ||
RequestParameters | ||
} from '@octokit/graphql/dist-types/types' | ||
|
||
import * as Graphql from '../../src/packages/graphql' | ||
import {GetPackagesQueryResponse} from '../../src/packages' | ||
|
||
export function getMockedPackagesQueryResponse( | ||
numPackages: number | ||
): GetPackagesQueryResponse { | ||
const packages = [] | ||
for (let i = 1; i <= numPackages; ++i) { | ||
packages.push({ | ||
node: { | ||
id: i.toString(), | ||
name: `package${i}` | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
repository: { | ||
packages: { | ||
totalCount: 200, | ||
pageInfo: { | ||
endCursor: 'AAA', | ||
hasNextPage: false | ||
}, | ||
edges: packages | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function mockPackagesQueryResponse(numVersions: number): void { | ||
const response = new Promise<GetPackagesQueryResponse>(resolve => { | ||
resolve(getMockedPackagesQueryResponse(numVersions)) | ||
}) as Promise<GraphQlQueryResponseData> | ||
jest | ||
.spyOn(Graphql, 'graphql') | ||
.mockImplementation( | ||
(token: string, query: string, parameters: RequestParameters) => response | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { getPackageNameFilter } from '../../src/packages' | ||
|
||
describe('package name filter -- create filter', () => { | ||
|
||
const packageNameList = [ | ||
'com.company.project.module1.package1', | ||
'com.company.project.module1.package2', | ||
'com.company.project.module2.package1', | ||
'com.company.project.module2.package2', | ||
'com.company.project.module3.package-name-lorem', | ||
'com.company.project.module3.package-name-ipsum', | ||
'com.company.project.module3.package-name-dolor', | ||
] | ||
|
||
it('getPackageNameFilter -- wildcard end filter', done => { | ||
const filter = getPackageNameFilter('com.company.project.module1.*') | ||
|
||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('wildcard') | ||
expect(result).toEqual([ | ||
'com.company.project.module1.package1', | ||
'com.company.project.module1.package2', | ||
]) | ||
done() | ||
}) | ||
|
||
it('getPackageNameFilter -- wildcard start filter', done => { | ||
const filter = getPackageNameFilter('*.package1') | ||
|
||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('wildcard') | ||
expect(result).toEqual([ | ||
'com.company.project.module1.package1', | ||
'com.company.project.module2.package1', | ||
]) | ||
done() | ||
}) | ||
|
||
it('getPackageNameFilter -- wildcard both sides filter', done => { | ||
const filter = getPackageNameFilter('*.project.module3.*') | ||
|
||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('wildcard') | ||
expect(result).toEqual([ | ||
'com.company.project.module3.package-name-lorem', | ||
'com.company.project.module3.package-name-ipsum', | ||
'com.company.project.module3.package-name-dolor' | ||
]) | ||
done() | ||
}) | ||
|
||
|
||
it('getPackageNameFilter -- wildcard all filter', done => { | ||
const filter = getPackageNameFilter('*') | ||
|
||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('wildcard') | ||
expect(result).toEqual(packageNameList.slice()) | ||
done() | ||
}) | ||
|
||
it('getPackageNameFilter -- regex filter', done => { | ||
const filter = getPackageNameFilter('/com\\.company\\.project\\.module.*\\.package1/') | ||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('regex') | ||
expect(result).toEqual([ | ||
'com.company.project.module1.package1', | ||
'com.company.project.module2.package1', | ||
]) | ||
done() | ||
}) | ||
|
||
it('getPackageNameFilter -- exact match filter', done => { | ||
const filter = getPackageNameFilter('com.company.project.module1.package1') | ||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters[0].type).toBe('string') | ||
expect(result).toEqual(['com.company.project.module1.package1']) | ||
done() | ||
}) | ||
|
||
|
||
it('getPackageNameFilter -- multiple filters', done => { | ||
const filter = getPackageNameFilter('com.company.project.module1.package1, com.company.project.module2.*, /.*module3.*-ipsum/') | ||
const result = packageNameList.filter(filter.apply); | ||
|
||
expect(filter.subfilters.length).toBe(3) | ||
expect(filter.subfilters[0].type).toBe('string') | ||
expect(filter.subfilters[1].type).toBe('wildcard') | ||
expect(filter.subfilters[2].type).toBe('regex') | ||
expect(result).toEqual([ | ||
'com.company.project.module1.package1', | ||
'com.company.project.module2.package1', | ||
'com.company.project.module2.package2', | ||
'com.company.project.module3.package-name-ipsum' | ||
]) | ||
done() | ||
}) | ||
|
||
|
||
it('getPackageNameFilter -- memoization, same input shoud return same output', done => { | ||
const filterText = 'com.company.project.module1.package1, com.company.project.module2.*, /.*module3.*-dolor/, *.-lorem' | ||
const filter1 = getPackageNameFilter(filterText) | ||
const filter2 = getPackageNameFilter(filterText) | ||
expect(filter1).toBe(filter2) | ||
expect(filter1.subfilters.length).toBe(4) | ||
done() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import {Input} from './input' | ||
import {EMPTY, Observable, of, throwError} from 'rxjs' | ||
import {deletePackageVersions, getOldestVersions, VersionInfo} from './version' | ||
import {concatMap, map, expand, tap} from 'rxjs/operators' | ||
import {getRepoPackages, getPackageNameFilter, PackageInfo} from './packages' | ||
import {concatMap, map, mergeMap, expand, tap} from 'rxjs/operators' | ||
|
||
const RATE_LIMIT = 99 | ||
let totalCount = 0 | ||
|
@@ -41,11 +42,58 @@ export function getVersionIds( | |
) | ||
} | ||
|
||
export function getPackageNames( | ||
owner: string, | ||
repo: string, | ||
numPackages: number, | ||
cursor: string, | ||
token: string | ||
): Observable<PackageInfo[]> { | ||
return getRepoPackages(owner, repo, numPackages, cursor, token).pipe( | ||
expand(value => | ||
value.paginate | ||
? getRepoPackages(owner, repo, numPackages, value.cursor, token) | ||
: EMPTY | ||
), | ||
tap( | ||
value => (totalCount = totalCount === 0 ? value.totalCount : totalCount) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use the totalCount variable to keep a check of no of total versions for a package. Using the same variable here will cause version count to be faulty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to review the whole code to see why that line was there. It is a bad idea to add that line since as you say, it is already being used for another purpose. I removed it, and because totalCount is no longer used, I removed all references on |
||
), | ||
map(value => value.packages) | ||
) | ||
} | ||
|
||
export function finalIds(input: Input): Observable<string[]> { | ||
if (input.packageVersionIds.length > 0) { | ||
return of(input.packageVersionIds) | ||
} | ||
if (input.hasOldestVersionQueryInfo()) { | ||
const filter = getPackageNameFilter(input.packageNames) | ||
if (!filter.isEmpty) { | ||
return getPackageNames( | ||
input.owner, | ||
input.repo, | ||
RATE_LIMIT, | ||
'', | ||
input.token | ||
) | ||
.pipe( | ||
mergeMap(value => { | ||
return value | ||
.filter(info => filter.apply(info.name)) | ||
.map(info => | ||
finalIds( | ||
new Input({ | ||
...input, | ||
packageNames: '', | ||
packageName: info.name | ||
}) | ||
) | ||
) | ||
}) | ||
) | ||
.pipe(mergeMap(val => val)) | ||
} | ||
|
||
if (input.minVersionsToKeep < 0) { | ||
// This code block is when num-old-versions-to-delete is specified. | ||
// Setting input.numOldVersionsToDelete is set as minimum of input.numOldVersionsToDelete and RATE_LIMIT | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
or package-names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thank you