Browse Source

set issue

tags/v0.1.8
yuyuanshifu 4 years ago
parent
commit
86565802a2
6 changed files with 56 additions and 91 deletions
  1. BIN
      docs/开源社区平台与区块链平台对接方案.docx
  2. +5
    -5
      models/blockchain.go
  3. +1
    -2
      models/pull_list.go
  4. +35
    -2
      modules/blockchain/resty.go
  5. +8
    -8
      modules/timer/timer.go
  6. +7
    -74
      routers/repo/blockchain.go

BIN
docs/开源社区平台与区块链平台对接方案.docx View File


+ 5
- 5
models/blockchain.go View File

@@ -46,19 +46,19 @@ func GetBlockChainByID(id int64) (*BlockChain, error) {
return getBlockChainByID(x, id)
}

func getBlockChainByPrID(e Engine, id int64) (*BlockChain, error) {
func getBlockChainByPrID(e Engine, prId int64) (*BlockChain, error) {
blockChain := new(BlockChain)
has, err := e.ID(id).Get(blockChain)
has, err := e.Where("pr_id = ?", prId).Get(blockChain)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("get block_chain by pr_id failed(%d)", id)
return nil, fmt.Errorf("get block_chain by pr_id failed(%d)", prId)
}
return blockChain, nil
}

func GetBlockChainByPrID(id int64) (*BlockChain, error) {
return getBlockChainByPrID(x, id)
func GetBlockChainByPrID(prId int64) (*BlockChain, error) {
return getBlockChainByPrID(x, prId)
}

func getBlockChainByCommitID(e Engine, commitID string) (*BlockChain, error) {


+ 1
- 2
models/pull_list.go View File

@@ -175,8 +175,7 @@ func (prs PullRequestList) InvalidateCodeComments(doer *User, repo *git.Reposito
func GetUnTransformedMergedPullRequests() ([]*PullRequest, error) {
prs := make([]*PullRequest, 0, 10)
return prs, x.
Where("has_merged = ? AND is_transformed = ?",true, false).
And("to_timestamp(merged_unix) >= ?", setting.CommitValidDate).
Where("has_merged = ? AND pull_request.is_transformed = ? AND to_timestamp(merged_unix) >= ?",true, false, setting.CommitValidDate).
Join("INNER", "issue", "issue.id = pull_request.issue_id").
Find(&prs)
}

+ 35
- 2
modules/blockchain/resty.go View File

@@ -17,8 +17,7 @@ const (
UrlGetBalance = "getBalance"
UrlNewRepo = "newRepo"
UrlContribute = "contribute"

ActionCommit = "commit"
UrlSetIssue = "setIssue"

Success = 0
)
@@ -47,6 +46,12 @@ type ContributeResult struct {
//Payload map[string]interface{} `json:"data"`
}

type SetIssueResult struct {
Code int `json:"code"`
Msg string `json:"message"`
//Payload map[string]interface{} `json:"data"`
}

func getRestyClient() *resty.Client {
if restyClient == nil {
restyClient = resty.New()
@@ -149,3 +154,31 @@ func Contribute(contractAddress, contributor, commitId string, amount int64) (*C

return &result, nil
}

func SetIssue(contractAddress, contributor string, issueId int64, amount int64) (*SetIssueResult, error) {
client := getRestyClient()
var result SetIssueResult

strAmount := strconv.FormatInt(amount, 10)
strIssue := strconv.FormatInt(issueId, 10)
res, err := client.R().
SetHeader("Accept", "application/json").
SetQueryParams(map[string]string{
"contractAddress" : contractAddress,
"contributor" : contributor,
"issueId": strIssue,
"amount": strAmount,
}).
SetResult(&result).
Get(setting.BlockChainHost + UrlSetIssue)

if err != nil {
return nil, fmt.Errorf("resty SetIssue: %v", err)
}

if result.Code != Success {
return &result, fmt.Errorf("SetIssue err: %s", res.String())
}

return &result, nil
}

+ 8
- 8
modules/timer/timer.go View File

@@ -12,16 +12,16 @@ func init() {
spec := "*/10 * * * *"
c.AddFunc(spec, repo.HandleUnDecompressAttachment)

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

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

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

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

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

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

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

@@ -179,82 +178,10 @@ func HandleBlockChainUnSuccessUsers() {
return
}

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

for _, action := range actions {
isTransformed := true
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)
continue
}

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

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

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)
continue
}

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)
continue
}
}

if isTransformed {
action.IsTransformed = isTransformed
//todo: update is_transformed
//models.updateaction(action)
}

}

return
}

func HandleBlockChainMergedPulls() {
prs, err := models.GetUnTransformedMergedPullRequests()
if err != nil {
log.Error("GetBlockChainUnSuccessUsers failed:", err.Error())
log.Error("GetUnTransformedMergedPullRequests failed:", err.Error())
return
}

@@ -265,6 +192,12 @@ func HandleBlockChainMergedPulls() {
continue
}

err = pr.LoadIssue()
if err != nil {
log.Error("LoadIssue(%s) failed:%v", pr.MergedCommitID, err)
continue
}

poster, err := models.GetUserByID(pr.Issue.PosterID)
if err != nil {
log.Error("GetUserByID(%s) failed:%v", pr.MergedCommitID, err)


Loading…
Cancel
Save