Skip to content

Commit

Permalink
Merge pull request #108 from ls1intum/theia-integration-test
Browse files Browse the repository at this point in the history
Fix repo detection for theia
  • Loading branch information
janthoXO authored Nov 26, 2024
2 parents cf85d5f + 94bac98 commit b314d4a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 15 deletions.
10 changes: 9 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
"request": "launch",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
"preLaunchTask": "${defaultBuildTask}",
"env": {
// "THEIA":"true",
"ARTEMIS_TOKEN":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhcnRlbWlzX2FkbWluIiwiYXV0aCI6IlJPTEVfVVNFUixST0xFX0FETUlOIiwidG9vbHMiOiJTQ09SUElPIiwiZXhwIjoxNzM1MjMwNzM3fQ.ipSGyQ4E3iE_g5v1ZLAMxs5E3CJ_TgyUoM9-Ni3tdz63dhFcEWtqSmM-WQlzPmjp0Bf_6t6Uu6OOsT8fIyMGVw",
"ARTEMIS_URL":"https://artemis-test9.artemis.cit.tum.de",
"GIT_URI":"https://[email protected]/git/THEIATESTTESTEXERCISE/theiatesttestexercise-artemis_admin.git",
"GIT_USER":"artemis_admin",
"GIT_MAIL":"[email protected]"
}
},
{
"name": "Extension Tests",
Expand Down
4 changes: 3 additions & 1 deletion src/authentication/authentication_provider.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as vscode from "vscode";
import { authenticateToken } from "./authentication_api";
import { theiaEnv } from "../theia/theia";
import { settings } from "../shared/settings";

export const AUTH_ID = "artemis";
const AUTH_NAME = `Credentials`; // what is displayed on the profile button
const SESSIONS_SECRET_KEY = `${AUTH_ID}.sessions`;
var SESSIONS_SECRET_KEY = `${AUTH_ID}.sessions`;

class ArtemisSession implements vscode.AuthenticationSession {
id: string = AUTH_ID;
Expand All @@ -27,6 +28,7 @@ export class ArtemisAuthenticationProvider implements vscode.AuthenticationProvi
private sessionPromise: Promise<ArtemisSession | undefined>;

constructor(private readonly secretStorage: vscode.SecretStorage) {
SESSIONS_SECRET_KEY = `${AUTH_ID}.${settings.base_url}.sessions`;
this.sessionPromise = this.getSessionFromStorage();

this._disposable = vscode.Disposable.from(
Expand Down
3 changes: 2 additions & 1 deletion src/exercise/exercise.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ export async function fetch_course_exercise_projectKey(
})
.then(async (response) => {
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`HTTP error! status: ${response.status} message: ${response.text}`
`HTTP error! status: ${response.status} message: ${errorText}`
);
}

Expand Down
25 changes: 15 additions & 10 deletions src/shared/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { set_state, state } from "./state";
import simpleGit, { RemoteWithRefs, SimpleGit } from "simple-git";
import * as path from "path";
import { retrieveVcsAccessToken } from "../authentication/authentication_api";
import { getLevel1SubfoldersOfWorkspace } from "../utils/filetree";

var gitRepo: SimpleGit | undefined;

Expand Down Expand Up @@ -165,15 +166,20 @@ async function getArtemisRepo(
return undefined;
}

for (const folder of workspaceFolders) {
const folderPath = folder.uri.fsPath;
const git: SimpleGit = simpleGit(folderPath);
// Get all level 1 subfolders of the workspace
const level1SubfoldersPath = await getLevel1SubfoldersOfWorkspace(workspaceFolders);

for (const folderPath of level1SubfoldersPath) {
const git: SimpleGit = simpleGit(folderPath.fsPath);
try {
const isRepo = await git.checkIsRepo();
if (isRepo) {
const remotes = await git.getRemotes(true);
if (remotes.length > 0) {
return { repo: git, remote: remotes[0] };
for(const remote of remotes) {
const url = new URL(remote.refs.fetch!);
if(url.hostname == new URL(settings.base_url).hostname && url.username == username) {
return { repo: git, remote: remote };
}
}
}
} catch (error: any) {
Expand All @@ -186,12 +192,11 @@ async function getArtemisRepo(

function getProjectKeyFromRepoUrl(repoUrl: string): string {
// extract projectKey {protocol}://{username}@{host}:{port}/git/{PROJECT_KEY}/{project_key}-{username}.git
const projectKeyMatch = repoUrl.match(
/^[a-zA-Z]+:\/\/[^@]+@[^:]+:[0-9]+\/git\/([^\/]+)\/[^\/]+-[^\/]+\.git$/
);
if (!projectKeyMatch) {
const parts = repoUrl.split("/");
if(parts.length < 5) {
throw new Error("Invalid artemis repository URL does not contain project key");
}

return projectKeyMatch[1];
const projectKey = parts[4]
return projectKey;
}
8 changes: 8 additions & 0 deletions src/theia/cloning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import simpleGit, { GitConfigScope } from "simple-git";
import * as path from "path";
import { theiaEnv } from "./theia";
import { getLevel1Subfolders } from "../utils/filetree";

export async function cloneTheia(cloneUrl: URL) {
// Check if a workspace is available in which the exercise can be cloned
Expand All @@ -17,6 +18,13 @@ export async function cloneTheia(cloneUrl: URL) {
const repoName = path.basename(cloneUrl.pathname, ".git"); // Use repository name as subdirectory name
const clonePath = path.join(destinationPath, repoName);

// check if repo already exists
const subfolders = await getLevel1Subfolders(vscode.Uri.file(destinationPath));
if (subfolders.some((folder) => folder.fsPath === clonePath)) {
vscode.window.showInformationMessage("Repository already cloned");
return;
}

// Clone the repository
const git = simpleGit(destinationPath);

Expand Down
4 changes: 2 additions & 2 deletions src/theia/theia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { execSync } from "child_process";

type theiaEnv = {
ARTEMIS_TOKEN: string | undefined;
ARTEMIS_URL: URL | undefined;
ARTEMIS_URL: string | undefined;
GIT_URI: URL | undefined;
GIT_USER: string | undefined;
GIT_MAIL: string | undefined;
Expand All @@ -18,7 +18,7 @@ function readTheiaEnv(): theiaEnv | undefined {

const theiaArtemisToken = getEnvVariable("ARTEMIS_TOKEN");
const theiaArtemisUrlString = getEnvVariable("ARTEMIS_URL");
const theiaArtemisUrl = theiaArtemisUrlString ? new URL(theiaArtemisUrlString) : undefined;
const theiaArtemisUrl = theiaArtemisUrlString ? theiaArtemisUrlString : undefined;
const theiaGitCloneUrlString = getEnvVariable("GIT_URI");
const theiaGitCloneUrl = theiaGitCloneUrlString ? new URL(theiaGitCloneUrlString) : undefined;
const theiaGitUserName = getEnvVariable("GIT_USER");
Expand Down
25 changes: 25 additions & 0 deletions src/utils/filetree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vscode from "vscode";

export async function getLevel1Subfolders(folderUri: vscode.Uri): Promise<vscode.Uri[]> {
const subfolders: vscode.Uri[] = [];
const entries = await vscode.workspace.fs.readDirectory(folderUri);

for (const [name, type] of entries) {
if (type === vscode.FileType.Directory) {
subfolders.push(vscode.Uri.joinPath(folderUri, name));
}
}

return subfolders;
}

export async function getLevel1SubfoldersOfWorkspace(workspaceFolders :readonly vscode.WorkspaceFolder[])
: Promise<vscode.Uri[]> {
return Promise.all(
workspaceFolders.map((folder) => getLevel1Subfolders(folder.uri))
).then((results) => {
const level1SubfoldersPath = results.flat();
return level1SubfoldersPath;
});
}

0 comments on commit b314d4a

Please sign in to comment.