gitClone static method

Future<Map> gitClone({
  1. required String username,
  2. required String repo_name,
  3. required String token_github,
  4. bool isOrg = false,
  5. bool isPrivate = false,
  6. String branch = "main",
  7. String? workingDirectory,
  8. Map<String, String>? environment,
  9. bool includeParentEnvironment = true,
  10. Client? httpClient,
})

Implementation

static Future<Map> gitClone({
  required String username,
  required String repo_name,
  required String token_github,
  bool isOrg = false,
  bool isPrivate = false,
  String branch = "main",
  String? workingDirectory,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
  Client? httpClient,
}) async {
  var res = await Process.run("which", ["git"]);

  if (res.exitCode != 0) {
    return {"@type": "error", "message": "git_not_found", "description": "Please install git first"};
  }

  GitHub gitHub = GitHub(auth: Authentication.withToken(token_github), client: httpClient);

  RepositorySlug repositorySlug = RepositorySlug(username, repo_name);
  Repository? repository = await Future(() async {
    try {
      return await gitHub.repositories.getRepository(repositorySlug);
    } catch (e) {
      return null;
    }
  });
  if (repository == null) {
    try {
      await gitHub.repositories.createRepository(
        CreateRepository(
          repo_name,
          private: isPrivate,
          autoInit: true,
        ),
        org: (isOrg) ? username : null,
      );
    } catch (e) {
      return {"@type": "error", "message": "cant_create_repo", "description": "Please Check Permission Token Github First"};
    }
  }
  List<String> git_adds = [
    "clone https://${username}:${token_github}@github.com/${username}/${repo_name} .",
  ];
  for (String element in git_adds) {
    Process process = await Process.start(
      "git",
      element.split(" "),
      workingDirectory: workingDirectory,
      environment: environment,
      includeParentEnvironment: includeParentEnvironment,
    );
    String message = "";
    process.stderr.listen((event) {
      try {
        message += utf8.decode(event, allowMalformed: true);
      } catch (e) {}
    });
    process.stdout.listen((event) {
      try {
        message += utf8.decode(event, allowMalformed: true);
      } catch (e) {}
    });
    int exit_code = await process.exitCode;
    bool is_error = false;
    if (exit_code != 0) {
      is_error = true;
    }
    if (is_error) {
      return {
        "@type": "error",
        "message": "",
        "description": "${message.trim()}".replaceAll(RegExp("(ghp_.*)", caseSensitive: false), ""),
      };
    }
  }

  return {
    "@type": "ok",
  };
}