├── LICENSE ├── README.md ├── app.yaml ├── nopr.go ├── static ├── favicon.ico ├── home.html └── style.css └── templates.go /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Pull Request Rejection Bot 2 | 3 | Some projects on GitHub don't accept GitHub Pull Requests. Maybe they have their own contribution processes. Maybe they hate freedom. Either way, GitHub doesn't provide a way to disable pull requests officially. 4 | 5 | So I wrote this. 6 | 7 | Using this tool, you can effectively disable pull requests for your repo on GitHub. When pull requests are disabled, any time a new one is opened it will immediately be closed by the bot. 8 | 9 | Sound fun? [Let's get started](http://nopullrequests.com/user) 10 | 11 | (Ironically, this project *does* accept pull requests...) 12 | 13 | ---------- 14 | 15 | License 16 | ----- 17 | 18 | Copyright 2015 Jason Hall 19 | 20 | Licensed under the Apache License, Version 2.0 (the "License"); 21 | you may not use this file except in compliance with the License. 22 | You may obtain a copy of the License at 23 | 24 | http://www.apache.org/licenses/LICENSE-2.0 25 | 26 | Unless required by applicable law or agreed to in writing, software 27 | distributed under the License is distributed on an "AS IS" BASIS, 28 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29 | See the License for the specific language governing permissions and 30 | limitations under the License. 31 | -------------------------------------------------------------------------------- /app.yaml: -------------------------------------------------------------------------------- 1 | application: nopullrequests 2 | version: 1 3 | runtime: go 4 | api_version: go1 5 | 6 | handlers: 7 | - url: /static 8 | static_dir: static 9 | 10 | - url: /favicon.ico 11 | static_files: static/favicon.ico 12 | upload: static/favicon.ico 13 | 14 | - url: / 15 | static_files: static/home.html 16 | upload: static/home.html 17 | 18 | - url: /.* 19 | script: _go_app 20 | -------------------------------------------------------------------------------- /nopr.go: -------------------------------------------------------------------------------- 1 | // TODO: allow users to configure behavior: 2 | // - whether to close the PR or add a status (closing hides statuses) 3 | // - whether to comment on the PR before closing 4 | // - custom text to use when closing 5 | // TODO: use appengine-value to store client secret 6 | // TODO: use gorilla sessions instead of Google auth 7 | // TODO: xsrf everywhere 8 | 9 | package nopr 10 | 11 | import ( 12 | "encoding/json" 13 | "fmt" 14 | "net/http" 15 | "strings" 16 | 17 | "appengine" 18 | "appengine/datastore" 19 | "appengine/urlfetch" 20 | "appengine/user" 21 | 22 | "github.com/google/go-github/github" 23 | ) 24 | 25 | const ( 26 | // TODO: store these more securely (and revoke these when you do!) 27 | clientID = "350be49c3c1988aac719" 28 | clientSecret = "f14c9383c4b8964781ea4acdd881946b1dfed488" 29 | redirectURLPath = "/oauthcallback" 30 | ) 31 | 32 | var scopes = strings.Join([]string{ 33 | "user:email", // permission to get basic information about the user 34 | "public_repo", // permission to close PRs 35 | "admin:repo_hook", // permission to add/delete webhooks 36 | // TODO: ask for this when we're not just closing the PR 37 | // "repo:status", // permission to add statuses to commits 38 | }, ",") 39 | 40 | func init() { 41 | http.HandleFunc("/start", startHandler) 42 | http.HandleFunc(redirectURLPath, oauthHandler) 43 | http.HandleFunc("/user", userHandler) 44 | http.HandleFunc("/enable/", enableHandler) 45 | http.HandleFunc("/disable/", disableHandler) 46 | http.HandleFunc("/revoke", revokeHandler) 47 | http.HandleFunc("/hook", webhookHandler) 48 | } 49 | 50 | func startHandler(w http.ResponseWriter, r *http.Request) { 51 | ctx := appengine.NewContext(r) 52 | u := user.Current(ctx) 53 | if u == nil { 54 | ctx.Infof("not logged in, redirecting...") 55 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 56 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 57 | return 58 | } 59 | 60 | ctx.Infof("starting oauth...") 61 | redirectURL := fmt.Sprintf("https://%s.appspot.com", appengine.AppID(ctx)) + redirectURLPath 62 | url := fmt.Sprintf("https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s", 63 | clientID, redirectURL, scopes) 64 | http.Redirect(w, r, url, http.StatusSeeOther) 65 | } 66 | 67 | func renderError(w http.ResponseWriter, msg string) { 68 | w.WriteHeader(http.StatusInternalServerError) 69 | errorTmpl.Execute(w, msg) 70 | } 71 | 72 | func oauthHandler(w http.ResponseWriter, r *http.Request) { 73 | ctx := appengine.NewContext(r) 74 | code := r.FormValue("code") 75 | if code == "" { 76 | ctx.Errorf("no code, going to start") 77 | http.Redirect(w, r, "/start", http.StatusSeeOther) 78 | return 79 | } 80 | 81 | u := user.Current(ctx) 82 | if u == nil { 83 | ctx.Infof("not logged in, redirecting...") 84 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 85 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 86 | return 87 | } 88 | 89 | tok, err := getAccessToken(ctx, code) 90 | if err != nil { 91 | ctx.Errorf("getting access token: %v", err) 92 | renderError(w, "Error getting access token") 93 | return 94 | } 95 | 96 | ghu, _, err := newClient(ctx, tok).Users.Get("") 97 | if err != nil { 98 | ctx.Errorf("getting user: %v", err) 99 | renderError(w, "Error getting user") 100 | return 101 | } 102 | 103 | if err := PutUser(ctx, User{ 104 | GoogleUserID: u.ID, 105 | GitHubUserID: *ghu.ID, 106 | GitHubToken: tok, 107 | }); err != nil { 108 | ctx.Errorf("put user: %v", err) 109 | renderError(w, "Error writing user entry") 110 | return 111 | } 112 | http.Redirect(w, r, "/user", http.StatusSeeOther) 113 | } 114 | 115 | func getAccessToken(ctx appengine.Context, code string) (string, error) { 116 | client := urlfetch.Client(ctx) 117 | url := fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", 118 | clientID, clientSecret, code) 119 | req, err := http.NewRequest("POST", url, nil) 120 | req.Header.Set("Accept", "application/json") 121 | resp, err := client.Do(req) 122 | if err != nil { 123 | ctx.Errorf("exchanging code: %v", err) 124 | return "", err 125 | } 126 | defer resp.Body.Close() 127 | var b struct { 128 | AccessToken string `json:"access_token"` 129 | } 130 | if err := json.NewDecoder(resp.Body).Decode(&b); err != nil { 131 | ctx.Errorf("decoding json: %v", err) 132 | return "", err 133 | } 134 | return b.AccessToken, nil 135 | } 136 | 137 | func newClient(ctx appengine.Context, tok string) *github.Client { 138 | return github.NewClient(&http.Client{Transport: transport{ctx, tok}}) 139 | } 140 | 141 | type transport struct { 142 | ctx appengine.Context 143 | tok string 144 | } 145 | 146 | func (t transport) RoundTrip(req *http.Request) (*http.Response, error) { 147 | req.Header.Set("Authorization", "token "+t.tok) 148 | return urlfetch.Client(t.ctx).Do(req) 149 | } 150 | 151 | type User struct { 152 | GoogleUserID string 153 | GitHubUserID int 154 | GitHubToken string 155 | } 156 | 157 | func PutUser(ctx appengine.Context, u User) error { 158 | k := datastore.NewKey(ctx, "User", u.GoogleUserID, 0, nil) 159 | _, err := datastore.Put(ctx, k, &u) 160 | return err 161 | } 162 | 163 | func GetUser(ctx appengine.Context, id string) *User { 164 | k := datastore.NewKey(ctx, "User", id, 0, nil) 165 | var u User 166 | if err := datastore.Get(ctx, k, &u); err == datastore.ErrNoSuchEntity { 167 | return nil 168 | } else if err != nil { 169 | ctx.Errorf("getting user: %v", err) 170 | return nil 171 | } 172 | return &u 173 | } 174 | 175 | func DeleteUser(ctx appengine.Context, userID string) error { 176 | return datastore.Delete(ctx, datastore.NewKey(ctx, "User", userID, 0, nil)) 177 | } 178 | 179 | func userHandler(w http.ResponseWriter, r *http.Request) { 180 | ctx := appengine.NewContext(r) 181 | uu := user.Current(ctx) 182 | if uu == nil { 183 | ctx.Infof("not logged in, redirecting...") 184 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 185 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 186 | return 187 | } 188 | u := GetUser(ctx, uu.ID) 189 | if u == nil { 190 | ctx.Infof("unknown user, going to /start") 191 | http.Redirect(w, r, "/start", http.StatusSeeOther) 192 | return 193 | } 194 | 195 | repos, _, err := newClient(ctx, u.GitHubToken).Repositories.List("", &github.RepositoryListOptions{ 196 | Type: "admin", 197 | }) 198 | if err != nil { 199 | ctx.Errorf("listing repos: %v", err) 200 | renderError(w, "Error listing repos") 201 | return 202 | } 203 | 204 | type data struct { 205 | Repo github.Repository 206 | Disabled bool 207 | } 208 | d := []data{} 209 | 210 | keys := []*datastore.Key{} 211 | for _, r := range repos { 212 | keys = append(keys, datastore.NewKey(ctx, "Repo", *r.FullName, 0, nil)) 213 | } 214 | repoEntities := make([]Repo, len(keys)) 215 | if err := datastore.GetMulti(ctx, keys, repoEntities); err != nil { 216 | if me, ok := err.(appengine.MultiError); ok { 217 | for i, e := range me { 218 | var disabled = e == nil 219 | d = append(d, data{Repo: repos[i], Disabled: disabled}) 220 | } 221 | } else { 222 | ctx.Errorf("getmulti: %v", err) 223 | renderError(w, "Error retrieving repos") 224 | return 225 | } 226 | } else { 227 | // all repos are disabled 228 | for _, r := range repos { 229 | d = append(d, data{Repo: r, Disabled: true}) 230 | } 231 | } 232 | 233 | if err := userTmpl.Execute(w, d); err != nil { 234 | ctx.Errorf("executing template: %v", err) 235 | } 236 | } 237 | 238 | type Repo struct { 239 | FullName string // e.g., MyUser/foo-bar 240 | UserID string // User key to use to close PRs 241 | WebhookID int // Used to delete the hook 242 | } 243 | 244 | func (r Repo) Split() (string, string) { 245 | parts := strings.Split(r.FullName, "/") 246 | if len(parts) < 2 { 247 | panic("invalid full name: " + r.FullName) 248 | } 249 | return parts[0], parts[1] 250 | } 251 | 252 | func PutRepo(ctx appengine.Context, r Repo) error { 253 | k := datastore.NewKey(ctx, "Repo", r.FullName, 0, nil) 254 | _, err := datastore.Put(ctx, k, &r) 255 | return err 256 | } 257 | 258 | func GetRepo(ctx appengine.Context, fn string) *Repo { 259 | k := datastore.NewKey(ctx, "Repo", fn, 0, nil) 260 | var r Repo 261 | if err := datastore.Get(ctx, k, &r); err == datastore.ErrNoSuchEntity { 262 | return nil 263 | } else if err != nil { 264 | ctx.Errorf("getting repo: %v", err) 265 | return nil 266 | } 267 | return &r 268 | } 269 | 270 | func DeleteRepo(ctx appengine.Context, fn string) error { 271 | return datastore.Delete(ctx, datastore.NewKey(ctx, "Repo", fn, 0, nil)) 272 | } 273 | 274 | func disableHandler(w http.ResponseWriter, r *http.Request) { 275 | if r.Method != "POST" { 276 | return 277 | } 278 | 279 | ctx := appengine.NewContext(r) 280 | uu := user.Current(ctx) 281 | if uu == nil { 282 | ctx.Infof("not logged in, redirecting...") 283 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 284 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 285 | return 286 | } 287 | u := GetUser(ctx, uu.ID) 288 | if u == nil { 289 | ctx.Infof("unknown user, going to /start") 290 | http.Redirect(w, r, "/start", http.StatusSeeOther) 291 | return 292 | } 293 | // TODO: check that the user is an admin on the repo 294 | 295 | fullName := r.URL.Path[len("/disable/"):] 296 | 297 | ghUser, ghRepo := Repo{FullName: fullName}.Split() 298 | hook, _, err := newClient(ctx, u.GitHubToken).Repositories.CreateHook(ghUser, ghRepo, &github.Hook{ 299 | Name: github.String("web"), 300 | Events: []string{"pull_request"}, 301 | Config: map[string]interface{}{ 302 | "content_type": "json", 303 | "url": fmt.Sprintf("https://%s.appspot.com/hook", appengine.AppID(ctx)), 304 | }, 305 | }) 306 | if err != nil { 307 | ctx.Errorf("creating hook: %v", err) 308 | renderError(w, "Error creating webhook") 309 | return 310 | } 311 | 312 | if err := PutRepo(ctx, Repo{ 313 | FullName: fullName, 314 | UserID: u.GoogleUserID, 315 | WebhookID: *hook.ID, 316 | }); err != nil { 317 | ctx.Errorf("put repo: %v", err) 318 | renderError(w, "Error writing repo entry") 319 | return 320 | } 321 | http.Redirect(w, r, "/user", http.StatusSeeOther) 322 | } 323 | 324 | func enableHandler(w http.ResponseWriter, r *http.Request) { 325 | if r.Method != "POST" { 326 | return 327 | } 328 | 329 | ctx := appengine.NewContext(r) 330 | uu := user.Current(ctx) 331 | if uu == nil { 332 | ctx.Infof("not logged in, redirecting...") 333 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 334 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 335 | return 336 | } 337 | u := GetUser(ctx, uu.ID) 338 | if u == nil { 339 | ctx.Infof("unknown user, going to /start") 340 | http.Redirect(w, r, "/start", http.StatusSeeOther) 341 | return 342 | } 343 | // TODO: check that the user is an admin on the repo 344 | 345 | fullName := r.URL.Path[len("/enable/"):] 346 | 347 | repo := GetRepo(ctx, fullName) 348 | if repo == nil { 349 | http.Error(w, "repo not found", http.StatusNotFound) 350 | return 351 | } 352 | 353 | ghUser, ghRepo := repo.Split() 354 | if _, err := newClient(ctx, u.GitHubToken).Repositories.DeleteHook(ghUser, ghRepo, repo.WebhookID); err != nil { 355 | ctx.Errorf("delete hook: %v", err) 356 | renderError(w, "Error deleting webhook") 357 | return 358 | } 359 | if err := DeleteRepo(ctx, repo.FullName); err != nil { 360 | ctx.Errorf("delete repo: %v", err) 361 | renderError(w, "Error deleting repo entry") 362 | return 363 | } 364 | http.Redirect(w, r, "/user", http.StatusSeeOther) 365 | } 366 | 367 | func webhookHandler(w http.ResponseWriter, r *http.Request) { 368 | ctx := appengine.NewContext(r) 369 | if r.Method != "POST" { 370 | return 371 | } 372 | if r.Header.Get("X-Github-Event") != "pull_request" { 373 | return 374 | } 375 | defer r.Body.Close() 376 | var hook github.PullRequestEvent 377 | if err := json.NewDecoder(r.Body).Decode(&hook); err != nil { 378 | ctx.Errorf("decoding json: %v", err) 379 | http.Error(w, err.Error(), http.StatusInternalServerError) 380 | return 381 | } 382 | if *hook.Action != "opened" && *hook.Action != "reopened" { 383 | return 384 | } 385 | ctx.Infof("got webhook for pull request %d opened for %q (%s)", *hook.Number, *hook.Repo.FullName, *hook.PullRequest.Head.SHA) 386 | 387 | repo := GetRepo(ctx, *hook.Repo.FullName) 388 | if repo == nil { 389 | ctx.Errorf("unknown repo") 390 | // TODO: delete webhook? 391 | return 392 | } 393 | 394 | user := GetUser(ctx, repo.UserID) 395 | if user == nil { 396 | ctx.Errorf("unknown user %q", repo.UserID) 397 | // TODO: user who configured the hook has left? 398 | return 399 | } 400 | 401 | ghUser, ghRepo := repo.Split() 402 | client := newClient(ctx, user.GitHubToken) 403 | 404 | // TODO: Commit statuses are hidden when the PR is closed, and stick around 405 | // once they're reopened. Either the PR should stay open with a failed status, 406 | // and the status should be removed when PRs are re-enabled (ugh), or we can 407 | // just skip the status and comment and close. 408 | /* 409 | if _, _, err := client.Repositories.CreateStatus(ghUser, ghRepo, *hook.PullRequest.Head.SHA, &github.RepoStatus{ 410 | State: github.String("error"), 411 | TargetURL: github.String("https://nopullrequests.appspot.com"), 412 | Description: github.String("This repository has chosen not to enable pull requests."), // TODO: configurable 413 | Context: github.String("no pull requests"), 414 | }); err != nil { 415 | ctx.Errorf("failed to create status on %q: %v", *hook.PullRequest.Head.SHA, err) 416 | } 417 | */ 418 | 419 | if _, _, err := client.Issues.CreateComment(ghUser, ghRepo, *hook.Number, &github.IssueComment{ 420 | Body: github.String("This repository has chosen to disable pull requests."), // TODO: configurable 421 | }); err != nil { 422 | ctx.Errorf("failed to create comment: %v", err) 423 | } 424 | 425 | if _, _, err := client.PullRequests.Edit(ghUser, ghRepo, *hook.Number, &github.PullRequest{ 426 | State: github.String("closed"), 427 | }); err != nil { 428 | ctx.Errorf("failed to close pull request: %v", err) 429 | } 430 | } 431 | 432 | func revokeHandler(w http.ResponseWriter, r *http.Request) { 433 | if r.Method != "POST" { 434 | return 435 | } 436 | 437 | ctx := appengine.NewContext(r) 438 | uu := user.Current(ctx) 439 | if uu == nil { 440 | ctx.Infof("not logged in, redirecting...") 441 | loginURL, _ := user.LoginURL(ctx, r.URL.Path) 442 | http.Redirect(w, r, loginURL, http.StatusSeeOther) 443 | return 444 | } 445 | u := GetUser(ctx, uu.ID) 446 | if u == nil { 447 | ctx.Infof("unknown user, going to /start") 448 | http.Redirect(w, r, "/start", http.StatusSeeOther) 449 | return 450 | } 451 | 452 | client := newClient(ctx, u.GitHubToken) 453 | 454 | q := datastore.NewQuery("Repo").Filter("UserID =", uu.ID) 455 | for t := q.Run(ctx); ; { 456 | var r Repo 457 | if _, err := t.Next(&r); err == datastore.Done { 458 | break 459 | } else if err != nil { 460 | ctx.Errorf("query: %v", err) 461 | renderError(w, "Error listing repos") 462 | return 463 | } 464 | ghUser, ghRepo := r.Split() 465 | if _, err := client.Repositories.DeleteHook(ghUser, ghRepo, r.WebhookID); err != nil { 466 | ctx.Errorf("delete hook: %v", err) 467 | renderError(w, "Error deleting hook") 468 | return 469 | } 470 | if err := DeleteRepo(ctx, r.FullName); err != nil { 471 | ctx.Errorf("delete repo: %v", err) 472 | renderError(w, "Error deleting repo entry") 473 | return 474 | } 475 | } 476 | 477 | url := fmt.Sprintf("https://api.github.com/applications/%s/tokens/%s", clientID, u.GitHubToken) 478 | ctx.Debugf(url) 479 | req, _ := http.NewRequest("DELETE", url, nil) 480 | req.SetBasicAuth(clientID, clientSecret) 481 | if resp, err := urlfetch.Client(ctx).Do(req); err != nil || resp.StatusCode != http.StatusNoContent { 482 | ctx.Errorf("revoking token (%d): %v", resp.StatusCode, err) 483 | renderError(w, "Error revoking access") 484 | return 485 | } 486 | 487 | if err := DeleteUser(ctx, uu.ID); err != nil { 488 | ctx.Errorf("delete user: %v", err) 489 | renderError(w, "Error deleting user entry") 490 | return 491 | } 492 | 493 | http.Redirect(w, r, "/", http.StatusSeeOther) 494 | } 495 | -------------------------------------------------------------------------------- /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imjasonh/nopullrequests/1023bf503752e0bc39787009d3baa6b0b5d54f43/static/favicon.ico -------------------------------------------------------------------------------- /static/home.html: -------------------------------------------------------------------------------- 1 | 2 | No Pull Requests 3 | 4 | 5 |
6 |

7 | GitHub Pull Request Rejection Bot

8 |

Some projects on GitHub don't accept GitHub Pull Requests. Maybe they have their own contribution processes. Maybe they hate freedom. Either way, GitHub doesn't provide a way to disable pull requests officially.

9 | 10 |

So I wrote this.

11 | 12 |

Using this tool, you can effectively disable pull requests for your repo on GitHub. When pull requests are disabled, any time a new one is opened it will immediately be closed by the bot. 13 | 14 |

Sound fun? 15 |

18 | 19 |
This project is not affiliated with GitHub.com. 20 | 21 | Fork me on GitHub 22 | 23 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font: 13px/1.4 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; 3 | text-align: center; 4 | } 5 | a { 6 | color: #4183c4; 7 | text-decoration: none; 8 | } 9 | a:hover { 10 | text-decoration: underline; 11 | } 12 | p, ul { 13 | color: #333; 14 | font-size: 16px; 15 | } 16 | ul { 17 | margin-right: auto; 18 | margin-left: auto; 19 | text-align: left; 20 | list-style-type: none; 21 | width: 600px; 22 | border: 1px solid #d8d8d8; 23 | border-bottom: 0; 24 | padding: 0; 25 | } 26 | li { 27 | height: 32px; 28 | border-bottom: 1px solid #d8d8d8; 29 | padding-left: 20px; 30 | position: relative; 31 | padding-top: 12px; 32 | } 33 | div#container { 34 | width: 800px; 35 | margin-right: auto; 36 | margin-left: auto; 37 | margin-top: 40px; 38 | background-color: #fafafa; 39 | border: 1px solid #e5e5e5; 40 | box-shadow: inset 0 0 10px rgba(0,0,0,0.05); 41 | border-radius: 3px; 42 | padding: 60px 60px 120px; 43 | } 44 | li button { 45 | position: absolute; 46 | right: 20px; 47 | top: 10px; 48 | } 49 | button { 50 | vertical-align: middle; 51 | color: #fff; 52 | display: inline-block; 53 | padding: 2px 10px; 54 | font-size: 13px; 55 | font-weight: bold; 56 | line-height: 20px; 57 | white-space: nowrap; 58 | vertical-align: middle; 59 | cursor: pointer; 60 | background-repeat: repeat-x; 61 | border: 1px solid; 62 | border-radius: 3px; 63 | -webkit-user-select: none; 64 | -moz-user-select: none; 65 | -ms-user-select: none; 66 | user-select: none; 67 | -webkit-appearance: none; 68 | } 69 | button#enable { 70 | background-color: #60b044; 71 | background-image: linear-gradient(#8add6d, #60b044); 72 | border-color: #5ca941; 73 | } 74 | button#disable { 75 | background-color:#b33630; 76 | background-image:-webkit-linear-gradient(#dc5f59, #b33630); 77 | border-color: #cd504a; 78 | } 79 | -------------------------------------------------------------------------------- /templates.go: -------------------------------------------------------------------------------- 1 | package nopr 2 | 3 | import "html/template" 4 | 5 | var userTmpl = template.Must(template.New("user").Parse(` 6 | No Pull Requests 7 | 8 | 9 |
10 |

11 | GitHub Pull Request Rejection Bot

12 | 31 |

« Home

32 |
This project is not affiliated with GitHub.com. 33 | 34 | 35 |
36 | Revoke access 37 |
38 | 39 | Fork me on GitHub 40 | `)) 41 | 42 | var errorTmpl = template.Must(template.New("error").Parse(` 43 | Oh noes!!1! 44 | 45 |
46 |

Oh noes!!1!

47 |

Something went wrong.

48 |

{{.}}

49 |

« Home

50 |
This project is not affiliated with GitHub.com. 51 | 52 | Fork me on GitHub 53 | `)) 54 | --------------------------------------------------------------------------------