Hi, I have a problem to updating existing file in repository.
I've created a function to obtain the last SHA from the file that I want to update:
func getSHAFile(ctx context.Context, accessToken string) string {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.CommitsListOptions{
Path: *github.String("update/ciao.txt"),
}
commits, _, _ := client.Repositories.ListCommits(ctx, "duckt14", "Pippo", opt)
sha := commits[len(commits)-1].GetSHA()
return sha
}
Then, I have this function to update the file:
func updateFile(ctx context.Context, accessToken string, file []byte, author string, time time.Time, email string, path string, sha string) error {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.RepositoryContentFileOptions{
Author: &github.CommitAuthor{
Name: &author,
Date: &time,
Email: &email,
},
Message: github.String("Updating test"),
Content: file,
SHA: &sha,
}
filepath := "update/ciao.txt"
_, _, err := client.Repositories.UpdateFile(ctx, "duckt14", "Pippo", filepath, opt)
return err
}
What succeed when I play the code is this error (409):
2023/03/14 17:25:25 PUT https://api.github.com/repos/duckt14/Pippo/contents/update/ciao.txt: 409 update/ciao.txt does not match c902b7ebed9a5f513fb5a796327ddcd7c7482337 []
I found this about the Error 409: https://github.com/PyGithub/PyGithub/issues/1815#issuecomment-805473881
Are there a solution for resolve with this error or any alternative solutions for updating a file into repository? 😕
Enrico
Hi, I have a problem to updating existing file in repository.
I've created a function to obtain the last SHA from the file that I want to update:
Then, I have this function to update the file:
What succeed when I play the code is this error (409):
2023/03/14 17:25:25 PUT https://api.github.com/repos/duckt14/Pippo/contents/update/ciao.txt: 409 update/ciao.txt does not match c902b7ebed9a5f513fb5a796327ddcd7c7482337 []I found this about the Error 409: https://github.com/PyGithub/PyGithub/issues/1815#issuecomment-805473881
Are there a solution for resolve with this error or any alternative solutions for updating a file into repository? 😕
Enrico