|
- package repo
-
- import (
- "code.gitea.io/gitea/modules/repository"
- "encoding/json"
- "strconv"
-
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/blockchain"
- "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"`
- TransactionHash string `json:"txHash"`
- }
-
- 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{
- "code": "-1",
- "message": "internal error",
- })
- return
- }
-
- if repo.BlockChainStatus == models.RepoBlockChainSuccess && len(repo.ContractAddress) != 0 {
- log.Error("the repo has been RepoBlockChainSuccess:", req.RepoId)
- ctx.JSON(200, map[string]string{
- "code": "-1",
- "message": "the repo has been RepoBlockChainSuccess",
- })
- return
- }
-
- repo.BlockChainStatus = models.RepoBlockChainSuccess
- 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{
- "code": "-1",
- "message": "internal error",
- })
- return
- }
-
- ctx.JSON(200, map[string]string{
- "code": "0",
- "message": "",
- })
- }
-
- func HandleBlockChainCommitNotify(ctx *context.Context) {
- var req BlockChainCommitNotify
- data, _ := ctx.Req.Body().Bytes()
- if err := json.Unmarshal(data, &req); err != nil {
- log.Error("json.Unmarshal failed:", err.Error())
- ctx.JSON(200, map[string]string{
- "code": "-1",
- "message": "response data error",
- })
- return
- }
-
- blockChain, err := models.GetBlockChainByCommitID(req.CommitID)
- if err != nil {
- log.Error("GetRepositoryByID failed:", err.Error())
- ctx.JSON(200, map[string]string{
- "code": "-1",
- "message": "internal error",
- })
- return
- }
-
- if blockChain.Status == models.BlockChainCommitSuccess {
- log.Error("the commit has been BlockChainCommitReady:", blockChain.RepoID)
- ctx.JSON(200, map[string]string{
- "code": "-1",
- "message": "the commit has been BlockChainCommitReady",
- })
- return
- }
-
- blockChain.Status = models.BlockChainCommitSuccess
- blockChain.TransactionHash = req.TransactionHash
-
- if err = models.UpdateBlockChainCols(blockChain, "status", "transaction_hash"); err != nil {
- log.Error("UpdateBlockChainCols failed:", err.Error())
- ctx.JSON(200, map[string]string{
- "code": "-1",
- "message": "internal error",
- })
- return
- }
-
- ctx.JSON(200, map[string]string{
- "code": "0",
- "message": "",
- })
- }
-
- func HandleBlockChainUnSuccessRepos() {
- repos, err := models.GetBlockChainUnSuccessRepos()
- if err != nil {
- log.Error("GetBlockChainUnSuccessRepos failed:", err.Error())
- return
- }
-
- for _, repo := range repos {
- err = repo.GetOwner()
- if err != nil {
- log.Error("GetOwner(%s) failed:%v", repo.Name, err)
- continue
- }
- if len(repo.Owner.PrivateKey) == 0 || len(repo.Owner.PublicKey) == 0 {
- log.Error("the user has not been init in block_chain:", repo.Owner.Name)
- continue
- }
- strRepoID := strconv.FormatInt(repo.ID, 10)
- log.Info(strRepoID)
- _, err = blockchain.NewRepo(strRepoID, repo.Owner.PublicKey, repo.Name)
- if err != nil {
- log.Error("blockchain.NewRepo(%s) failed:%v", strRepoID, err)
- }
- }
-
- return
- }
-
- func HandleBlockChainUnSuccessCommits() {
- blockChains, err := models.GetBlockChainUnSuccessCommits()
- if err != nil {
- log.Error("GetBlockChainUnSuccessCommits failed:", err.Error())
- return
- }
-
- 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:%v", block_chain.CommitID, err)
- }
- }
-
- return
- }
-
- func HandleBlockChainUnSuccessUsers() {
- users, err := models.GetBlockChainUnSuccessUsers()
- if err != nil {
- log.Error("GetBlockChainUnSuccessUsers failed:", err.Error())
- return
- }
-
- for _, user := range users {
- result, err := blockchain.CreateBlockchainAccount()
- if err != nil {
- log.Error("blockchain.CreateBlockchainAccount(%s) failed:%v", user.Name, err)
- continue
- }
-
- user.PublicKey = result.Payload["publickey"].(string)
- user.PrivateKey = result.Payload["privatekey"].(string)
-
- models.UpdateUser(user)
- }
-
- 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
- }
|