diff --git a/docs/开源社区平台与区块链平台对接方案.docx b/docs/开源社区平台与区块链平台对接方案.docx index b44801e42..8b0093547 100755 Binary files a/docs/开源社区平台与区块链平台对接方案.docx and b/docs/开源社区平台与区块链平台对接方案.docx differ diff --git a/models/blockchain.go b/models/blockchain.go index 07f6d05bd..a5b335184 100755 --- a/models/blockchain.go +++ b/models/blockchain.go @@ -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) { diff --git a/models/pull_list.go b/models/pull_list.go index 582d44f60..ee61f2ab5 100755 --- a/models/pull_list.go +++ b/models/pull_list.go @@ -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) } diff --git a/modules/blockchain/resty.go b/modules/blockchain/resty.go index 75374740d..09178181a 100755 --- a/modules/blockchain/resty.go +++ b/modules/blockchain/resty.go @@ -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 +} diff --git a/modules/timer/timer.go b/modules/timer/timer.go index a501234d1..176f84423 100755 --- a/modules/timer/timer.go +++ b/modules/timer/timer.go @@ -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() } diff --git a/routers/repo/blockchain.go b/routers/repo/blockchain.go index 77529ea07..2e7eff6fc 100755 --- a/routers/repo/blockchain.go +++ b/routers/repo/blockchain.go @@ -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)