-
-
Save pipethedev/8b8ed9fb2b26047e056effbda71fb1ac to your computer and use it in GitHub Desktop.
search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async getRepos(installationId: number, { page, limit, q }: any, git: GIT_TYPE = GIT_TYPE.GITHUB) { | |
page = Number(page); | |
limit = Number(limit); | |
const deploymentProvider = DeploymentProvider.getProvider(git); | |
let data: any; | |
if (q) { | |
const { response } = await deploymentProvider.search({ | |
q, | |
installationId, | |
type: "repositories", | |
}); | |
data = response.data; | |
data.repositories = data.items; | |
} else { | |
const { response } = await deploymentProvider.getAllRepository({ | |
installationId, | |
limit: 100, | |
}); | |
data = response.data; | |
} | |
const totalCount = data.total_count; | |
let repositories = data.repositories; | |
const startIndex = (page - 1) * limit; | |
const endIndex = startIndex + limit; | |
// Fetch additional pages if necessary | |
while (repositories.length < limit && repositories.length < totalCount) { | |
const nextPage = Math.ceil(repositories.length / 100) + 1; | |
const { response } = await deploymentProvider.getAllRepository({ | |
installationId, | |
limit: 100, | |
page: nextPage, | |
}); | |
repositories = repositories.concat(response.data.repositories); | |
} | |
return { | |
repositories: repositories | |
.sort( | |
(a: IRepository, b: IRepository) => | |
new Date(b.pushed_at).getTime() - new Date(a.pushed_at).getTime(), | |
) | |
.filter((repo: IRepository) => | |
q ? repo.name.toLowerCase().includes(q.toLowerCase()) : true, | |
) | |
.slice(startIndex, endIndex) | |
.map((repo: IRepository) => this.mapRepoData(repo, installationId)), | |
totalPages: Math.ceil(repositories.length / limit), | |
currentPage: page, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment