octokit.net icon indicating copy to clipboard operation
octokit.net copied to clipboard

Push files from Azure function triggered by blobstorage fails on Exception Reference cannot be updated

Open istisan opened this issue 4 years ago • 3 comments

I use the following code on an Azure function to push files on a github repository when a new file is uploaded to a blobstorage, that trigger the function. But it doesn't work if multiple file are uploaded to blobstorage in a short time interval: only one random file is pushed to github and then the function throw an exception; in the log: Description: The process was terminated due to an unhandled exception. Exception Info: Octokit.ApiValidationException: Reference cannot be updated {"message":"Reference cannot be updated","documentation_url":"https://docs.github.com/rest/reference/git..."}. This is the code:

public static async void PushToGithub(string fileName, Stream myBlob)
    {
        // github variables
        var owner = GITHUB_USER;
        var repo = GITHUB_REPO;
        var token = GITHUB_TOKEN;

        //Create our Client
        var github = new GitHubClient(new ProductHeaderValue("GithubCommit"));
        var tokenAuth = new Credentials(token);

        github.Credentials = tokenAuth;

        var headMasterRef = "heads/master";

        // Get reference of master branch
        var masterReference = await github.Git.Reference.Get(owner, repo, headMasterRef);
        // Get the laster commit of this branch
        var latestCommit = await github.Git.Commit.Get(owner, repo, masterReference.Object.Sha);

        // For image, get image content and convert it to base64
        byte[] bytes;
        using (var memoryStream = new MemoryStream())
        {
            myBlob.Position = 0;
            myBlob.CopyTo(memoryStream);
            bytes = memoryStream.ToArray();
        }

        var pdfBase64 = Convert.ToBase64String(bytes);
        // Create blob
        var pdfBlob = new NewBlob { Encoding = EncodingType.Base64, Content = (pdfBase64) };
        var pdfBlobRef = await github.Git.Blob.Create(owner, repo, pdfBlob);

        // Create new Tree
        var nt = new NewTree { BaseTree = latestCommit.Tree.Sha };
        // Add items based on blobs
        nt.Tree.Add(new NewTreeItem { Path = fileName, Mode = "100644", Type = TreeType.Blob, Sha = pdfBlobRef.Sha });

        var newTree = await github.Git.Tree.Create(owner, repo, nt);

        // Create Commit
        var newCommit = new NewCommit("File update " + DateTime.UtcNow, newTree.Sha, masterReference.Object.Sha);
        var commit = await github.Git.Commit.Create(owner, repo, newCommit);

        // Update HEAD with the commit
        await github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha, true));
    }

How can I solve so it pushes correctly to github all the files that are uploaded on the blobstorage? Thanks in advance, Marco

istisan avatar Dec 07 '20 13:12 istisan

@istisan it's hard to say what the root cause is without a bit more context but I wonder if your master branch has some sort of protection set.

await github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha, true));

The true parameter on the ReferenceUpdate here is to not enforce a "fast-forward" update of the ref, so if the API is rejecting you with a "Reference cannot be updated" message it could be that the repository requires changes to that branch to go through a PR review workflow (or something like that).

shiftkey avatar Feb 25 '21 15:02 shiftkey

Uploading IMG_20210122_073219.jpg…

Aarojas3012a avatar Feb 26 '21 14:02 Aarojas3012a

@Aarojas3012a I can't see the image you attached. @shiftkey so do you think to set the parameter to false? How do I check if there is a sort of protection set on master branch?

istisan avatar Feb 26 '21 16:02 istisan

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

github-actions[bot] avatar Jul 25 '23 02:07 github-actions[bot]