1 | import * as wmill from "windmill-client"; |
2 | import { basename } from "node:path" |
3 | const util = require('util'); |
4 | const exec = util.promisify(require('child_process').exec); |
5 |
|
6 |
|
7 | export async function main(repo_url_resource_path: string) { |
8 | const cwd = process.cwd(); |
9 | process.env["HOME"] = "."; |
10 | console.log(`Cloning repo from resource`); |
11 | const repo_name = await git_clone(repo_url_resource_path); |
12 | process.chdir(`${cwd}/${repo_name}`); |
13 | console.log(`Attempting an empty push to repository ${repo_name}`); |
14 | await git_push(); |
15 | console.log("Finished"); |
16 | process.chdir(`${cwd}`); |
17 | } |
18 |
|
19 | async function git_clone(repo_resource_path: string): Promise<string> { |
20 | |
21 | let repo_url = (await wmill.getResource(repo_resource_path)).url; |
22 | const azureMatch = repo_url.match(/AZURE_DEVOPS_TOKEN\((?<url>.+)\)/); |
23 | if (azureMatch) { |
24 | console.log( |
25 | "Requires Azure DevOps service account access token, requesting..." |
26 | ); |
27 | const azureResource = await wmill.getResource(azureMatch.groups.url); |
28 | const response = await fetch( |
29 | `https://login.microsoftonline.com/${azureResource.azureTenantId}/oauth2/token`, |
30 | { |
31 | method: "POST", |
32 | body: new URLSearchParams({ |
33 | client_id: azureResource.azureClientId, |
34 | client_secret: azureResource.azureClientSecret, |
35 | grant_type: "client_credentials", |
36 | resource: "499b84ac-1321-427f-aa17-267ca6975798/.default", |
37 | }), |
38 | } |
39 | ); |
40 | const { access_token } = await response.json(); |
41 | repo_url = repo_url.replace(azureMatch[0], access_token); |
42 | } |
43 | const repo_name = basename(repo_url, ".git"); |
44 | await sh_run(4, "git", "clone", "--quiet", "--depth", "1", repo_url, repo_name); |
45 | return repo_name; |
46 | } |
47 | async function git_push() { |
48 | await sh_run(undefined, "git", "config", "user.email", process.env["WM_EMAIL"]) |
49 | await sh_run(undefined, "git", "config", "user.name", process.env["WM_USERNAME"]) |
50 | await sh_run(undefined, "git", "push"); |
51 | } |
52 |
|
53 | async function sh_run( |
54 | secret_position: number | undefined, |
55 | cmd: string, |
56 | ...args: string[] |
57 | ) { |
58 | const nargs = secret_position != undefined ? args.slice() : args; |
59 | if (secret_position && secret_position < 0) { |
60 | secret_position = nargs.length - 1 + secret_position; |
61 | } |
62 | if (secret_position != undefined) { |
63 | nargs[secret_position] = "***"; |
64 | } |
65 | |
66 | console.log(`Running '${cmd} ${nargs.join(" ")} ...'`); |
67 | const command = exec(`${cmd} ${args.join(" ")}`) |
68 | |
69 | |
70 | |
71 | const { error, stdout, stderr } = await command |
72 | if (stdout.length > 0) { |
73 | console.log(stdout); |
74 | } |
75 | if (stderr.length > 0) { |
76 | console.log(stderr); |
77 | } |
78 | if (error) { |
79 | const err = `SH command '${cmd} ${nargs.join( |
80 | " " |
81 | )}' returned with error ${error}`; |
82 | throw Error(err); |
83 | } |
84 | console.log("Command successfully executed"); |
85 | } |
86 |
|