forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.go
124 lines (108 loc) · 3.03 KB
/
commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/plugin/remote"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/worker"
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/shared/model"
"github.com/goji/context"
"github.com/zenazn/goji/web"
)
// GetCommitList accepts a request to retrieve a list
// of recent commits by Repo, and retur in JSON format.
//
// GET /api/repos/:host/:owner/:name/commits?limit=:limit&offset=:offset
//
func GetCommitList(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var repo = ToRepo(c)
var limit = ToLimit(r)
var offset = ToOffset(r)
commits, err := datastore.GetCommitList(ctx, repo, limit, offset)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(commits)
}
// GetCommit accepts a request to retrieve a commit
// from the datastore for the given repository, branch and
// commit hash.
//
// GET /api/repos/:host/:owner/:name/branches/:branch/commits/:commit
//
func GetCommit(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var (
branch = c.URLParams["branch"]
hash = c.URLParams["commit"]
repo = ToRepo(c)
)
commit, err := datastore.GetCommitSha(ctx, repo, branch, hash)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(commit)
}
// PostHook accepts a post-commit hook and parses the payload
// in order to trigger a build. The payload is specified to the
// remote system (ie GitHub) and will therefore get parsed by
// the appropriate remote plugin.
//
// POST /api/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit}
//
func PostCommit(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var (
branch = c.URLParams["branch"]
hash = c.URLParams["commit"]
host = c.URLParams["host"]
repo = ToRepo(c)
remote = remote.Lookup(host)
)
commit, err := datastore.GetCommitSha(ctx, repo, branch, hash)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if commit.Status == model.StatusStarted ||
commit.Status == model.StatusEnqueue {
w.WriteHeader(http.StatusConflict)
return
}
commit.Status = model.StatusEnqueue
commit.Started = 0
commit.Finished = 0
commit.Duration = 0
if err := datastore.PutCommit(ctx, commit); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
owner, err := datastore.GetUser(ctx, repo.UserID)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Request a new token and update
user_token, err := remote.GetToken(owner)
if user_token != nil {
owner.Access = user_token.AccessToken
owner.Secret = user_token.RefreshToken
owner.TokenExpiry = user_token.Expiry
datastore.PutUser(ctx, owner)
} else if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// drop the items on the queue
go worker.Do(ctx, &worker.Work{
User: owner,
Repo: repo,
Commit: commit,
Host: httputil.GetURL(r),
})
w.WriteHeader(http.StatusOK)
}