|
1 | 1 | package repos |
2 | 2 |
|
3 | | -import ( |
4 | | - "net/http" |
5 | | - |
6 | | - "github.com/gin-gonic/gin" |
7 | | - gb "github.com/gitploy-io/gitploy/internal/server/global" |
8 | | - "github.com/gitploy-io/gitploy/model/ent" |
9 | | - "github.com/gitploy-io/gitploy/model/extent" |
10 | | - "go.uber.org/zap" |
| 3 | +type ( |
| 4 | + CommitsAPI service |
11 | 5 | ) |
12 | | - |
13 | | -func (r *Repo) ListCommits(c *gin.Context) { |
14 | | - var ( |
15 | | - branch = c.Query("branch") |
16 | | - page = c.DefaultQuery("page", "1") |
17 | | - perPage = c.DefaultQuery("per_page", "30") |
18 | | - ) |
19 | | - |
20 | | - ctx := c.Request.Context() |
21 | | - |
22 | | - uv, _ := c.Get(gb.KeyUser) |
23 | | - u := uv.(*ent.User) |
24 | | - |
25 | | - rv, _ := c.Get(KeyRepo) |
26 | | - repo := rv.(*ent.Repo) |
27 | | - |
28 | | - commits, err := r.i.ListCommits(ctx, u, repo, branch, atoi(page), atoi(perPage)) |
29 | | - if err != nil { |
30 | | - r.log.Check(gb.GetZapLogLevel(err), "Failed to list commits.").Write(zap.Error(err)) |
31 | | - gb.ResponseWithError(c, err) |
32 | | - return |
33 | | - } |
34 | | - |
35 | | - gb.Response(c, http.StatusOK, commits) |
36 | | -} |
37 | | - |
38 | | -func (r *Repo) GetCommit(c *gin.Context) { |
39 | | - var ( |
40 | | - sha = c.Param("sha") |
41 | | - ) |
42 | | - |
43 | | - ctx := c.Request.Context() |
44 | | - |
45 | | - uv, _ := c.Get(gb.KeyUser) |
46 | | - u := uv.(*ent.User) |
47 | | - |
48 | | - rv, _ := c.Get(KeyRepo) |
49 | | - repo := rv.(*ent.Repo) |
50 | | - |
51 | | - commit, err := r.i.GetCommit(ctx, u, repo, sha) |
52 | | - if err != nil { |
53 | | - r.log.Check(gb.GetZapLogLevel(err), "Failed to get the commit.").Write(zap.Error(err)) |
54 | | - gb.ResponseWithError(c, err) |
55 | | - return |
56 | | - } |
57 | | - |
58 | | - gb.Response(c, http.StatusOK, commit) |
59 | | -} |
60 | | - |
61 | | -func (r *Repo) ListStatuses(c *gin.Context) { |
62 | | - var ( |
63 | | - sha = c.Param("sha") |
64 | | - ) |
65 | | - |
66 | | - ctx := c.Request.Context() |
67 | | - |
68 | | - uv, _ := c.Get(gb.KeyUser) |
69 | | - u := uv.(*ent.User) |
70 | | - |
71 | | - rv, _ := c.Get(KeyRepo) |
72 | | - repo := rv.(*ent.Repo) |
73 | | - |
74 | | - ss, err := r.i.ListCommitStatuses(ctx, u, repo, sha) |
75 | | - if err != nil { |
76 | | - r.log.Check(gb.GetZapLogLevel(err), "Failed to list commit statuses.").Write(zap.Error(err)) |
77 | | - gb.ResponseWithError(c, err) |
78 | | - return |
79 | | - } |
80 | | - |
81 | | - gb.Response(c, http.StatusOK, map[string]interface{}{ |
82 | | - "state": mergeState(ss), |
83 | | - "statuses": ss, |
84 | | - }) |
85 | | -} |
86 | | - |
87 | | -func mergeState(ss []*extent.Status) string { |
88 | | - // The state is failure if one of them is failure. |
89 | | - for _, s := range ss { |
90 | | - if s.State == extent.StatusStateFailure || s.State == extent.StatusStateCancelled { |
91 | | - return string(extent.StatusStateFailure) |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - for _, s := range ss { |
96 | | - if s.State == extent.StatusStatePending { |
97 | | - return string(extent.StatusStatePending) |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - return string(extent.StatusStateSuccess) |
102 | | -} |
0 commit comments