netlify / open-api

Open API specification of Netlify's API
https://open-api.netlify.com/#/default
MIT License
261 stars 84 forks source link

Site doesn't respond to event from webhook when created using createSite #143

Open mojavelinux opened 5 years ago

mojavelinux commented 5 years ago

When I create a site with a linked GitLab repository using the createSite operation, the site doesn't respond to incoming events (push or merge request) from the webhook (https://api.netlify.com/hooks/gitlab).

Here's the API call I'm using to create the site (which contains a netlify.toml file):

const Netlify = require('netlify')
const client = new Netlify(process.env.NETLIFY_TOKEN)

;(async () => {
  const { id: deployKeyId } = await client.createDeployKey()
  const site = await client.createSite({
    body: {
      repo: {
        provider: 'gitlab',
        deploy_key_id: deployKeyId,
        repo_path: 'group/repo-name',
        repo_branch: 'master',
      }
    }
  })
})()

If I click "Edit settings", then "Link to a different repository", the site starts responding to events from the webhook. However, there doesn't seem to be any change in the site settings (at least as reported by the API).

Is the createSite API not capable of setting up a site with CI properly? If so, what setting am I missing?

I also noticed that even if the repository is public, and I set the public_repo property to true, Netlify still reports the repository as private (as indicated by the lock icon next to the repository URL on the Build Settings page and confirmed by the API).

mojavelinux commented 5 years ago

I also find it questionable that all the docs for using createSite with a repo all say "it's a bit complicated to create a repo object, please contact support". Come on. This operation is central to the API and automation. Please document it properly. And it's not that complicated, as my example above reveals. We just need it to actually work.

bcomnes commented 5 years ago

We have improvements coming to the repos/hooks setup process that have been recently launched that need to be documented. I'll give this a deeper read during the week.

mojavelinux commented 5 years ago

That's great to hear, Bret. I'm ready to help test. Just call on me. πŸ™‹β€β™‚οΈ

bcomnes commented 5 years ago

@mojavelinux I have not forgotten about this, but I haven't addressed it yet. Sorry for the wait.

mojavelinux commented 5 years ago

Thanks for the update, Bret!

chmelevskij commented 5 years ago

Is there any updates on this or ways how to help?

prestonlimlianjie commented 5 years ago

@bcomnes Any updates on this?

prestonlimlianjie commented 5 years ago

Actually I just gave it a shot @chmelevskij and it works. Here are the steps I took (I'm using GitHub as my provider).

  1. Obtain GitHub repo ID
  2. Create a Netlify deploy key
  3. Add the Netlify deploy key (public key) to the GitHub repo
  4. Call the createSite API
chmelevskij commented 5 years ago

@prestonlimlianjie as specified in the issue description, it works just fine with Github, but fails with Gitlab.

peabnuts123 commented 5 years ago

I am seeing this behaviour with GitHub as far as I can tell. I am using Terraform so maybe there's something in there / user-error, but it seems to be as-described. The project sets up and runs a deployment at-first, but then changes to the upstream repository are not picked up by netlify and subsequent deployments are not triggered.

ehmicky commented 5 years ago

Hi @mojavelinux @chmelevskij @prestonlimlianjie @peabnuts123. First of all, sorry for the late response.

The repositories webhooks cannot be enabled from the Netlify API. The best way to enable them is either to:

We understand being able to setup repositories webhooks via the Netlify API would be very useful. Unfortunately we cannot provide this feature at the moment for security reasons. The incoming webhooks are setup by the Netlify web app using the GitHub/GitLab/Bitbucket API under the hood. This requires credentials that are obtained from your browser and would be more problematic to do via the Netlify API while maintaining security compliance.

We understand though that this is suboptimal if you're trying to programmatically create Sites. So I've created a request that you can follow here to allow performing this action via our JavaScript client. Would this work for you?

chmelevskij commented 5 years ago

I think most of the people end up on this issue following the terraform provider https://github.com/terraform-providers/terraform-provider-netlify/issues/14

It's quite interesting that it does work for github but not gitlab.

Wouldn't js client use the createSite operation as well then and have the same issue?

ehmicky commented 5 years ago

At the moment our CLI is opening a browser window to ask for the OAuth credentials for GitHub. I believe for GitLab and Bitbucket this requires some manual setup though at the moment. That being said, we do want to decrease as much setup as possible for any Git providers in the future, to make it as developer-friendly as possible.

peabnuts123 commented 5 years ago

The reason I come to this issue is mostly that the behaviour seems bugged, rather than unimplemented. Note that I am still using the Terraform provider, so there is still some room for error in that fact. Upon creating a site, a connection to GitHub is made and added to the site, and a build is triggered (and succeeds), however, no further upstream changes trigger that build. However, visiting the website in a browser looks like the connection is set up and working correctly. If I manually remove the connection and re-add it using the browser, the resultant config looks the same, however, upstream changes are correctly registered subsequently. I could understand if the Netlify provider in Terraform was not actually able to set up the connection to GitHub, and then I had to log in manually and set that up myself, but seems like the application is created in a strange, bugged state.

ehmicky commented 5 years ago

@peabnuts123 we might have a bug there that shows the GitHub connection as setup in the UI even though it is not. However as far as I understand it the GitHub connection is not supposed to be setup when following those steps.

chmelevskij commented 5 years ago

But it even works with terraform github πŸ˜† Where it doesn't for gitlab.