| @@ -2,14 +2,22 @@ package models | |||
| import ( | |||
| "code.gitea.io/gitea/modules/timeutil" | |||
| "fmt" | |||
| "time" | |||
| ) | |||
| type BlockChainCommitStatus int | |||
| const ( | |||
| BlockChainCommitInit BlockChainCommitStatus = iota | |||
| BlockChainCommitReady | |||
| BlockChainCommitFailed | |||
| ) | |||
| type BlockChain struct { | |||
| ID int64 `xorm:"pk autoincr"` | |||
| CommitID string `xorm:"INDEX NOT NULL"` | |||
| Contributor string `xorm:"INDEX NOT NULL"` | |||
| ContractAddress string `xorm:"INDEX NOT NULL"` | |||
| Action string `xorm:"INDEX"` | |||
| Status BlockChainCommitStatus `xorm:"INDEX NOT NULL DEFAULT 0"` | |||
| Amount int64 `xorm:"INDEX"` | |||
| UserID int64 `xorm:"INDEX"` | |||
| RepoID int64 `xorm:"INDEX"` | |||
| @@ -21,3 +29,42 @@ type BlockChain struct { | |||
| Repo *Repository `xorm:"-"` | |||
| } | |||
| func getBlockChainByID(e Engine, id int64) (*BlockChain, error) { | |||
| blockChain := new(BlockChain) | |||
| has, err := e.ID(id).Get(blockChain) | |||
| if err != nil { | |||
| return nil, err | |||
| } else if !has { | |||
| return nil, fmt.Errorf("get block_chain by id failed(%d)", id) | |||
| } | |||
| return blockChain, nil | |||
| } | |||
| func GetBlockChainByID(id int64) (*BlockChain, error) { | |||
| return getBlockChainByID(x, id) | |||
| } | |||
| func getBlockChainByCommitID(e Engine, commitID string) (*BlockChain, error) { | |||
| blockChain := new(BlockChain) | |||
| has, err := e.Where("commit_id = ?", commitID).Get(blockChain) | |||
| if err != nil { | |||
| return nil, err | |||
| } else if !has { | |||
| return nil, fmt.Errorf("get block_chain by commitID failed(%s)", commitID) | |||
| } | |||
| return blockChain, nil | |||
| } | |||
| func GetBlockChainByCommitID(commitID string) (*BlockChain, error) { | |||
| return getBlockChainByCommitID(x, commitID) | |||
| } | |||
| func updateBlockChainCols(e Engine, blockChain *BlockChain, cols ...string) error { | |||
| _, err := e.ID(blockChain.ID).Cols(cols...).Update(blockChain) | |||
| return err | |||
| } | |||
| func UpdateBlockChainCols(blockChain *BlockChain, cols ...string) error { | |||
| return updateBlockChainCols(x, blockChain, cols...) | |||
| } | |||
| @@ -135,6 +135,7 @@ func NewRepoContext() { | |||
| // RepositoryStatus defines the status of repository | |||
| type RepositoryStatus int | |||
| type RepoBlockChainStatus int | |||
| // all kinds of RepositoryStatus | |||
| const ( | |||
| @@ -142,6 +143,12 @@ const ( | |||
| RepositoryBeingMigrated // repository is migrating | |||
| ) | |||
| const ( | |||
| RepoBlockChainInit RepoBlockChainStatus = iota | |||
| RepoBlockChainReady | |||
| RepoBlockChainFailed | |||
| ) | |||
| // Repository represents a git repository. | |||
| type Repository struct { | |||
| ID int64 `xorm:"pk autoincr"` | |||
| @@ -199,6 +206,7 @@ type Repository struct { | |||
| //blockchain | |||
| ContractAddress string `xorm:"INDEX"` | |||
| Balance int64 `xorm:"NOT NULL DEFAULT 0"` | |||
| BlockChainStatus RepoBlockChainStatus `xorm:"NOT NULL DEFAULT 0"` | |||
| CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` | |||
| UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` | |||
| @@ -120,15 +120,18 @@ func GetBalance(contractAddress, contributor string) (*GetBalanceResult, error) | |||
| return &result, nil | |||
| } | |||
| func Contribute(jobID string) (*ContributeResult, error) { | |||
| func Contribute(contractAddress, contributor, action, commitId string, codeLine int) (*ContributeResult, error) { | |||
| client := getRestyClient() | |||
| var result ContributeResult | |||
| res, err := client.R(). | |||
| SetHeader("Accept", "application/json"). | |||
| SetQueryParams(map[string]string{ | |||
| "contractAddress" : "", | |||
| "contributor" : "", | |||
| "contractAddress" : contractAddress, | |||
| "contributor" : contributor, | |||
| "action" : action, | |||
| "commitId": commitId, | |||
| "amount": string(codeLine), | |||
| }). | |||
| SetResult(&result). | |||
| Get(setting.BlockChainHost + UrlContribute) | |||
| @@ -0,0 +1,101 @@ | |||
| package repo | |||
| import ( | |||
| "encoding/json" | |||
| "code.gitea.io/gitea/models" | |||
| "code.gitea.io/gitea/modules/context" | |||
| "code.gitea.io/gitea/modules/log" | |||
| ) | |||
| type BlockChainInitNotify struct { | |||
| RepoId int64 `json:"repoId"` | |||
| ContractAddress string `json:"contractAddress"` | |||
| } | |||
| type BlockChainCommitNotify struct { | |||
| CommitID string `json:"commitId"` | |||
| } | |||
| func HandleBlockChainInitNotify(ctx *context.Context) { | |||
| var req BlockChainInitNotify | |||
| data, _ := ctx.Req.Body().Bytes() | |||
| json.Unmarshal(data, &req) | |||
| repo, err := models.GetRepositoryByID(req.RepoId) | |||
| if err != nil { | |||
| log.Error("GetRepositoryByID failed:", err.Error()) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "internal error", | |||
| }) | |||
| return | |||
| } | |||
| if repo.BlockChainStatus == models.RepoBlockChainReady && len(repo.ContractAddress) != 0 { | |||
| log.Error("the repo has been RepoBlockChainReady:", req.RepoId) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "the repo has been RepoBlockChainReady", | |||
| }) | |||
| return | |||
| } | |||
| repo.BlockChainStatus = models.RepoBlockChainReady | |||
| repo.ContractAddress = req.ContractAddress | |||
| if err = models.UpdateRepositoryCols(repo, "block_chain_status", "contract_address"); err != nil { | |||
| log.Error("UpdateRepositoryCols failed:", err.Error()) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "internal error", | |||
| }) | |||
| return | |||
| } | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code": "0", | |||
| }) | |||
| } | |||
| func HandleBlockChainCommitNotify(ctx *context.Context) { | |||
| //contributor := ctx.Query("contributor") | |||
| //contractAddress := ctx.Query("contractAddress") | |||
| var req BlockChainCommitNotify | |||
| data, _ := ctx.Req.Body().Bytes() | |||
| json.Unmarshal(data, &req) | |||
| blockChain, err := models.GetBlockChainByCommitID(req.CommitID) | |||
| if err != nil { | |||
| log.Error("GetRepositoryByID failed:", err.Error()) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "internal error", | |||
| }) | |||
| return | |||
| } | |||
| if blockChain.Status == models.BlockChainCommitReady { | |||
| log.Error("the commit has been BlockChainCommitReady:", blockChain.RepoID) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "the commit has been BlockChainCommitReady", | |||
| }) | |||
| return | |||
| } | |||
| blockChain.Status = models.BlockChainCommitReady | |||
| if err = models.UpdateBlockChainCols(blockChain, "status"); err != nil { | |||
| log.Error("UpdateBlockChainCols failed:", err.Error()) | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code" : "-1", | |||
| "message" : "internal error", | |||
| }) | |||
| return | |||
| } | |||
| ctx.JSON(200, map[string]string{ | |||
| "result_code": "0", | |||
| }) | |||
| } | |||
| @@ -544,6 +544,11 @@ func RegisterRoutes(m *macaron.Macaron) { | |||
| m.Post("/action/:action", user.Action) | |||
| }, reqSignIn) | |||
| m.Group("/blockchain", func() { | |||
| m.Post("/init_notify", repo.HandleBlockChainInitNotify) | |||
| m.Post("/commit_notify", repo.HandleBlockChainCommitNotify) | |||
| }) | |||
| if macaron.Env == macaron.DEV { | |||
| m.Get("/template/*", dev.TemplatePreview) | |||
| } | |||