From 91308c21a18e97e045e999fe1239e2d0794de15d Mon Sep 17 00:00:00 2001 From: "Cornelius A. Ludmann" Date: Wed, 16 Jun 2021 12:01:21 +0000 Subject: [PATCH] [GitLab] Fix opening empty GitLab repo --- .../src/gitlab/gitlab-context-parser.ts | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/components/server/src/gitlab/gitlab-context-parser.ts b/components/server/src/gitlab/gitlab-context-parser.ts index bc57b60bfd318f..ee98b6834f5b9e 100644 --- a/components/server/src/gitlab/gitlab-context-parser.ts +++ b/components/server/src/gitlab/gitlab-context-parser.ts @@ -122,7 +122,40 @@ export class GitlabContextParser extends AbstractContextParser implements IConte protected async handleDefaultContext(user: User, host: string, owner: string, repoName: string): Promise { try { const repository = await this.fetchRepo(user, `${owner}/${repoName}`); - return this.handleTreeContext(user, host, owner, repoName, repository.defaultBranch ? [ repository.defaultBranch ] : []); + if (!repository.defaultBranch) { + return { + isFile: false, + path: '', + title: `${owner}/${repoName}`, + repository + } + } + + try { + const branchOrTag = await this.getBranchOrTag(user, owner, repoName, [repository.defaultBranch!]); + return { + isFile: false, + path: '', + title: `${owner}/${repoName} - ${branchOrTag.name}`, + ref: branchOrTag.name, + revision: branchOrTag.revision, + refType: branchOrTag.type, + repository + }; + } catch (error) { + if (error && error.message && (error.message as string).startsWith("Cannot find tag/branch for context")) { + // the repo is empty (has no branches) + return { + isFile: false, + path: '', + title: `${owner}/${repoName} - ${repository.defaultBranch}`, + revision: '', + repository + } + } else { + throw error; + } + } } catch (error) { if (UnauthorizedError.is(error)) { throw error; @@ -148,7 +181,7 @@ export class GitlabContextParser extends AbstractContextParser implements IConte revision: branchOrTag && branchOrTag.revision, refType: branchOrTag && branchOrTag.type, repository - } + }; if (!branchOrTag) { return context; }