Relink repository via the API

Hi, we are managing a lot of sites via the Netlify API and we’ve recently encountered problems when renaming repositories after a site is created. Builds will consistently fail with the following message:

Failed during stage 'preparing repo': error checking for ref: Warning: Permanently added 'github.com' (ECDSA) to the list of known hosts.

git@github.com: Permission denied (publickey).

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
: exit status 128

I’ve tried using the updateSite operation defined in the Open API spec, but it doesn’t work, I still see the old repo name in the settings. I’m including in the request the repo id, name, and a newly generated deploy key.

The site name is framework-example-site.netlify.app.
Full deploy log:

11:50:34 AM: Build ready to start
11:50:36 AM: build-image version: 73def8bb10593b9b818f44989a75ea508018ccb7 (focal)
11:50:36 AM: build-image tag: v4.5.2
11:50:36 AM: buildbot version: 007e3cee9690c5dd1ea3d0f59b0390ab3a4afae2
11:50:36 AM: Building without cache
11:50:36 AM: Starting to prepare the repo for build
11:50:36 AM: User git error while checking for ref refs/heads/main
11:50:36 AM: Creating deploy upload records
11:50:37 AM: Failing build: Failed to prepare repo
11:50:37 AM: Failed during stage 'preparing repo': error checking for ref: Warning: Permanently added 'github.com' (ECDSA) to the list of known hosts.

git@github.com: Permission denied (publickey).

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
: exit status 128
11:50:37 AM: Finished processing build request in 867.302948ms

I need a solution that involves the API, we cannot support doing this via the UI.

Thanks!

hi there gonzalo, we haven’t forgotten about you! we will respond to this as soon as we get a chance to. thank you for your patience.

Hey @GonzaloHirschToptal,

Does this help:

@hrishikesh I saw the general post you sent and changed my implementation to include a newly generated deploy key and I still got this error. I just tried via the UI to link the new repository and all I see is that it basically sends a PUT to https://api.netlify.com/api/v1/sites/SITE_ID, but on the OpenAPI spec the only way to edit the site is via a PATCH request. So that leads me to believe that the spec is outdated. Is there a newer version of the docs that fits this?

Apart from that, we use the JS client for Netlify, which I understand that only works with the methods defined in that spec. We have implemented a client of our own just for things that are not defined on that spec, but if there’s an updated version of the spec, just to be able to see parameters and expected format for the PUT method, that’d be great.

Yes, the Spec is definitely outdated, and the JSClient and the spec are in sync. Unfortunately, there’s no newer or up to date version and the UI that Netlify uses is your only source of truth when it comes to finding the latest endpoint.

About the actual error, I’d try to get some more info on how to do what you need.

@hrishikesh thanks for looking for more info. I’ll definitely be trying the PUT via our custom client in a few.

I tried using the PUT request, and it seems like it’s working ok now. We need to test it further, but looks like it worked. Thanks for the help @hrishikesh

1 Like

Glad to hear it is working now, @GonzaloHirschToptal :netliconfetti: Please follow up here should that change.