Collaboration Properties

Started by blakesha, June 07, 2024, 01:56:56 AM

Previous topic - Next topic

blakesha

Hi Team

Is there any way to get at the collaboration properties of the model from script. Specifically, the current branch details?

Phil Beauvoir

#1
Hi,

jArchi is not linked to other Archi plug-ins such as coArchi and so there is no API that exposes coArchi's methods. However, you could either add the JGit library that coArchi uses (https://www.eclipse.org/jgit/) to the class path and use that or read the contents of the "<repo_path>/.git/HEAD" file to access the current branch.

Here's some example code:

var imports = new JavaImporter(org.eclipse.jgit.api, java.io, java.nio.file);
with(imports) {
    // Path to the git repository (on Windows)
    const repoPath = new File("C:/Users/user-name/Documents/Archi/model-repository/repo");

    // Method #1 - using JGit to access the repository

    // Path to the lib folder containing JGit jar files (on Windows)
    const libPath =  new File("C:/Users/user-name/AppData/Roaming/Archi/dropins/org.archicontribs.modelrepository_0.9.2.202311181027/lib");
   
    // Add JGit jars to the class path
    libPath.listFiles().forEach(jarFile => {
        Java.addToClasspath(jarFile);
    });

    // Open the repository using JGit
    const git = Git.open(repoPath);

    // Print the current branch
    console.log(git.repository.branch);
   
    // Close the JGit repository
    git.close();

    // Method #2 - read the "HEAD" file in the .git folder of the repo

    const path = new File(repoPath, ".git/HEAD").toPath();
    const branch = Files.readString(path);
    console.log(branch.trim().split('/')[2]);
}

If all you want to do is get the name of the current branch, the second method is far simpler. Note that access to JGit and calling its methods is done at your own peril, and the jar file paths will be different for each coArchi version.
If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.