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.
• 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
! 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
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
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
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
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
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
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
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