0
Git repo test read write
One script reply has been approved by the moderators Verified

This script will test the connection to the Git repo passed stored in the resources passed as an argument. It first clones the repo (checking read) and then attemps an empty push (checking write)

Created by hugo697 147 days ago Viewed 6003 times
0
Submitted by hugo697 Deno
Verified 147 days ago
1
import * as wmill from "npm:windmill-client@1.235.0";
2
import { basename } from "https://deno.land/std@0.208.0/path/mod.ts";
3

4
export async function main(repo_url_resource_path: string) {
5
  const cwd = Deno.cwd();
6
  Deno.env.set("HOME", ".");
7
  console.log(`Cloning repo from resource`);
8

9
  const repo_name = await git_clone(repo_url_resource_path);
10

11
  Deno.chdir(`${cwd}/${repo_name}`);
12

13
  console.log(`Attempting an empty push to repository ${repo_name}`);
14
  await git_push();
15

16
  console.log("Finished");
17
  Deno.chdir(`${cwd}`);
18
}
19

20
async function git_clone(repo_resource_path: string): Promise<string> {
21
  // TODO: handle private SSH keys as well
22
  const repo_url = (await wmill.getResource(repo_resource_path)).url;
23
  const repo_name = basename(repo_url, ".git");
24

25
  await sh_run("git", "clone", "--quiet", "--depth", "1", repo_url, repo_name);
26
  return repo_name;
27
}
28

29
async function git_push() {
30
  await sh_run("git", "config", "user.email", Deno.env.get("WM_EMAIL"));
31
  await sh_run("git", "config", "user.name", Deno.env.get("WM_USERNAME"));
32
  await sh_run("git", "push");
33
}
34

35
async function sh_run(cmd: string, ...args: string[]) {
36
  // console.log(`Running '${cmd} ${args.join(" ")}'`)
37
  const command = new Deno.Command(cmd, {
38
    args: args,
39
  });
40
  const { code, stdout: _stdout, stderr: _stderr } = await command.output();
41

42
  if (code !== 0) {
43
    throw `SH command '${cmd} ${args.join(
44
      " "
45
    )}' returned with a non-zero status ${code}`;
46
  }
47
}
48