Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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.

Kyle Daigle

October 02, 2015
Tweet

More Decks by Kyle Daigle

Other Decks in Programming

Transcript

  1. How people build software Things we built & use Sampling

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

    # $ % ! AUTOMATION Lower friction PLATFORM Use Our Own APIs HUBOT Chat-Ops
  3. 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
  4. 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
  5. How people build software GitHub supported client libraries 11 (

    ) * ! RUBY github/octokit.rb OBJECTIVE-C github/octokit.objc .NET github/octokit.net
  6. 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
  7. 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
  8. 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);
  9. 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
  10. 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
  11. 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);
  12. 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
  13. 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
  14. 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)
  15. 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
  16. How people build software Nagios & GitHub How to use

    GitHub Issues to create better collaboration around alerts 36 1
  17. 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$" }
  18. 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
  19. 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