diff --git a/lib/services/gitea-service.ts b/lib/services/gitea-service.ts index ef53081c..0eddce4f 100644 --- a/lib/services/gitea-service.ts +++ b/lib/services/gitea-service.ts @@ -48,18 +48,13 @@ export class GiteaService { ? this.getApiEndpoint(`org/${this.owner.substring(1)}/repos`) : this.getApiEndpoint('user/repos'); - // Create the repository + // Create the repository with minimal parameters const response = await axios.post( endpoint, { name: repoName, - description: mission.intention || mission.name, private: true, - auto_init: true, - gitignores: 'Node', - license: 'MIT', - readme: 'Default', - default_branch: 'main' + auto_init: true }, { headers: { @@ -98,27 +93,30 @@ export class GiteaService { async addCollaborators(owner: string, repo: string, missionUsers: any[]): Promise { console.log(`Adding ${missionUsers.length} collaborators to ${owner}/${repo}`); - const addCollaboratorPromises = missionUsers.map(async (missionUser) => { + // First try to look up users if needed + const usersWithInfo = await this.enrichUsersWithGiteaInfo(missionUsers); + + const addCollaboratorPromises = usersWithInfo.map(async (userInfo) => { try { - // Skip if no username is provided - if (!missionUser.user.username) { - console.log(`No username for user ${missionUser.user.email}, skipping`); + // Skip if no username is found + if (!userInfo.giteaUsername) { + console.log(`No Gitea username for user ${userInfo.missionUser.user.email}, skipping`); return; } // Determine permission level based on role // For Gitea: 'read', 'write', 'admin' let permission = 'write'; - if (missionUser.role === 'gardien-temps') { + if (userInfo.missionUser.role === 'gardien-temps') { permission = 'admin'; - } else if (missionUser.role === 'observateur') { + } else if (userInfo.missionUser.role === 'observateur') { permission = 'read'; } - console.log(`Adding ${missionUser.user.username} as ${permission}`); + console.log(`Adding ${userInfo.giteaUsername} as ${permission}`); await axios.put( - this.getApiEndpoint(`repos/${owner}/${repo}/collaborators/${missionUser.user.username}`), + this.getApiEndpoint(`repos/${owner}/${repo}/collaborators/${userInfo.giteaUsername}`), { permission }, { headers: { @@ -128,9 +126,9 @@ export class GiteaService { } ); - console.log(`Added ${missionUser.user.username} as collaborator`); + console.log(`Added ${userInfo.giteaUsername} as collaborator`); } catch (error) { - console.error(`Error adding collaborator ${missionUser.user.username}:`, error); + console.error(`Error adding collaborator ${userInfo.missionUser.user.email}:`, error); // Continue with other collaborators even if one fails } }); @@ -139,6 +137,64 @@ export class GiteaService { await Promise.all(addCollaboratorPromises); console.log('Finished adding collaborators'); } + + /** + * Try to enrich users with Gitea username information + * @param missionUsers The mission users + * @returns Users with additional Gitea username info if available + */ + private async enrichUsersWithGiteaInfo(missionUsers: any[]): Promise> { + // For each user, try to find their Gitea username + return await Promise.all(missionUsers.map(async (missionUser) => { + let giteaUsername = null; + + // Try to search for the user by email directly + try { + if (!missionUser.user.email) { + console.log('User has no email address, skipping'); + return { missionUser, giteaUsername }; + } + + // Look up user by email in Gitea + const searchResponse = await axios.get( + this.getApiEndpoint('users/search'), + { + params: { q: missionUser.user.email }, + headers: { + 'Authorization': `token ${this.apiToken}` + } + } + ); + + if (searchResponse.data && + searchResponse.data.data && + searchResponse.data.data.length > 0) { + // Look for exact email match first + const exactMatch = searchResponse.data.data.find((user: any) => + user.email === missionUser.user.email + ); + + if (exactMatch) { + giteaUsername = exactMatch.username; + console.log(`Found exact match Gitea username "${giteaUsername}" for ${missionUser.user.email}`); + } else { + // If no exact match, use the first result + giteaUsername = searchResponse.data.data[0].username; + console.log(`Found closest match Gitea username "${giteaUsername}" for ${missionUser.user.email}`); + } + } else { + console.log(`No Gitea user found for email ${missionUser.user.email}`); + } + } catch (error) { + console.log(`Error searching for Gitea user with email ${missionUser.user.email}:`, error); + } + + return { missionUser, giteaUsername }; + })); + } /** * Delete a repository