tangled
alpha
login
or
join now
julien.rbrt.fr
/
tangled-core
forked from
tangled.org/core
0
fork
atom
Monorepo for Tangled — https://tangled.org
0
fork
atom
overview
issues
pulls
pipelines
hook,cmd/knot: add knot hook command
anirudh.fi
9 months ago
d4acb23f
275d63d3
verified
This commit was signed with the committer's
known signature
.
anirudh.fi
SSH Key Fingerprint:
SHA256:cz35vdbiWEzCNEfuL9fMC2JVIhtXavXBHrRjv8gxpAk=
+82
1 changed file
expand all
collapse all
unified
split
hook
hook.go
+82
hook/hook.go
···
1
1
+
package hook
2
2
+
3
3
+
import (
4
4
+
"bufio"
5
5
+
"context"
6
6
+
"fmt"
7
7
+
"net/http"
8
8
+
"os"
9
9
+
"strings"
10
10
+
11
11
+
"github.com/urfave/cli/v3"
12
12
+
)
13
13
+
14
14
+
// The hook command is nested like so:
15
15
+
//
16
16
+
// knot hook --[flags] [hook]
17
17
+
func Command() *cli.Command {
18
18
+
return &cli.Command{
19
19
+
Name: "hook",
20
20
+
Usage: "run git hooks",
21
21
+
Flags: []cli.Flag{
22
22
+
&cli.StringFlag{
23
23
+
Name: "git-dir",
24
24
+
Usage: "base directory for git repos",
25
25
+
},
26
26
+
&cli.StringFlag{
27
27
+
Name: "user-did",
28
28
+
Usage: "git user's did",
29
29
+
},
30
30
+
&cli.StringFlag{
31
31
+
Name: "user-handle",
32
32
+
Usage: "git user's handle",
33
33
+
},
34
34
+
&cli.StringFlag{
35
35
+
Name: "internal-api",
36
36
+
Usage: "endpoint for the internal API",
37
37
+
Value: "http://localhost:5444",
38
38
+
},
39
39
+
},
40
40
+
Commands: []*cli.Command{
41
41
+
{
42
42
+
Name: "post-recieve",
43
43
+
Usage: "sends a post-recieve hook to the knot (waits for stdin)",
44
44
+
Action: postRecieve,
45
45
+
},
46
46
+
},
47
47
+
}
48
48
+
}
49
49
+
50
50
+
func postRecieve(ctx context.Context, cmd *cli.Command) error {
51
51
+
gitDir := cmd.String("git-dir")
52
52
+
userDid := cmd.String("user-did")
53
53
+
userHandle := cmd.String("user-handle")
54
54
+
endpoint := cmd.String("internal-api")
55
55
+
56
56
+
payloadReader := bufio.NewReader(os.Stdin)
57
57
+
payload, _ := payloadReader.ReadString('\n')
58
58
+
59
59
+
client := &http.Client{}
60
60
+
61
61
+
req, err := http.NewRequest("POST", endpoint+"/hooks/post-receive", strings.NewReader(payload))
62
62
+
if err != nil {
63
63
+
return fmt.Errorf("failed to create request: %w", err)
64
64
+
}
65
65
+
66
66
+
req.Header.Set("Content-Type", "text/plain")
67
67
+
req.Header.Set("X-Git-Dir", gitDir)
68
68
+
req.Header.Set("X-Git-User-Did", userDid)
69
69
+
req.Header.Set("X-Git-User-Handle", userHandle)
70
70
+
71
71
+
resp, err := client.Do(req)
72
72
+
if err != nil {
73
73
+
return fmt.Errorf("failed to execute request: %w", err)
74
74
+
}
75
75
+
defer resp.Body.Close()
76
76
+
77
77
+
if resp.StatusCode != http.StatusOK {
78
78
+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
79
79
+
}
80
80
+
81
81
+
return nil
82
82
+
}