diff --git a/client/src/components/pipelines/form.vue b/client/src/components/pipelines/form.vue index 0935bd376..7e82e34d6 100644 --- a/client/src/components/pipelines/form.vue +++ b/client/src/components/pipelines/form.vue @@ -631,6 +631,7 @@ export default defineComponent({ gogs: false, docker: true }, + repositoriesListLoaded: false, // flag to indicate if repositoriesList has been loaded git: { keys: {}, repository: {}, @@ -721,11 +722,12 @@ export default defineComponent({ }, mounted() { this.getContextList(); - this.listRepositories(); + this.listRepositories().then(() => { + this.loadRepository(); + this.loadPipeline(); + }); this.loadDefaultregistry(); this.listBuildpacks(); - this.loadRepository(); - this.loadPipeline(); }, components: { Breadcrumbs, @@ -747,10 +749,18 @@ export default defineComponent({ } }); }, - listRepositories() { - axios.get('/api/repo/providers').then(response => { - this.repositoriesList = response.data - }); + async listRepositories() { + const response = await axios.get('/api/repo/providers'); + this.repositoriesList = response.data + this.repositoriesListLoaded = true; + // Set default repotab to first enabled provider (not github by default) + const providers = ['github', 'gitea', 'gitlab', 'gogs', 'bitbucket']; + for (const provider of providers) { + if (this.repositoriesList[provider as keyof typeof this.repositoriesList] === true) { + this.repotab = provider; + break; + } + } }, listBuildpacks() { axios.get('/api/config/runpacks').then(response => { @@ -827,6 +837,10 @@ export default defineComponent({ }, loadRepository() { + // Only load repositories if the selected provider is enabled + if (!this.repositoriesList[this.repotab as keyof typeof this.repositoriesList]) { + return; + } axios.get(`/api/repo/${this.repotab}/repositories`) .then(response => { this.gitrepoItems = response.data; diff --git a/server/src/repo/git/github.ts b/server/src/repo/git/github.ts index dbb580c2b..0ee75e6ea 100644 --- a/server/src/repo/git/github.ts +++ b/server/src/repo/git/github.ts @@ -17,11 +17,14 @@ import { RequestError } from '@octokit/types'; export class GithubApi extends Repo { private octokit: any; + private token: string; constructor(baseUrl: string, token: string) { super('github'); - if (baseUrl === '') { + this.token = token; + + if (!baseUrl || baseUrl === '') { baseUrl = 'https://api.github.com'; } @@ -31,6 +34,10 @@ export class GithubApi extends Repo { }); } + private isConfigured(): boolean { + return !!this.token && this.token !== ''; + } + protected async getRepository(gitrepo: string): Promise { let ret: IRepository = { status: 500, @@ -43,6 +50,11 @@ export class GithubApi extends Repo { }, }; + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping getRepository'); + return ret; + } + const parsed = gitUrlParse(gitrepo); const repo = parsed.name; const owner = parsed.owner; @@ -92,6 +104,10 @@ export class GithubApi extends Repo { } public async getRepositories() { + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping getRepositories'); + return []; + } const res = await this.octokit.request('GET /user/repos', {}); return res.data; } @@ -308,6 +324,10 @@ export class GithubApi extends Repo { public async listRepos(): Promise { const ret: string[] = []; + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping listRepos'); + return ret; + } try { const repos = await this.octokit.request('GET /user/repos', { visibility: 'all', @@ -325,6 +345,10 @@ export class GithubApi extends Repo { public async getBranches(gitrepo: string): Promise { const ret: string[] = []; + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping getBranches'); + return ret; + } const { repo, owner } = this.parseRepo(gitrepo); @@ -348,6 +372,10 @@ export class GithubApi extends Repo { public async getReferences(gitrepo: string): Promise { const ret: string[] = []; + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping getReferences'); + return ret; + } const { repo, owner } = this.parseRepo(gitrepo); @@ -401,6 +429,10 @@ export class GithubApi extends Repo { public async getPullrequests(gitrepo: string): Promise { const ret: IPullrequest[] = []; + if (!this.isConfigured()) { + this.logger.debug('GitHub API is not configured, skipping getPullrequests'); + return ret; + } const { repo, owner } = this.parseRepo(gitrepo);