mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
加入Git Checkout
功能
This commit is contained in:
@@ -262,6 +262,7 @@ func main() {
|
||||
authGroup.GET("/git/branches", routes.GetGitRemoteBranches) // 获取 Git 分支
|
||||
authGroup.GET("/git/public-key", routes.GetGitSshPublicKey) // 获取 SSH 公钥
|
||||
authGroup.GET("/git/commits", routes.GetGitCommits) // 获取 Git Commits
|
||||
authGroup.POST("/git/checkout", routes.PostGitCheckout) // 获取 Git Commits
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,3 +53,30 @@ func GetGitCommits(c *gin.Context) {
|
||||
Data: commits,
|
||||
})
|
||||
}
|
||||
|
||||
func PostGitCheckout(c *gin.Context) {
|
||||
type ReqBody struct {
|
||||
SpiderId string `json:"spider_id"`
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
var reqBody ReqBody
|
||||
if err := c.ShouldBindJSON(&reqBody); err != nil {
|
||||
}
|
||||
if reqBody.SpiderId == "" || !bson.IsObjectIdHex(reqBody.SpiderId) {
|
||||
HandleErrorF(http.StatusInternalServerError, c, "invalid request")
|
||||
return
|
||||
}
|
||||
spider, err := model.GetSpider(bson.ObjectIdHex(reqBody.SpiderId))
|
||||
if err != nil {
|
||||
HandleError(http.StatusInternalServerError, c, err)
|
||||
return
|
||||
}
|
||||
if err := services.GitCheckout(spider, reqBody.Hash); err != nil {
|
||||
HandleError(http.StatusInternalServerError, c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Status: "ok",
|
||||
Message: "success",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -360,6 +360,7 @@ func GetGitSshPublicKey() string {
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 获取Git分支
|
||||
func GetGitBranches(s model.Spider) (branches []GitBranch, err error) {
|
||||
// 打开 repo
|
||||
repo, err := git.PlainOpen(s.Src)
|
||||
@@ -387,6 +388,7 @@ func GetGitBranches(s model.Spider) (branches []GitBranch, err error) {
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
// 获取Git Tags
|
||||
func GetGitTags(s model.Spider) (tags []GitTag, err error) {
|
||||
// 打开 repo
|
||||
repo, err := git.PlainOpen(s.Src)
|
||||
@@ -414,11 +416,13 @@ func GetGitTags(s model.Spider) (tags []GitTag, err error) {
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func GetHeadHash(repo *git.Repository) string {
|
||||
// 获取Git Head Hash
|
||||
func GetGitHeadHash(repo *git.Repository) string {
|
||||
head, _ := repo.Head()
|
||||
return head.Hash().String()
|
||||
}
|
||||
|
||||
// 获取Git远端分支
|
||||
func GetGitRemoteBranches(s model.Spider) (branches []GitBranch, err error) {
|
||||
// 打开 repo
|
||||
repo, err := git.PlainOpen(s.Src)
|
||||
@@ -453,6 +457,7 @@ func GetGitRemoteBranches(s model.Spider) (branches []GitBranch, err error) {
|
||||
return branches, err
|
||||
}
|
||||
|
||||
// 获取Git Commits
|
||||
func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
|
||||
// 打开 repo
|
||||
repo, err := git.PlainOpen(s.Src)
|
||||
@@ -502,7 +507,7 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
|
||||
Author: commit.Author.Name,
|
||||
Email: commit.Author.Email,
|
||||
Ts: commit.Author.When,
|
||||
IsHead: commit.Hash.String() == GetHeadHash(repo),
|
||||
IsHead: commit.Hash.String() == GetGitHeadHash(repo),
|
||||
Branches: branchesDict[commit.Hash.String()],
|
||||
RemoteBranches: remoteBranchesDict[commit.Hash.String()],
|
||||
Tags: tagsDict[commit.Hash.String()],
|
||||
@@ -517,3 +522,35 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
|
||||
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
func GitCheckout(s model.Spider, hash string) (err error) {
|
||||
// 打开 repo
|
||||
repo, err := git.PlainOpen(s.Src)
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
|
||||
// 获取worktree
|
||||
wt, err := repo.Worktree()
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
|
||||
// Checkout
|
||||
if err := wt.Checkout(&git.CheckoutOptions{
|
||||
Hash: plumbing.NewHash(hash),
|
||||
Create: false,
|
||||
Force: true,
|
||||
Keep: false,
|
||||
}); err != nil {
|
||||
log.Error(err.Error())
|
||||
debug.PrintStack()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@
|
||||
{{c.author}} ({{c.email}})
|
||||
</div>
|
||||
</div>
|
||||
<div class="row" style="margin-top: 5px">
|
||||
<div class="row" style="margin-top: 10px">
|
||||
<div class="tags">
|
||||
<el-tag
|
||||
v-if="c.is_head"
|
||||
@@ -211,6 +211,17 @@
|
||||
{{t.label}}
|
||||
</el-tag>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<el-button
|
||||
v-if="!c.is_head"
|
||||
type="danger"
|
||||
:icon="isGitCheckoutLoading ? 'el-icon-loading' : 'el-icon-position'"
|
||||
size="mini"
|
||||
@click="checkout(c)"
|
||||
>
|
||||
Checkout
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-timeline-item>
|
||||
@@ -233,6 +244,7 @@ export default {
|
||||
isGitBranchesLoading: false,
|
||||
isGitSyncLoading: false,
|
||||
isGitResetLoading: false,
|
||||
isGitCheckoutLoading: false,
|
||||
syncFrequencies: [
|
||||
{ label: '1m', value: '0 * * * * *' },
|
||||
{ label: '5m', value: '0 0/5 * * * *' },
|
||||
@@ -334,13 +346,26 @@ export default {
|
||||
return d
|
||||
})
|
||||
},
|
||||
async checkout (c) {
|
||||
this.isGitCheckoutLoading = true
|
||||
try {
|
||||
const res = await this.$request.post('/git/checkout', { spider_id: this.spiderForm._id, hash: c.hash })
|
||||
if (!res.data.error) {
|
||||
this.$message.success(this.$t('Checkout success'))
|
||||
}
|
||||
} finally {
|
||||
this.isGitCheckoutLoading = false
|
||||
await this.getCommits()
|
||||
}
|
||||
this.$st.sendEv('爬虫详情', 'Git', 'Checkout')
|
||||
},
|
||||
async updateGit () {
|
||||
this.getCommits()
|
||||
},
|
||||
getCommitType (c) {
|
||||
if (c.is_head) return 'primary'
|
||||
if (c.branches && c.branches.length) {
|
||||
if (c.branches.map(d => d.name).includes('master')) {
|
||||
if (c.branches.map(d => d.label).includes('master')) {
|
||||
return 'danger'
|
||||
} else {
|
||||
return 'warning'
|
||||
@@ -439,4 +464,10 @@ export default {
|
||||
.git-settings .log .commit .row .tags .el-tag {
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.git-settings .log .commit .row .actions {
|
||||
right: 0;
|
||||
bottom: 5px;
|
||||
position: absolute;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user