@@ -3,11 +3,12 @@ package main
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "flag"
7
- "fmt"
6
+ "io/fs"
8
7
"log/slog"
9
8
"os"
10
9
"os/exec"
10
+ "path/filepath"
11
+ "strings"
11
12
12
13
"github.com/buger/jsonparser"
13
14
"github.com/google/go-github/v69/github"
@@ -17,8 +18,6 @@ import (
17
18
)
18
19
19
20
func main () {
20
- flag .Parse ()
21
-
22
21
fetchAndWriteAPIDefinition ()
23
22
updateGoGithubDep ()
24
23
}
@@ -92,13 +91,13 @@ func fetchAndWriteAPIDefinition() {
92
91
93
92
// to catch possible format errors
94
93
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 ())
96
95
errorsFound = true
97
96
}
98
97
99
98
// to catch everything else (hopefully)
100
99
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 ())
102
101
errorsFound = true
103
102
}
104
103
@@ -110,33 +109,121 @@ func fetchAndWriteAPIDefinition() {
110
109
func updateGoGithubDep () {
111
110
ghClient := github .NewClient (nil )
112
111
113
- fileContent , _ , _ , err := ghClient .Repositories .GetContents (
112
+ releaseInfo , _ , err := ghClient .Repositories .GetLatestRelease (
114
113
context .Background (),
115
114
"google" ,
116
115
"go-github" ,
117
- "/go.mod" ,
118
- nil ,
119
116
)
120
117
121
118
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 )
123
125
}
124
126
125
- decodedContents , err := fileContent . GetContent ( )
127
+ goGithubMockFileBytes , err := os . ReadFile ( "go.mod" )
126
128
127
129
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 )
129
136
}
130
137
131
- goModFile , err := modfile .Parse (
138
+ goGithubMockModFile , err := modfile .Parse (
132
139
"go.mod" ,
133
- [] byte ( decodedContents ) ,
140
+ goGithubMockFileBytes ,
134
141
nil ,
135
142
)
136
143
137
144
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
+ }
139
162
}
140
163
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
+ }
142
229
}
0 commit comments