@@ -3,11 +3,12 @@ package main
33import (
44 "bytes"
55 "context"
6- "flag"
7- "fmt"
6+ "io/fs"
87 "log/slog"
98 "os"
109 "os/exec"
10+ "path/filepath"
11+ "strings"
1112
1213 "github.com/buger/jsonparser"
1314 "github.com/google/go-github/v69/github"
@@ -17,8 +18,6 @@ import (
1718)
1819
1920func main () {
20- flag .Parse ()
21-
2221 fetchAndWriteAPIDefinition ()
2322 updateGoGithubDep ()
2423}
@@ -92,13 +91,13 @@ func fetchAndWriteAPIDefinition() {
9291
9392 // to catch possible format errors
9493 if err := exec .Command ("gofmt" , "-w" , gen .OUTPUT_FILEPATH ).Run (); err != nil {
95- slog .Info ("error executing gofmt" , "err" , err .Error ())
94+ slog .Error ("error executing gofmt" , "err" , err .Error ())
9695 errorsFound = true
9796 }
9897
9998 // to catch everything else (hopefully)
10099 if err := exec .Command ("go" , "vet" , "./..." ).Run (); err != nil {
101- slog .Info ("error executing go vet" , "err" , err .Error ())
100+ slog .Error ("error executing go vet" , "err" , err .Error ())
102101 errorsFound = true
103102 }
104103
@@ -110,33 +109,121 @@ func fetchAndWriteAPIDefinition() {
110109func updateGoGithubDep () {
111110 ghClient := github .NewClient (nil )
112111
113- fileContent , _ , _ , err := ghClient .Repositories .GetContents (
112+ releaseInfo , _ , err := ghClient .Repositories .GetLatestRelease (
114113 context .Background (),
115114 "google" ,
116115 "go-github" ,
117- "/go.mod" ,
118- nil ,
119116 )
120117
121118 if err != nil {
122- panic ("error fetching go.mod contents from google/go-github: " + err .Error ())
119+ slog .Error (
120+ "error fetching latest release from google/go-github" ,
121+ "err" , err .Error (),
122+ )
123+
124+ os .Exit (1 )
123125 }
124126
125- decodedContents , err := fileContent . GetContent ( )
127+ goGithubMockFileBytes , err := os . ReadFile ( "go.mod" )
126128
127129 if err != nil {
128- panic ("error decoding go.mod contents from google/go-github: " + err .Error ())
130+ slog .Error (
131+ "error reading go.mod contents from go-github-mock" ,
132+ "err" , err .Error (),
133+ )
134+
135+ os .Exit (1 )
129136 }
130137
131- goModFile , err := modfile .Parse (
138+ goGithubMockModFile , err := modfile .Parse (
132139 "go.mod" ,
133- [] byte ( decodedContents ) ,
140+ goGithubMockFileBytes ,
134141 nil ,
135142 )
136143
137144 if err != nil {
138- panic ("error parsing go.mod contents from google/go-github: " + err .Error ())
145+ slog .Error (
146+ "error parsing go.mod contents from go-github-mock" ,
147+ "err" , err .Error (),
148+ )
149+
150+ os .Exit (1 )
151+ }
152+
153+ localGoGithubPath := ""
154+ localGoGithubVersion := ""
155+
156+ for _ , requireInfo := range goGithubMockModFile .Require {
157+ if strings .HasPrefix (requireInfo .Mod .Path , "github.com/google/go-github/v" ) {
158+ localGoGithubPath = requireInfo .Mod .Path
159+ localGoGithubVersion = requireInfo .Mod .Version
160+ break
161+ }
139162 }
140163
141- fmt .Println (goModFile .Module .Mod .Path )
164+ // e.g. "v69.2.0" => "v69"
165+ latestGoGithubPath := "github.com/google/go-github/" + strings .Split (* releaseInfo .TagName , "." )[0 ]
166+ latestGoGithubVersion := * releaseInfo .TagName
167+
168+ // if versions are the same, exit early
169+ if localGoGithubPath == latestGoGithubPath && localGoGithubVersion == latestGoGithubVersion {
170+ slog .Info (
171+ "go-github dependency already on latest release, skipping upgrade" ,
172+ "local-go-github" , localGoGithubPath ,
173+ "local-go-github-version" , localGoGithubVersion ,
174+ "latest-go-github" , latestGoGithubPath ,
175+ "latest-go-github-version" , latestGoGithubVersion ,
176+ )
177+ return
178+ }
179+
180+ if localGoGithubPath == "" || localGoGithubVersion == "" {
181+ slog .Error ("unable to find local go-github version information" )
182+ os .Exit (1 )
183+ }
184+
185+ filepath .Walk ("." , func (path string , info fs.FileInfo , err error ) error {
186+ if strings .HasSuffix (path , ".go" ) {
187+ fileBytes , err := os .ReadFile (path )
188+
189+ if err != nil {
190+ return err
191+ }
192+
193+ fileContentsStr := string (fileBytes )
194+
195+ fileContentsStr = strings .ReplaceAll (
196+ fileContentsStr ,
197+ localGoGithubPath ,
198+ latestGoGithubPath ,
199+ )
200+
201+ fileContentsStr = strings .ReplaceAll (
202+ fileContentsStr ,
203+ localGoGithubVersion ,
204+ latestGoGithubVersion ,
205+ )
206+
207+ if err := os .WriteFile (
208+ path ,
209+ []byte (fileContentsStr ),
210+ os .FileMode (os .O_TRUNC ),
211+ ); err != nil {
212+ slog .Error (
213+ "failed to write update to file" ,
214+ "file" , path ,
215+ "err" , err .Error (),
216+ )
217+
218+ os .Exit (1 )
219+ }
220+ }
221+
222+ return nil
223+ })
224+
225+ // to catch possible format errors
226+ if err := exec .Command ("gofmt" , "-w" , gen .OUTPUT_FILEPATH ).Run (); err != nil {
227+ slog .Info ("error executing gofmt" , "err" , err .Error ())
228+ }
142229}
0 commit comments