summaryrefslogtreecommitdiff
path: root/doPatchsets.go
blob: be1f28fc3f6ec699143a7185945e80336cfe614c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main

import (
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"
)

func getPatchset(w http.ResponseWriter, pbname string) {
	if pbname == "" {
		fmt.Fprintf(w, "filename was empty")
		return
	}
	msg := fmt.Sprintf("filename = %s\n", pbname)
	log.Info(msg)

	filename := filepath.Join(LIBDIR, "patchset/", pbname)
	data, err := os.ReadFile(filename)
	if err != nil {
		msg := fmt.Sprintf("Error reading file %s: %v\n", filename, err)
		fmt.Printf(msg)
		fmt.Fprintf(w, msg)
		return
	}
	var m *forgepb.Patchset
	m = new(forgepb.Patchset)
	if err := m.Unmarshal(data); err != nil {
		msg := fmt.Sprintf("proto.Unmarshal() failed on %s len=%d\n", filename, len(data))
		fmt.Printf(msg)
		fmt.Fprintf(w, msg)
		msg = fmt.Sprintf("proto.Unmarshal() error %v\n", err)
		fmt.Printf(msg)
		fmt.Fprintf(w, msg)
		return
	}
	log.Info("going to w.Write(data) with len", len(data))
	w.Write(data)
}

func listPatchsets(w http.ResponseWriter) {
	dirname := filepath.Join(LIBDIR, "patchset/")
	// Open the directory
	entries, err := os.ReadDir(dirname)
	if err != nil {
		fmt.Printf("Error reading directory: %v\n", err)
		fmt.Fprintf(w, "Error reading directory: %v\n", err)
		return
	}

	// Iterate through the directory entries
	for _, entry := range entries {
		// Check if the entry is a file and matches the *.pb pattern
		if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" {
			bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name()))
			if err != nil {
				fmt.Fprintln(w, entry.Name(), err)
				fmt.Println(entry.Name(), err)
				continue
			}
			var p *forgepb.Patchset
			p = new(forgepb.Patchset)
			err = p.Unmarshal(bytes)
			if err != nil {
				fmt.Fprintln(w, entry.Name(), err)
				fmt.Println(entry.Name(), err)
				continue
			}
			author := "Author: " + p.GitAuthorName
			author += " <" + p.GitAuthorEmail + ">"

			// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
			// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
			fmt.Fprintln(w, entry.Name(), p.Name, p.Comment, author)
			fmt.Println(entry.Name(), p.Name, p.Comment, author)
		}
	}
}

func savePatchset(w http.ResponseWriter, msg []byte) {
	log.Info("proto.Unmarshal() try message len", len(msg))
	var m *forgepb.Patchset
	m = new(forgepb.Patchset)
	if err := m.Unmarshal(msg); err != nil {
		log.Info("proto.Unmarshal() failed on wire message len", len(msg))
		log.Info("error =", err)
		return
	}
	log.Info("GOT patchset:", len(msg))
	fmt.Fprintln(w, "GOT patchset:", len(msg))
	all := m.Patches.SortByFilename()
	for all.Scan() {
		repo := all.Next()
		log.Info("filename:", repo.Filename)
		fmt.Fprintln(w, "filename:", repo.Filename)
	}
	now := time.Now()
	// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
	timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
	filename := filepath.Join(LIBDIR, "patchset/", timestamp+".submitted.pb")
	regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		log.Info("filename open error:", filename, err)
		fmt.Fprintln(w, "filename open error:", filename, err)
		return
	}
	regfile.Write(msg)
	regfile.Close()
}