|
| 1 | +// Copyright 2020 The Gogs Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package repo |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "gopkg.in/macaron.v1" |
| 11 | + log "unknwon.dev/clog/v2" |
| 12 | + |
| 13 | + "github.com/G-Node/gogs/internal/db" |
| 14 | + "github.com/G-Node/gogs/internal/tool" |
| 15 | +) |
| 16 | + |
| 17 | +func TriggerTask(c *macaron.Context) { |
| 18 | + branch := c.Query("branch") |
| 19 | + pusherID := c.QueryInt64("pusher") |
| 20 | + secret := c.Query("secret") |
| 21 | + if branch == "" || pusherID <= 0 || secret == "" { |
| 22 | + c.Error(http.StatusBadRequest, "Incomplete branch, pusher or secret") |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + username := c.Params(":username") |
| 27 | + reponame := c.Params(":reponame") |
| 28 | + |
| 29 | + owner, err := db.Users.GetByUsername(username) |
| 30 | + if err != nil { |
| 31 | + if db.IsErrUserNotExist(err) { |
| 32 | + c.Error(http.StatusBadRequest, "Owner does not exist") |
| 33 | + } else { |
| 34 | + c.Status(http.StatusInternalServerError) |
| 35 | + log.Error("Failed to get user [name: %s]: %v", username, err) |
| 36 | + } |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // 🚨 SECURITY: No need to check existence of the repository if the client |
| 41 | + // can't even get the valid secret. Mostly likely not a legitimate request. |
| 42 | + if secret != tool.MD5(owner.Salt) { |
| 43 | + c.Error(http.StatusBadRequest, "Invalid secret") |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + repo, err := db.Repos.GetByName(owner.ID, reponame) |
| 48 | + if err != nil { |
| 49 | + if db.IsErrRepoNotExist(err) { |
| 50 | + c.Error(http.StatusBadRequest, "Repository does not exist") |
| 51 | + } else { |
| 52 | + c.Status(http.StatusInternalServerError) |
| 53 | + log.Error("Failed to get repository [owner_id: %d, name: %s]: %v", owner.ID, reponame, err) |
| 54 | + } |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + pusher, err := db.Users.GetByID(pusherID) |
| 59 | + if err != nil { |
| 60 | + if db.IsErrUserNotExist(err) { |
| 61 | + c.Error(http.StatusBadRequest, "Pusher does not exist") |
| 62 | + } else { |
| 63 | + c.Status(http.StatusInternalServerError) |
| 64 | + log.Error("Failed to get user [id: %d]: %v", pusherID, err) |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + log.Trace("TriggerTask: %s/%s@%s by %q", owner.Name, repo.Name, branch, pusher.Name) |
| 70 | + |
| 71 | + go db.HookQueue.Add(repo.ID) |
| 72 | + go db.AddTestPullRequestTask(pusher, repo.ID, branch, true) |
| 73 | + c.Status(http.StatusAccepted) |
| 74 | +} |
0 commit comments