Integrating with the GitHub API

Integrating with the GitHub API

This is a talk I gave at GitHub Universe for users who want to jump into the GitHub API. This talk covers creating organization webhooks, commit statuses, deployments, and then tying it all together.

Dd18bb36fa5f06e45843ff8de33b793e?s=128

Kyle Daigle

October 02, 2015
Tweet

Transcript

  1. How people build software Integrating with the GitHub API @kdaigle

    GitHub Universe
  2. How people build software Kyle Daigle Senior Software Engineer Platform,

    API & Webhooks @kdaigle 2 !
  3. How people build software Things we built & use Sampling

    of tools & integrations that various teams at GitHub have built and use daily 3 "
  4. How people build software How to start a fire… 4

    # $ % ! AUTOMATION Lower friction PLATFORM Use Our Own APIs HUBOT Chat-Ops
  5. How people build software Here’s what I’ll cover… 5 !

    • Get started with the GitHub API • Automate repository setup with organization webhooks • Track test output, code coverage, and more with commit statuses • Track deployments of pull requests across multiple environments and deployment schemes • Create tracking issues from Nagios alerts that include diagnostic information, and alert engineers with team mentions • Answer your questions at the end
  6. How people build software Getting started with the API Grab

    your API keys and lets go 6 &
  7. How people build software https://developer.github.com Lots more documentation & information

    that I won’t be able to cover in this talk 7 '
  8. How people build software Getting started with the API 8

    &
  9. How people build software Getting started with the API 9

    &
  10. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Make a simple API call with your token 10 ! curl -H "Authorization: token abc123" https://api.github/zen
  11. How people build software GitHub supported client libraries 11 (

    ) * ! RUBY github/octokit.rb OBJECTIVE-C github/octokit.objc .NET github/octokit.net
  12. How people build software Organization Webhooks Automate setup and integration

    tasks with organization webhooks 12 $
  13. How people build software How do webhooks work?

  14. How people build software What is a GitHub webhook? 14

    ! GitHub Your
 Server Pull Request is created Issue comment created Repository created … and more HTTP POST Update the GitHub API Trigger a Deployment Update your systems
  15. How people build software How do organization webhooks work? 15

    $ + , ! WEBHOOK Setup a webhook
 on the Organization EVENT Do that thing
 in your org PAYLOAD Act on the payload
 and setup once
  16. How people build software Repository created payload 16 !

  17. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Setup a webhook for your entire organization 17 ! //////////////////////////////////////
 //Put the cipher in encryption mode
 desCipher.init(Cipher.ENCRYPT_MODE, desKey);
 
 //Encrypt and output the base64 data
 byte[] clearText = message1.getBytes();
 byte[] encryptedBytes = desCipher.doFinal(clearText);
 BASE64Encoder b64e = new sun.misc.BASE64Encoder();
 String base64Encrypted = b64e.encode(encryptedBytes);
 System.out.println("Encrypted text: " + base64Encrypted);
  18. How people build software Receive repository & organization events 18

    !
  19. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Add a team to every new repository 19 ! p o s t " / post "/webhooks" do team_id = find_relevant_team_id repository = payload["repository"]["full_name"] client.add_team_repository(team_id, repository) notify_external_compliance_system end
  20. How people build software Overloading Statuses Use commit statuses for

    more than just test running 20 0
  21. How people build software How do statuses work?

  22. How people build software Pull Request with build status 22

    !
  23. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Create a commit status 23 ! p o s t " / post "/webhooks" do repository = payload["repository"]["full_name"] sha = payload["repository"]["sha"] options = { context: "universe", target_url: “https://universe.example.com/sha/#{sha}”, description: "We are building the application now..." } client.create_status(repository, sha, "pending", options) end
  24. How people build software Pending commit status 24 !

  25. How people build software Failed commit status 25 !

  26. How people build software Successful commit status 26 !

  27. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 How to overload commit statuses 27 ! //////////////////////////////////////
 //Put the cipher in encryption mode
 desCipher.init(Cipher.ENCRYPT_MODE, desKey);
 
 //Encrypt and output the base64 data
 byte[] clearText = message1.getBytes();
 byte[] encryptedBytes = desCipher.doFinal(clearText);
 BASE64Encoder b64e = new sun.misc.BASE64Encoder();
 String base64Encrypted = b64e.encode(encryptedBytes);
 System.out.println("Encrypted text: " + base64Encrypted);
  28. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Build your project on every push 28 ! p o s t " / post "/webhooks" do # ... client.create_status(repository, sha, "pending", options) `script/bootstrap` if $!.exitstatus == 0 # Success client.create_status(repository, sha, "success", options) else client.create_status(repository, sha, "failure", options) end rescue client.create_status(repository, sha, "error", options) end
  29. How people build software Deploying Everything Track and automate deployments

    across multiple build types 29 "
  30. How people build software How do deployments work?

  31. How people build software Deployments are shown in Pull Requests

    31 !
  32. How people build software How to handle a Deployment Event

    32 ! GitHub Service HTTP POST Start deploying the code GitHub API Report back success/failure with logs GitHub API Trigger a Deployment
 via your systems
  33. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Deploy & build your projects 33 ! r e p o s repository = payload["repository"]["full_name"] ref = payload["deployment"]["ref"] options = { task: "deploy", auto_merge: true, required_contexts: ["universe"], payload: { setting: 1234 }, environment: "staging", description: "Deploying the demo to staging" } client.create_deployment(repository, ref, options)
  34. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Update your deployment with its progress 34 ! p o s t " / post "/webhooks" do repository = payload["repository"]["full_name"] sha = payload["deployment"]["sha"] deployment_url = payload["deployment"]["url"] `script/deploy` # Or this could be the payload[“deployment”][“task”] if success client.create_deployment_status(deployment_url, "success") else client.create_deployment_status(deployment_url, "failure") end end
  35. How people build software Or use an existing integration 35

    ! https://github.com/integrations
  36. How people build software Nagios & GitHub How to use

    GitHub Issues to create better collaboration around alerts 36 1
  37. How people build software “Nines” issue automatically created 37 !

  38. How people build software Add graphite graphs and more 38

    !
  39. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Nagios command to shell script 39 ! d e f i n e define command { name host-ninesapp-for-metrics command_name host-ninesapp-for-metrics command_line /usr/bin/printf "%b" "$LONGHOSTOUTPUT$" | /usr/bin/env NAGIOS_SEVERITY="$_HOSTSEVERITY$" NAGIOS_NOTIFICATION_TYPE="$NOTIFICATIONTYPE$" NAGIOS_HOSTNAME="$HOSTNAME$" NAGIOS_STATE="$HOSTSTATE$" NAGIOS_LONGDATETIME="$LONGDATETIME$" NAGIOS_OUTPUT="$HOSTOUTPUT$" NAGIOS_ACKCOMMENT="$HOSTACKCOMMENT$" NAGIOS_PROBLEMID="$HOSTPROBLEMID$" NAGIOS_LASTPROBLEMID="$LASTHOSTPROBLEMID $" NAGIOS_NOTES="$HOSTNOTES$" NAGIOS_NOTESURL="$HOSTNOTESURL$" NAGIOS_ACTIONURL="$HOSTACTIONURL$" gh-ninesapp-update-alert -t "$NOTIFICATIONTYPE$" -a "$HOSTNAME$" -e "$HOSTSTATE$" -d "$LONGDATETIME$" - o "$HOSTOUTPUT$" -c "$HOSTACKCOMMENT$" -i "$HOSTPROBLEMID$" -l "$LASTHOSTPROBLEMID$" -n "$HOSTNOTESURL$" -z "$HOSTACTIONURL$" }
  40. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Receive the alert from shell script 40 ! c l a s s A class Api::V1::AlertsController < Api::V1::ApiController def create @alert = Alert.create(alert_params) respond_with @alert, status: 201 end end
  41. How people build software 51 52 53 54 55 56

    57 58 59 60 61 62 63 64 65 66 67 Create the issue with team mentions 41 ! d e f c r e def create_issue_from_alert client.create_issue(repo, title, body, attributes) end def body template = Erubis::Eruby.new(File.read("app/views/issues/body.md.erb")) body = template.evaluate(alert) end
  42. How people build software “Nines” issue 42 !

  43. How people build software https://developer.github.com Have a bunch more API

    questions? Check this out. 43 '
  44. How people build software Thank you! @kdaigle