Skip to content

Unable to update existing file in repository - Error 409 #2707

Description

@duckt14

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions