Skip to content

Commit

Permalink
Merge pull request #459 from snyk-tech-services/fix/check-if-project-…
Browse files Browse the repository at this point in the history
…names-match-on-list-repos

fix: check if project name matches when fetching repos
  • Loading branch information
ariadna-roman committed Jul 31, 2023
2 parents 0304771 + 2636682 commit 6980abd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/lib/source-handlers/bitbucket-server/list-repos.ts
Expand Up @@ -11,14 +11,15 @@ interface BitbucketServeRepoData {
name: string;
project: {
key: string;
name?: string;
};
}[];
isLastPage: boolean;
nextPageStart: number;
}
export const fetchAllRepos = async (
url: string,
projectKey: string,
projectName: string,
token: string,
): Promise<BitbucketServerRepoData[]> => {
let lastPage = false;
Expand All @@ -27,11 +28,11 @@ export const fetchAllRepos = async (
let startFrom = 0;
const limit = 100;
while (!lastPage) {
debug(`Fetching page ${pageCount} for ${projectKey}\n`);
debug(`Fetching page ${pageCount} for ${projectName}\n`);
try {
const { repos, isLastPage, start } = await getRepos(
url,
projectKey,
projectName,
token,
startFrom,
limit,
Expand All @@ -49,7 +50,7 @@ export const fetchAllRepos = async (

const getRepos = async (
url: string,
projectKey: string,
projectName: string,
token: string,
startFrom: number,
limit: number,
Expand All @@ -65,20 +66,20 @@ const getRepos = async (
const { body, statusCode } =
await limiterWithRateLimitRetries<BitbucketServeRepoData>(
'get',
`${url}/rest/api/1.0/repos?projectname=${projectKey}&state=AVAILABLE&start=${startFrom}&limit=${limit}`,
`${url}/rest/api/1.0/repos?projectname=${projectName}&state=AVAILABLE&start=${startFrom}&limit=${limit}`,
headers,
limiter,
60000,
);
if (statusCode != 200) {
throw new Error(`Failed to fetch repos for ${url}/rest/api/1.0/repos?projectname=${projectKey}&state=AVAILABLE&start=${startFrom}&limit=${limit}\n
throw new Error(`Failed to fetch repos for ${url}/rest/api/1.0/repos?projectname=${projectName}&state=AVAILABLE&start=${startFrom}&limit=${limit}\n
Status Code: ${statusCode}\n
Response body: ${JSON.stringify(body)}`);
}
const { values, isLastPage, nextPageStart } = body;
start = nextPageStart || -1;
for (const repo of values) {
if (repo.project.key === projectKey) {
if (repo.project.name === projectName) {
repos.push({ projectKey: repo.project.key, repoSlug: repo.name });
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/source-handlers/bitbucket-server/project-is-empty.ts
Expand Up @@ -2,7 +2,7 @@ import { getBitbucketServerToken } from './get-bitbucket-server-token';
import { fetchAllRepos } from './list-repos';

export async function bitbucketServerProjectIsEmpty(
projectKey: string,
projectName: string,
sourceUrl?: string,
): Promise<boolean> {
const bitbucketServerToken = getBitbucketServerToken();
Expand All @@ -13,7 +13,7 @@ export async function bitbucketServerProjectIsEmpty(
}
const repos = await fetchAllRepos(
sourceUrl,
projectKey,
projectName,
bitbucketServerToken,
);
if (!repos || repos.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/system/fixtures/org-data/bitbucket-server-orgs.json
@@ -1,7 +1,7 @@
{
"orgData": [
{
"name": "AN",
"name": "antoine-snyk-demo",
"orgId": "<snyk_org_id>",
"integrations": {
"bitbucket-server": "<snyk_org_integration_id>"
Expand Down

0 comments on commit 6980abd

Please sign in to comment.