summaryrefslogtreecommitdiff
path: root/findNext.go
blob: f9d7dd5d24c1df81d8a88a90c169e577179ba5f6 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// This is a simple example
package main

import (
	"fmt"
	"slices"

	"go.wit.com/log"

	"go.wit.com/lib/protobuf/gitpb"
)

var findCounter int
var findFix bool = false
var findOk bool = true

func checkDeps(repo *gitpb.Repo) error {
	if repo.GoDeps == nil {
		return fmt.Errorf("%s has GoDeps == nil", repo.GetGoPath())
	}
	all := repo.GoDeps.All()
	for all.Scan() {
		dep := all.Next()
		// log.Info(repo.GetGoPath(), dep.GoPath, dep.Version)

		// check if the package in question is waiting for another package to publish
		found := me.forge.FindByGoPath(dep.GoPath)
		if found == nil {
			return fmt.Errorf("%s has dep == nil", repo.GetGoPath(), dep.GoPath)
		}
		all := me.found.SortByFullPath()
		for all.Scan() {
			check := all.Next()
			if found.GetGoPath() == check.GetGoPath() {
				// this package is waiting on other packages to publish
				return fmt.Errorf("%s is waiting on %s", repo.GetGoPath(), found.GetGoPath())
			}
		}
		// found package isn't being published. is the version correct?
		// never check this? we are done?
		/*
			if found.GetLastTag() == dep.Version {
				// everything is normal
			} else {
				return fmt.Errorf("%s version mismatch on %s (%s vs %s)", repo.GetGoPath(), found.GetGoPath(), found.GetLastTag(), dep.Version)
			}
		*/
	}
	// everything might be cool?
	return nil
}

// trys to figure out if there is still something to update
// todo: redo this logic as it is terrible
// rename this findNext()
func findNext() bool {
	findCounter = 0
	all := me.found.SortByFullPath()
	for all.Scan() {
		check := all.Next()
		if check.GetMasterBranchName() != check.GetCurrentBranchName() {
			log.Info("YOU MUST BE ON THE MASTER BRANCH", check.GetGoPath())
			continue
		}
		if check.IsDirty() {
			log.Info("CAN NOT RELEASE DIRTY REPO", check.GetGoPath())
			continue
		}
		if alreadyDone(check) {
			log.Info("findNext() alreadyDone. WHY IS THIS STILL CHECKING?", check.GetGoPath())
			continue
		}
		log.Info("CHECKING:", check.GetGoPath())
		// _, err := check.RunVerboseOnError([]string{"go-mod-clean", "--strict"})
		_, err := check.RunQuiet([]string{"go-mod-clean", "--smart"})
		if err != nil {
			log.Info("FAILED: findNext() go-mod-clean --smart", check.GetGoPath(), err)
			continue
		}

		findCounter += 1
		if !check.ParseGoSum() {
			log.Info("ParseGoSum() failed", check.GetGoPath())
			log.Info("ParseGoSum() failed", check.GetGoPath())
			log.Info("ParseGoSum() failed", check.GetGoPath())
			continue
		}

		/*
			if err := me.forge.CleanGoDepsCheckOk(check); err != nil {
				log.Info("CleanGoDepsCheckOk() failed", check.GetGoPath(), err)
				log.Info("CleanGoDepsCheckOk() failed", check.GetGoPath(), err)
				continue
			}
		*/

		/*
			if err := checkDeps(check); err != nil {
				log.Info("CHECK DEPS FAILED", check.GetGoPath(), err)
				log.Info("CHECK DEPS FAILED", check.GetGoPath(), err)
				log.Info("CHECK DEPS FAILED", check.GetGoPath(), err)
				continue
			} else {
				log.Info("Might be ok?", check.GetGoPath())
			}
		*/
		fixGodeps(check)
		/*
			} else {
				log.Info("SKIPPING FIX", check.GetGoPath())
				log.Info("SKIPPING FIX", check.GetGoPath())
				log.Info("SKIPPING FIX", check.GetGoPath())
			}
		*/

		if err := me.forge.FinalGoDepsCheckOk(check, argv.Verbose); err != nil {
			// if err := me.forge.FinalGoDepsCheckOk(check, false); err != nil {
			log.Info("FinalGoDepsCheckOk() repo=", check.GetGoPath(), "err:", err)
			log.Info("CHECKING END:", check.GetGoPath())
			log.Info("")
			continue
		}
		log.Info("GOOD TO GO ON", check.GetGoPath())
		setCurrentRepo(check, "should be good to release", "pretty sure")
		return true
	}
	if findCounter == 0 {
		log.Info("NOTHING TO UPDATE. findCounter =", findCounter)
	} else {
		log.Info("me.current is nil findCounter =", findCounter, "so set findFix =", findFix)
	}
	log.Info("tried to findNext() but not sure what to do next counter =", findCounter, "findFix =", findFix)
	setCurrentRepo(nil, "findNext found nothing", "crap")
	me.release.status.SetText("ALL DONE?")
	return false
}

