Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

Create a webhook (orgs and repos)

Hi!

Some background about the application:
A simple github notification API written in Javascript, firebase is used to authenticate/login users to the API.

Now am trying to create a webhook as documented in "orgs webhook create" aswell as "repos webhook create"
Here is a code snippet trying to create a webhook for an organization.

export const createHook = (url, token) => async dispatch => {
  console.log('token: ', token) // xxxxxxxxxxxxxx
  console.log('url: ', url) // https://api.github.com/orgs/1dv023/hooks
  try {
    await fetch(url, {
      method: 'POST',
      headers: {
        Authorization: `token ${token}`
      },
      data: {
        name: 'web',
        active: true,
        events: [
          'release',
          'repository',
          'push',
          'issues',
          'organization'
        ],
        config: {
          url: 'https://webhook.site/4d8985d9-f043-44a4-8f2a-b4d9bd5e029a',
          content_type: 'json'
        }
      }
    }).then((result) => {
      console.log(result)
    })
  } catch (error) {
    console.log('error: ', error)
    dispatch(setAlert(`An error has occured!: ${error.message}`, 'danger'))
  }
}

The response from the code above:

body: ReadableStream {
locked: false
}
bodyUsed: false
headers: Headers {
<prototype>: HeadersPrototype { append: append(), delete: delete(), get: get(), … }
}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "cors"
url: "https://api.github.com/orgs/1dv021/hooks"
<prototype>: ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … }
github.js:58

Here's code snippet to create webhook for my personal repo (public), currently harded coded url in order to test.

export const createHook = (token) => dispatch => {
  console.log('token: ', token) // xxxxxxxxxxxxxx
  fetch({
    url: 'https://api.github.com/repos/MosaKasem/Developers_hangout/hooks',
    method: 'POST',
    headers: {
      Authorization: `token ${token}`
    },
    body: {
      name: 'web',
      active: true,
      events: [
        'release',
        'repository',
        'push',
        'issues',
        'issue_comment'
      ],
      config: {
        url: 'https://webhook.site/4d8985d9-f043-44a4-8f2a-b4d9bd5e029a',
        content_type: 'json',
        insecure_ssl: '1'
      }
    }
  }).then((response) => {
    console.log(response)
  }).catch((err) => {
    console.log(err)
  })
}

When I do fetch I get this response, it gives back 200 but the hook is not created.

Response
body: ReadableStream { locked: false }
bodyUsed: false
headers: Headers {  }
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3000/profile/[object%20Object]"
<prototype>: ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … }
github.js:60

When posting with axios I get:

Error: "Request failed with status code 422"


Am I missing something?
The token has these rights:
X-OAuth-Scopes: admin:org, admin:org_hook, admin:repo_hook, repo, user

 

 

Some feedback on what it could be is very appreciated.

 

 

 

1 Reply
Copilot Lvl 2
Message 2 of 2

Re: Create a webhook (orgs and repos)

I still dont know what the cause is but I used "request-promise-native" library to make the request against the API and it seemed to have worked, for now atleast.

Thanks.