missions api2 gitea
This commit is contained in:
parent
4ac46d2d3c
commit
a33048ccf6
@ -48,18 +48,13 @@ export class GiteaService {
|
|||||||
? this.getApiEndpoint(`org/${this.owner.substring(1)}/repos`)
|
? this.getApiEndpoint(`org/${this.owner.substring(1)}/repos`)
|
||||||
: this.getApiEndpoint('user/repos');
|
: this.getApiEndpoint('user/repos');
|
||||||
|
|
||||||
// Create the repository
|
// Create the repository with minimal parameters
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
endpoint,
|
endpoint,
|
||||||
{
|
{
|
||||||
name: repoName,
|
name: repoName,
|
||||||
description: mission.intention || mission.name,
|
|
||||||
private: true,
|
private: true,
|
||||||
auto_init: true,
|
auto_init: true
|
||||||
gitignores: 'Node',
|
|
||||||
license: 'MIT',
|
|
||||||
readme: 'Default',
|
|
||||||
default_branch: 'main'
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
@ -98,27 +93,30 @@ export class GiteaService {
|
|||||||
async addCollaborators(owner: string, repo: string, missionUsers: any[]): Promise<void> {
|
async addCollaborators(owner: string, repo: string, missionUsers: any[]): Promise<void> {
|
||||||
console.log(`Adding ${missionUsers.length} collaborators to ${owner}/${repo}`);
|
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 {
|
try {
|
||||||
// Skip if no username is provided
|
// Skip if no username is found
|
||||||
if (!missionUser.user.username) {
|
if (!userInfo.giteaUsername) {
|
||||||
console.log(`No username for user ${missionUser.user.email}, skipping`);
|
console.log(`No Gitea username for user ${userInfo.missionUser.user.email}, skipping`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine permission level based on role
|
// Determine permission level based on role
|
||||||
// For Gitea: 'read', 'write', 'admin'
|
// For Gitea: 'read', 'write', 'admin'
|
||||||
let permission = 'write';
|
let permission = 'write';
|
||||||
if (missionUser.role === 'gardien-temps') {
|
if (userInfo.missionUser.role === 'gardien-temps') {
|
||||||
permission = 'admin';
|
permission = 'admin';
|
||||||
} else if (missionUser.role === 'observateur') {
|
} else if (userInfo.missionUser.role === 'observateur') {
|
||||||
permission = 'read';
|
permission = 'read';
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Adding ${missionUser.user.username} as ${permission}`);
|
console.log(`Adding ${userInfo.giteaUsername} as ${permission}`);
|
||||||
|
|
||||||
await axios.put(
|
await axios.put(
|
||||||
this.getApiEndpoint(`repos/${owner}/${repo}/collaborators/${missionUser.user.username}`),
|
this.getApiEndpoint(`repos/${owner}/${repo}/collaborators/${userInfo.giteaUsername}`),
|
||||||
{ permission },
|
{ permission },
|
||||||
{
|
{
|
||||||
headers: {
|
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) {
|
} 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
|
// Continue with other collaborators even if one fails
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -140,6 +138,64 @@ export class GiteaService {
|
|||||||
console.log('Finished adding collaborators');
|
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<Array<{
|
||||||
|
missionUser: any,
|
||||||
|
giteaUsername: string | null
|
||||||
|
}>> {
|
||||||
|
// 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
|
* Delete a repository
|
||||||
* @param owner Repository owner
|
* @param owner Repository owner
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user