Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent that lambda webhook fails when it tries to process a installation_repositories event #2210

Closed
jgutierrezglez opened this issue Jun 29, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@jgutierrezglez
Copy link
Contributor

Hello team,

I hope you're doing well. I noticed the lambda webhook fails when it receives an installation_repositories event from GitHub. Even if in the GitHub app is configured to receive only workflow_job events - this event is still received by the application that we have created to manage our GitHub runners (our application is installed at the organizational level and any project from this organization can use it).

The error returned by the lambda in this particular scenario is the following:

ERROR [webhook:ab4e8da5-4c70-488d-a47f-f1f62cb4356c index.js:91029 Runtime.handler]
TypeError Cannot read property 'full_name' of undefined
error stack:
• index.js:84926 handle
  index.js:84926:63
• index.js:91026 githubWebhook
  index.js:91026:18

There is a real quick fix that be can be applied to the webhook lambda to prevent this issue:

https://github.com/philips-labs/terraform-aws-github-runner/blob/develop/modules/webhook/lambdas/webhook/src/webhook/handler.ts#L32

  const payload = JSON.parse(body);
  LogFields.fields.event = githubEvent;
  LogFields.fields.repository = payload.repository.full_name;
  LogFields.fields.action = payload.action;

  if (!supportedEvents.includes(githubEvent)) {
    logger.warn(`Unsupported event type.`, LogFields.print());
    return {
      statusCode: 202,
      body: `Ignoring unsupported event ${githubEvent}`,
    };
  }
 
 LogFields.fields.name = payload[githubEvent].name;
 LogFields.fields.status = payload[githubEvent].status;
 LogFields.fields.started_at = payload[githubEvent]?.started_at;

if the 4 first lines of the snipped are moved just below the if (!supportedEvents.includes(githubEvent)) line

  if (!supportedEvents.includes(githubEvent)) {
    logger.warn(`Unsupported event type.`, LogFields.print());
    return {
      statusCode: 202,
      body: `Ignoring unsupported event ${githubEvent}`,
    };
  }
  const payload = JSON.parse(body);
  LogFields.fields.event = githubEvent;
  LogFields.fields.repository = payload.repository.full_name;
  LogFields.fields.action = payload.action;
  LogFields.fields.name = payload[githubEvent].name;
  LogFields.fields.status = payload[githubEvent].status;
  LogFields.fields.started_at = payload[githubEvent]?.started_at;
   

this change fixes the issue (tested already in our runners) - as the request is discarded by this condition without making that the lambda fails.

I tried to create this PR myself and submit to you - but it seems I don't have permissions to collaborate.

Thanks,

Javier G.

@npalm
Copy link
Member

npalm commented Jul 15, 2022

Thanks for creating the issue. Would be strange if you are not allowed ot create a PR. THe repo is fully open source, no restriction on who can make a PR. Please can you try it again and submit a PR?

@npalm npalm added the bug Something isn't working label Jul 15, 2022
@jgutierrezglez
Copy link
Contributor Author

Hello @npalm

I'm trying to push my changes to a branch - it seems that my user doesn't have enough rights to do it:

git push --set-upstream origin webhook_lambda_improvement
ERROR: Permission to philips-labs/terraform-aws-github-runner.git denied to jgutierrezglez.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Cheers,

Javier G.

@rashidnhm
Copy link

@jgutierrezglez you have to fork this repository, push your changes to that, then open a PR from your fork to this repository

@npalm
Copy link
Member

npalm commented Jul 25, 2022

Omdeed the normal process in GitHub is just create a fork, push your changes to your fork (new branch or default one). Next open a PR.

@jgutierrezglez
Copy link
Contributor Author

PR created: #2288

@npalm
Copy link
Member

npalm commented Aug 9, 2022

merged

@npalm npalm closed this as completed Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants