@@ -26,7 +26,7 @@ func GetBookmarksRepoName() string {
2626 return "MyBookmarks"
2727}
2828
29- func UpdateBookmarks (ctx context.Context , githubUser string , userToken * oauth2.Token , sourceRef , branch , text string ) error {
29+ func UpdateBookmarks (ctx context.Context , githubUser string , userToken * oauth2.Token , sourceRef , branch , text , expectSHA string ) error {
3030 client := github .NewClient (oauth2 .NewClient (ctx , oauth2 .StaticTokenSource (userToken )))
3131 defaultBranch , created , err := GetDefaultBranch (ctx , githubUser , client , branch )
3232 if err != nil {
@@ -62,14 +62,16 @@ func UpdateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.T
6262 return fmt .Errorf ("CreateRepo: %w" , err )
6363 }
6464 return CreateBookmarks (ctx , githubUser , userToken , branch , text )
65- err = nil
6665 }
6766 if err != nil {
6867 return fmt .Errorf ("UpdateBookmarks: client.Repositories.GetContents: %w" , err )
6968 }
7069 if contents == nil || contents .Content == nil {
7170 return nil
7271 }
72+ if expectSHA != "" && contents .SHA != nil && * contents .SHA != expectSHA {
73+ return fmt .Errorf ("bookmarks modified concurrently" )
74+ }
7375 _ , _ , err = client .Repositories .UpdateFile (ctx , githubUser , RepoName , "bookmarks.txt" , & github.RepositoryContentFileOptions {
7476 Message : SP ("Auto change from web" ),
7577 Content : []byte (text ),
@@ -182,24 +184,28 @@ func CreateBookmarks(ctx context.Context, githubUser string, userToken *oauth2.T
182184 return nil
183185}
184186
185- func GetBookmarks (ctx context.Context , githubUser string , ref string , userToken * oauth2.Token ) (string , error ) {
187+ func GetBookmarks (ctx context.Context , githubUser string , ref string , userToken * oauth2.Token ) (string , string , error ) {
186188 client := github .NewClient (oauth2 .NewClient (ctx , oauth2 .StaticTokenSource (userToken )))
187189 contents , _ , resp , err := client .Repositories .GetContents (ctx , githubUser , RepoName , "bookmarks.txt" , & github.RepositoryContentGetOptions {
188190 Ref : ref ,
189191 })
190192 switch resp .StatusCode {
191193 case http .StatusNotFound :
192- return "" , nil
194+ return "" , "" , nil
193195 }
194196 if err != nil {
195- return "" , fmt .Errorf ("GetBookmarks: %w" , err )
197+ return "" , "" , fmt .Errorf ("GetBookmarks: %w" , err )
196198 }
197199 if contents .Content == nil {
198- return "" , nil
200+ return "" , "" , nil
199201 }
200202 s , err := base64 .StdEncoding .DecodeString (* contents .Content )
201203 if err != nil {
202- return "" , fmt .Errorf ("GetBookmarks: StdEncoding.DecodeString: %w" , err )
204+ return "" , "" , fmt .Errorf ("GetBookmarks: StdEncoding.DecodeString: %w" , err )
205+ }
206+ sha := ""
207+ if contents .SHA != nil {
208+ sha = * contents .SHA
203209 }
204- return string (s ), nil
210+ return string (s ), sha , nil
205211}
0 commit comments