-
Notifications
You must be signed in to change notification settings - Fork 4
/
git.go
121 lines (114 loc) · 5.1 KB
/
git.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
package cproject
import (
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/project/git"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/views/vgit"
"projectforge.dev/projectforge/views/vpage"
)
var (
authorsArg = &cutil.Arg{Key: "authors", Title: "Authors", Description: "Limit to a set of author emails (comma-separated)"}
branchArg = &cutil.Arg{Key: "name", Title: "Branch Name", Description: "The name to used for the new branch"}
dryRunArg = &cutil.Arg{Key: "dryRun", Title: "Dry Run", Description: "Runs without any destructive operations", Type: "bool", Default: util.BoolTrue}
limitArg = &cutil.Arg{Key: "limit", Title: "Limit", Description: "Limits the results to, at most, this amount", Type: "number", Default: "100"}
messageArg = &cutil.Arg{Key: "message", Title: "Message", Description: "The message to used for the commit"}
pathArg = &cutil.Arg{Key: "path", Title: "Path", Description: "Limits the results to the provided path (leave blank for all)"}
sinceArg = &cutil.Arg{Key: "since", Title: "Since", Description: "Limit to a date range", Type: "datetime"}
gitBranchArgs = cutil.Args{branchArg}
gitCommitArgs = cutil.Args{messageArg}
gitHistoryArgs = cutil.Args{pathArg, sinceArg, authorsArg, limitArg}
gitMagicArgs = cutil.Args{messageArg, dryRunArg}
)
func GitAction(rc *fasthttp.RequestCtx) {
a, _ := cutil.RCRequiredString(rc, "act", false)
controller.Act("git.action."+a, rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", false)
if err != nil {
return "", err
}
prj, err := as.Services.Projects.Get(key)
if err != nil {
return "", errors.Wrap(err, "unable to load project")
}
bc := []string{"projects", prj.Key, "Git"}
var result *git.Result
actn := git.ActionStatusFromString(a)
switch a {
case git.ActionStatus.Key, "":
// runs by default
case git.ActionCreateRepo.Key:
result, err = as.Services.Git.CreateRepo(ps.Context, prj, ps.Logger)
case git.ActionFetch.Key:
result, err = as.Services.Git.Fetch(ps.Context, prj, ps.Logger)
case git.ActionCommit.Key:
argRes := cutil.CollectArgs(rc, gitCommitArgs)
if argRes.HasMissing() {
ps.Data = argRes
url := fmt.Sprintf("/git/%s/commit", prj.Key)
return controller.Render(rc, as, &vpage.Args{URL: url, Directions: "Enter your commit message", ArgRes: argRes}, ps, bc...)
}
result, err = as.Services.Git.Commit(ps.Context, prj, argRes.Values["message"], ps.Logger)
case git.ActionUndoCommit.Key:
result, err = as.Services.Git.UndoCommit(ps.Context, prj, ps.Logger)
case git.ActionPull.Key:
result, err = as.Services.Git.Pull(ps.Context, prj, ps.Logger)
case git.ActionPush.Key:
result, err = as.Services.Git.Push(ps.Context, prj, ps.Logger)
case git.ActionOutdated.Key:
result, err = as.Services.Git.Outdated(ps.Context, prj, ps.Logger)
case git.ActionHistory.Key:
argRes := cutil.CollectArgs(rc, gitHistoryArgs)
if argRes.HasMissing() {
ps.Data = argRes
url := fmt.Sprintf("/git/%s/history", prj.Key)
return controller.Render(rc, as, &vpage.Args{URL: url, Directions: "Choose your options", ArgRes: argRes}, ps, bc...)
}
path := argRes.Values["paths"]
since, _ := util.TimeFromString(argRes.Values["since"])
authors := util.StringSplitAndTrim(argRes.Values["authors"], ",")
commit := string(rc.URI().QueryArgs().Peek("commit"))
limit, _ := strconv.Atoi(argRes.Values["limit"])
hist := &git.HistoryResult{Path: path, Since: since, Authors: authors, Commit: commit, Limit: limit}
result, err = as.Services.Git.History(ps.Context, prj, hist, ps.Logger)
case git.ActionBranch.Key:
argRes := cutil.CollectArgs(rc, gitBranchArgs)
if argRes.HasMissing() {
url := fmt.Sprintf("/git/%s/branch", prj.Key)
ps.Data = argRes
return controller.Render(rc, as, &vpage.Args{URL: url, Directions: "Enter your branch name", ArgRes: argRes}, ps, bc...)
}
result, err = as.Services.Git.Commit(ps.Context, prj, argRes.Values["message"], ps.Logger)
case git.ActionMagic.Key:
argRes := cutil.CollectArgs(rc, gitMagicArgs)
if argRes.HasMissing() {
url := fmt.Sprintf("/git/%s/magic", prj.Key)
ps.Data = argRes
return controller.Render(rc, as, &vpage.Args{URL: url, Directions: "Enter your commit message", ArgRes: argRes}, ps, bc...)
}
dryRun := argRes.Values["dryRun"] == util.BoolTrue
result, err = as.Services.Git.Magic(ps.Context, prj, argRes.Values["message"], dryRun, ps.Logger)
default:
err = errors.Errorf("unhandled action [%s]", a)
}
if err != nil {
return "", err
}
statusResult, err := as.Services.Git.Status(ps.Context, prj, ps.Logger)
if err != nil {
return "", errors.Wrap(err, "unable to pull repo status")
}
if result == nil {
result = statusResult
} else {
result.Data = result.Data.Merge(statusResult.Data)
}
ps.Data = result
return controller.Render(rc, as, &vgit.Result{Action: actn, Result: result}, ps, bc...)
})
}