// tries to fix the go.mod and go.sum files
func fixGodeps(check *gitpb.Repo) bool {
	var good bool = true

	check.GoDeps = nil

	if result, err := check.RunQuiet([]string{"go-mod-clean", "--strict"}); err != nil {
		// log.Info("fixGoDeps() runGoClean() strict failed", check.GetGoPath(), err)
		// log.Info("fixGoDeps() runGoClean() strict failed", check.GetGoPath(), len(result.Stdout), len(result.Stderr))
		if len(result.Stderr) > 0 {
			slices.Reverse(result.Stderr)
			log.Info("fixGoDeps() runGoClean() strict failed", check.GetGoPath(), result.Stderr[0])
		}
		if len(result.Stdout) > 0 {
			slices.Reverse(result.Stdout)
			log.Info("fixGoDeps() runGoClean() strict failed", check.GetGoPath(), result.Stdout[0])
		}
		// log.Info("fixGoDeps() runGoClean() strict failed", check.GetGoPath())
		return false
	}
	if check.ParseGoSum() {
		return true
	} else {
		log.Info("ParseGoSum() failed but go-mod-clean --strict worked", check.GetGoPath())
		return false
	}
	// skip primative ones
	if check.GetGoPrimitive() {
		if check.Exists("go.sum") {
			log.Info("fixGoDeps() has go.sum but says it is primitive", check.GetGoPath())
			return false
		}
		log.Info("fixGoDeps() skipping primitive", check.GetGoPath())
		return true
	}
	log.Printf("current repo %s go dependancy count: %d\n", check.GetGoPath(), check.GoDepsLen())
	log.Printf("current repo %s go dependancy count: %d\n", check.GetGoPath(), check.GoDepsLen())
	log.Printf("current repo %s go dependancy count: %d\n", check.GetGoPath(), check.GoDepsLen())
	deps := check.GoDeps.SortByGoPath()
	for deps.Scan() {
		depRepo := deps.Next()
		// log.Info("found dep", depRepo.GetGoPath())
		if me.forge.Config.IsReadOnly(depRepo.GetGoPath()) {
			log.Info("IsReadOnly = true", depRepo.GetGoPath())
			continue
		} else {
			// log.Info("IsReadOnly = false", depRepo.GetGoPath())
		}
		found := me.forge.FindByGoPath(depRepo.GetGoPath())
		if found == nil {
			log.Info("not found:", depRepo.GetGoPath())
			continue
		}
		log.Printf("%-48s  dep ver=%10s repo ver=%10s target ver=%10s\n", found.GetGoPath(), depRepo.GetVersion(), found.GetMasterVersion(), found.GetTargetVersion())
		if depRepo.GetVersion() != found.GetMasterVersion() {
			log.Printf("%-48s  %10s (gitpb depRepo)\n", depRepo.GetGoPath(), depRepo.GetVersion())
			log.Printf("%-48s  %10s (gitpb found)\n", found.GetGoPath(), found.GetMasterVersion())
			cmd := []string{"go", "get", depRepo.GetGoPath() + "@latest"}
			check.RunVerbose(cmd)
		}
	}
	return good
}

func setCurrentRepo(check *gitpb.Repo, s string, note string) bool {
	me.current = check
	if check == nil {
		me.release.repo.SetText("")
		me.release.version.SetText("")
		me.release.releaseVersionB.SetText("nope")
		me.release.version.SetText("badver")
	} else {
		me.release.repo.SetText(check.GetGoPath())
		me.release.version.SetText(check.GetTargetVersion())
		me.release.releaseVersionB.SetText("release version " + check.GetTargetVersion())
		me.release.version.SetText(check.GetTargetVersion())
	}
	me.release.status.SetText(s)
	me.release.notes.SetText(note)
	me.release.openrepo.Enable()

	return true
}