Browse Source

commit interface

tags/v0.1.8
yuyuanshifu 4 years ago
parent
commit
5da6b83398
5 changed files with 103 additions and 4 deletions
  1. +11
    -0
      models/action.go
  2. +9
    -0
      models/blockchain.go
  3. +2
    -0
      modules/setting/setting.go
  4. +7
    -0
      modules/timer/timer.go
  5. +74
    -4
      routers/repo/blockchain.go

+ 11
- 0
models/action.go View File

@@ -67,6 +67,7 @@ type Action struct {
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
RefName string
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
IsTransformed bool `xorm:"INDEX NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}
@@ -344,3 +345,13 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {

return actions, nil
}

func GetUnTransformedActions() ([]*Action, error) {
actions := make([]*Action, 0, 10)
err := x.Where("op_type = ?", ActionCommitRepo).
And("content != ''").
And("is_transformed = ?", false).
And("to_timestamp(created_unix) >= ?", setting.CommitValidDate).
Find(&actions)
return actions, err
}

+ 9
- 0
models/blockchain.go View File

@@ -75,3 +75,12 @@ func GetBlockChainUnSuccessCommits() ([]*BlockChain, error) {
Where("status != ?", BlockChainCommitSuccess).
Find(&blockChains)
}

func InsertBlockChain(blockChain *BlockChain) (_ *BlockChain, err error) {

if _, err := x.Insert(blockChain); err != nil {
return nil, err
}

return blockChain, nil
}

+ 2
- 0
modules/setting/setting.go View File

@@ -446,6 +446,7 @@ var (

//blockchain config
BlockChainHost string
CommitValidDate string
)

// DateLang transforms standard language locale name to corresponding value in datetime plugin.
@@ -1129,6 +1130,7 @@ func NewContext() {

sec = Cfg.Section("blockchain")
BlockChainHost = sec.Key("HOST").MustString("http://192.168.136.66:3302/")
CommitValidDate = sec.Key("COMMIT_VALID_DATE").MustString("2021-01-15")
}

func loadInternalToken(sec *ini.Section) string {


+ 7
- 0
modules/timer/timer.go View File

@@ -8,12 +8,19 @@ import (

func init() {
c := cron.New()

spec := "*/10 * * * *"
c.AddFunc(spec, repo.HandleUnDecompressAttachment)

specCheckBlockChainUserSuccess := "*/10 * * * *"
c.AddFunc(specCheckBlockChainUserSuccess, repo.HandleBlockChainUnSuccessUsers)

specCheckRepoBlockChainSuccess := "*/5 * * * *"
c.AddFunc(specCheckRepoBlockChainSuccess, repo.HandleBlockChainUnSuccessRepos)

specCheckUnTransformedActions := "*/1 * * * *"
c.AddFunc(specCheckUnTransformedActions, repo.HandleUnTransformedActions)

specCheckBlockChainCommitSuccess := "*/3 * * * *"
c.AddFunc(specCheckBlockChainCommitSuccess, repo.HandleBlockChainUnSuccessCommits)
c.Start()


+ 74
- 4
routers/repo/blockchain.go View File

@@ -1,6 +1,7 @@
package repo

import (
"code.gitea.io/gitea/modules/repository"
"encoding/json"
"strconv"

@@ -121,7 +122,7 @@ func HandleBlockChainUnSuccessRepos() {
for _, repo := range repos {
err = repo.GetOwner()
if err != nil {
log.Error("GetOwner(%s) failed:%s", repo.Name, err.Error())
log.Error("GetOwner(%s) failed:%v", repo.Name, err)
continue
}
if len(repo.Owner.PrivateKey) == 0 || len(repo.Owner.PublicKey) == 0 {
@@ -132,7 +133,7 @@ func HandleBlockChainUnSuccessRepos() {
log.Info(strRepoID)
_, err = blockchain.NewRepo(strRepoID, repo.Owner.PublicKey, repo.Name)
if err != nil {
log.Error("blockchain.NewRepo(%s) failed:%s", strRepoID, err.Error())
log.Error("blockchain.NewRepo(%s) failed:%v", strRepoID, err)
}
}

@@ -149,7 +150,7 @@ func HandleBlockChainUnSuccessCommits() {
for _, block_chain := range blockChains {
_, err = blockchain.Contribute(block_chain.ContractAddress, block_chain.Contributor, blockchain.ActionCommit, block_chain.CommitID, int(block_chain.Amount))
if err != nil {
log.Error("blockchain.Contribute(%s) failed:%s", block_chain.CommitID, err.Error())
log.Error("blockchain.Contribute(%s) failed:%v", block_chain.CommitID, err)
}
}

@@ -166,7 +167,7 @@ func HandleBlockChainUnSuccessUsers() {
for _, user := range users {
result, err := blockchain.CreateBlockchainAccount()
if err != nil {
log.Error("blockchain.CreateBlockchainAccount(%s) failed:%s", user.Name, err.Error())
log.Error("blockchain.CreateBlockchainAccount(%s) failed:%v", user.Name, err)
continue
}

@@ -178,3 +179,72 @@ func HandleBlockChainUnSuccessUsers() {

return
}

func HandleUnTransformedActions() {
actions, err := models.GetUnTransformedActions()
if err != nil {
log.Error("GetUnTransformedActions failed:", err.Error())
return
}

isTransformed := true

for _, action := range actions {
var content repository.PushCommits
err = json.Unmarshal([]byte(action.Content), &content)
if err != nil {
isTransformed = false
log.Error("json.Unmarshal action.Content(%s) failed:%v", action.Content, err)
break
}

repo, err := models.GetRepositoryByID(action.RepoID)
if err != nil {
isTransformed = false
log.Error("GetRepositoryByID(%d) failed:%v", action.RepoID, err)
break
}

if repo.ContractAddress == "" {
isTransformed = false
log.Error("the repo(%s) has not been initialized in block_chain", repo.Name)
break
}

for _, commit := range content.Commits {
_, err = models.GetBlockChainByCommitID(commit.Sha1)
if err == nil {
log.Info("the commit(%s) has been transformed", commit.Sha1)
continue
}

user, err := models.GetUserByName(commit.CommitterName)
if err != nil {
isTransformed = false
log.Error("GetUserByName(%s) failed:%v", commit.CommitterName, err)
break
}

blockChain := models.BlockChain{
CommitID : commit.Sha1,
Contributor : user.PublicKey,
ContractAddress : repo.ContractAddress,
Status : models.BlockChainCommitInit,
Amount : 1,
UserID : action.UserID,
RepoID : action.RepoID,
}
_, err = models.InsertBlockChain(&blockChain)
if err != nil {
isTransformed = false
log.Error("InsertBlockChain(%s) failed:%v", commit.Sha1, err)
break
}
}

}

log.Info("", isTransformed)

return
}

Loading…
Cancel
Save