Skip to content

Commit b736096

Browse files
authored
Merge pull request #127 from github/delete-repository-event
Add delete repository event webhook
2 parents 93a65f7 + 04f2d14 commit b736096

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
gem "sinatra"
4+
gem "octokit"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.5.0)
5+
public_suffix (~> 2.0, >= 2.0.2)
6+
faraday (0.11.0)
7+
multipart-post (>= 1.2, < 3)
8+
multipart-post (2.0.0)
9+
octokit (4.6.2)
10+
sawyer (~> 0.8.0, >= 0.5.3)
11+
public_suffix (2.0.5)
12+
rack (1.6.5)
13+
rack-protection (1.5.3)
14+
rack
15+
sawyer (0.8.1)
16+
addressable (>= 2.3.5, < 2.6)
17+
faraday (~> 0.8, < 1.0)
18+
sinatra (1.4.8)
19+
rack (~> 1.5)
20+
rack-protection (~> 1.4)
21+
tilt (>= 1.3, < 3)
22+
tilt (2.0.6)
23+
24+
PLATFORMS
25+
ruby
26+
27+
DEPENDENCIES
28+
octokit
29+
sinatra
30+
31+
BUNDLED WITH
32+
1.14.6
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# :x: Delete Repository Event
2+
3+
### :dart: Purpose
4+
5+
This Ruby server:
6+
7+
1. Listens for when a [repository is deleted](https://help.github.com/enterprise/user/articles/deleting-a-repository/) using the [`repository`](https://developer.github.com/enterprise/v3/activity/events/types/#repositoryevent) event and `deleted` action.
8+
9+
2. Creates an issue in `GITHUB_NOTIFICATION_REPOSITORY` as a notification and includes:
10+
11+
- a link to restore the repository
12+
- the delete repository payload
13+
14+
### :gear: Configuration
15+
16+
1. See the [webhooks](https://developer.github.com/webhooks/) documentation for information on how to [create webhooks](https://developer.github.com/webhooks/creating/) and [configure your server](https://developer.github.com/webhooks/configuring/).
17+
18+
2. Set the following required environment variables:
19+
20+
- `GITHUB_HOST` - the domain of the GitHub Enterprise instance. e.g. github.example.com
21+
- `GITHUB_API_TOKEN` - a [Personal Access Token](https://help.github.com/enterprise/user/articles/creating-a-personal-access-token-for-the-command-line/) that has the ability to create an issue in the notification repository
22+
- `GITHUB_NOTIFICATION_REPOSITORY` - the repository in which to create the nofication issue. e.g. github.example.com/administrative-notifications
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Hook example for notifying an administrator in a repository by creating an issue when a repository is deleted.
2+
#
3+
# Needs the following environment variables
4+
# GITHUB_HOST - the domain of the GitHub Enterprise instance. e.g. github.example.com
5+
# GITHUB_API_TOKEN - a Personal Access Token that has the ability to create an issue in the notification repository.
6+
# GITHUB_NOTIFICATION_REPOSITORY - the repository in which to create the nofication issue. e.g.
7+
#
8+
# Dependencies:
9+
# octokit - https://github.com/octokit/octokit.rb
10+
# sinatrarb - http://www.sinatrarb.com/
11+
12+
require 'octokit'
13+
require 'sinatra'
14+
require 'json'
15+
16+
enable :logging
17+
github_api_token = ENV['GITHUB_API_TOKEN']
18+
github_notification_repository = ENV['GITHUB_NOTIFICATION_REPOSITORY']
19+
github_host_fqdn = ENV['GITHUB_HOST']
20+
github_api_endpoint = "https://#{github_host_fqdn}/api/v3"
21+
22+
Octokit.configure do |c|
23+
c.api_endpoint = github_api_endpoint
24+
c.access_token = github_api_token
25+
end
26+
27+
# Needed so that the webhook setup passes
28+
post '/' do
29+
200
30+
end
31+
32+
# When receiving a webhook for repository deletion (https://developer.github.com/v3/activity/events/types/#repositoryevent)
33+
# create an issue in the `github_notification_repository` set by environment variable
34+
post '/delete-repository-event' do
35+
begin
36+
github_event = request.env['HTTP_X_GITHUB_EVENT']
37+
if github_event == "repository"
38+
request.body.rewind
39+
parsed = JSON.parse(request.body.read)
40+
action = parsed['action']
41+
42+
if action == 'deleted'
43+
# create a new issue in the repository configured above
44+
full_name = parsed['repository']['full_name']
45+
purgatory_link = "https://#{github_host_fqdn}/stafftools/users/#{parsed['repository']['owner']['login']}/purgatory"
46+
client = Octokit::Client.new
47+
client.create_issue(github_notification_repository, "Repository deleted: #{full_name}", "[Restore the repository](#{purgatory_link})\n```json\n#{JSON.pretty_generate(parsed)}\n```")
48+
49+
return 201,"Repository deleted: #{full_name}, notification created in #{github_notification_repository}"
50+
end
51+
end
52+
return 418, "No such teapot"
53+
rescue => e
54+
status 500
55+
"exception encountered #{e}"
56+
end
57+
end

0 commit comments

Comments
 (0)