Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Fix deploy from gitlab using a group's repo
Browse files Browse the repository at this point in the history
- only supported user based repos
- fixes #4153
  • Loading branch information
richard-cox committed Jul 29, 2020
1 parent cd44987 commit 288e1d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, of as observableOf } from 'rxjs';
import { map } from 'rxjs/operators';
import { combineLatest, Observable, of as observableOf, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Md5 } from 'ts-md5/dist/md5';

import { GitBranch, GitCommit, GitRepo } from '../../../store/types/git.types';
Expand Down Expand Up @@ -29,19 +29,18 @@ export class GitLabSCM implements GitSCM {
getRepository(httpClient: HttpClient, projectName: string): Observable<GitRepo> {
const parts = projectName.split('/');

let obs$ = httpClient.get(`${gitLabAPIUrl}/users/${parts[0]}/projects?search=${parts[1]}`);
if (parts.length !== 2) {
obs$ = observableOf(null);
}
const obs$ = parts.length !== 2 ?
observableOf(null) :
httpClient.get(`${gitLabAPIUrl}/projects/${parts.join('%2F')}`);

return obs$.pipe(
map((data: any) => {
if (data.length !== 1) {
if (!data) {
throw new HttpErrorResponse({
status: 404
});
}
return this.convertProject(data[0]);
return this.convertProject(data);
})
);
}
Expand Down Expand Up @@ -110,17 +109,25 @@ export class GitLabSCM implements GitSCM {

getMatchingRepositories(httpClient: HttpClient, projectName: string): Observable<string[]> {
const prjParts = projectName.split('/');
let url = `${gitLabAPIUrl}/projects?search=${projectName}`;
if (prjParts.length > 1) {
url = `${gitLabAPIUrl}/users/${prjParts[0]}/projects?search=${prjParts[1]}`;
}
return httpClient.get(url).pipe(
map((repos: any) => {
return repos.map(item => item.path_with_namespace);
})

const obs$ = prjParts.length > 1 ?
this.getMatchingUserGroupRepositories(httpClient, prjParts) :
httpClient.get(`${gitLabAPIUrl}/projects?search=${projectName}`);

return obs$.pipe(
map((repos: any[]) => repos.map(item => item.path_with_namespace)),
);
}

private getMatchingUserGroupRepositories(httpClient: HttpClient, prjParts: string[]): Observable<any[]> {
return combineLatest([
httpClient.get<[]>(`${gitLabAPIUrl}/users/${prjParts[0]}/projects/?search=${prjParts[1]}`).pipe(catchError(() => of([]))),
httpClient.get<[]>(`${gitLabAPIUrl}/groups/${prjParts[0]}/projects?search=${prjParts[1]}`).pipe(catchError(() => of([]))),
]).pipe(
map(([a, b]: [any[], any[]]) => a.concat(b)),
)
}

private convertProject(prj: any): GitRepo {
return {
...prj,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export function createFailedGithubRequestMessage(error: any, logger: LoggerServi
const response = parseHttpPipeError(error, logger);
const message = response.message || '';
return error.status === 403 && message.startsWith('API rate limit exceeded for') ?
'Github ' + message.substring(0, message.indexOf('(')) :
'Github request failed';
'Git ' + message.substring(0, message.indexOf('(')) :
'Git request failed';
}

@Injectable()
Expand Down

0 comments on commit 288e1d7

Please sign in to comment.