1. add upload pull request comment info function

2. add query pull request comment latest info function
3. add query pull request comment all info function
This commit is contained in:
sulenn 2021-05-12 21:43:17 +08:00
parent fc02643b26
commit e4250e7578
12 changed files with 384 additions and 16 deletions

View File

@ -8,5 +8,5 @@ var (
RepoCurBalanceDownOverFlow int64 = -1005 // 当前项目总 Token 数低于0
UserBblanceNotEnough int64 = -1006 // 用户余额不足
ContractAddress string = "0x904ed579402eD8BBb80ee7F0eb02e8226d78a70f"
ContractAddress string = "0xE46EE87f19049d5705989f49305b136d0732a0Cf"
)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,7 @@ contract OpenSource {
string constant COMMIT_TABLE = "commit";
string constant PUSH_TABLE = "push";
string constant PULL_REQUEST_TABLE = "pull_request";
string constant PULL_REQUEST_COMMENT_TABLE = "pull_request_comment";
constructor() public {
tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
@ -19,6 +20,7 @@ contract OpenSource {
tableFactory.createTable(COMMIT_TABLE, "commit_hash", "repo_id,author,email,time,content,commit_diff");
tableFactory.createTable(PUSH_TABLE, "push_id", "push_number,repo_id,reponame,ownername,username,branch,commit_shas,time");
tableFactory.createTable(PULL_REQUEST_TABLE, "pull_request_id", "content");
tableFactory.createTable(PULL_REQUEST_COMMENT_TABLE, "pull_request_comment_id", "content");
}
// create repo
@ -444,7 +446,7 @@ contract OpenSource {
return (entry.getString("pull_request_id"), entry.getString("content"));
}
// select pull request All info
// select pull request all info
function selectPullRequestAllInfo(string memory pull_request_id)
public
view
@ -465,4 +467,58 @@ contract OpenSource {
return (pull_request_id_list, content_list);
}
// add pull request comment info
function addPullRequestCommentData(string memory pull_request_comment_id, string memory content)
public
returns (int256)
{
Table pull_request_comment_table = tableFactory.openTable(PULL_REQUEST_COMMENT_TABLE);
Entry pull_request_comment_entry = pull_request_comment_table.newEntry();
pull_request_comment_entry.set("pull_request_comment_id", pull_request_comment_id);
pull_request_comment_entry.set("content", content);
int256 count = pull_request_comment_table.insert(pull_request_comment_id, pull_request_comment_entry);
return count;
}
// select pull request comment latest info
function selectPullRequestCommentInfo(string memory pull_request_comment_id)
public
view
returns (string memory, string memory)
{
Table pull_request_comment_table = tableFactory.openTable(PULL_REQUEST_COMMENT_TABLE);
Condition condition = pull_request_comment_table.newCondition();
Entries entries = pull_request_comment_table.select(pull_request_comment_id, condition);
if (entries.size() == 0) {
return ("", "");
}
Entry entry = entries.get(entries.size()-1);
return (entry.getString("pull_request_comment_id"), entry.getString("content"));
}
// select pull request comment all info
function selectPullRequestCommentAllInfo(string memory pull_request_comment_id)
public
view
returns (string[] memory, string[] memory)
{
Table pull_request_comment_table = tableFactory.openTable(PULL_REQUEST_COMMENT_TABLE);
Condition condition = pull_request_comment_table.newCondition();
Entries entries = pull_request_comment_table.select(pull_request_comment_id, condition);
string[] memory pull_request_id_comment_list = new string[](uint256(entries.size()));
string[] memory content_list = new string[](uint256(entries.size()));
for (int256 i = 0; i < entries.size(); ++i) {
Entry entry = entries.get(i);
pull_request_id_comment_list[uint256(i)] = entry.getString("pull_request_id_comment");
content_list[uint256(i)] = entry.getString("content");
}
return (pull_request_id_comment_list, content_list);
}
}

View File

@ -16,7 +16,7 @@ type FiscoBcos struct {
Author string `json:"author"`
Email string `json:"email"`
Time string `json:"time"`
Content string `json:"message"`
Content string `json:"content"`
CommitDiff string `json:"commit_diff"`
// repo push 相关
@ -28,7 +28,7 @@ type FiscoBcos struct {
CommitShas []string `json:"commit_shas"`
// repo_id, username, time
// pull request 相关
// repo pull request 相关
PullRequestID string `json:"pull_request_id"`
PullRequestNumber uint64 `json:"pull_request_number"`
Action string `json:"action"`
@ -39,9 +39,15 @@ type FiscoBcos struct {
Reviewers []string `json:"reviewers"`
MergeUser string `json:"merge_user"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_ad"`
UpdatedAt string `json:"updated_at"`
// repo_id, reponame, ownername, username, content, commit_shas
// repo pull request comment 相关
PullRequestCommentID string `json:"pull_request_comment_id"`
PullRequestCommentNumber uint64 `json:"pull_request_comment_number"`
ParentID string `json:"parent_id"`
// pull_request_number, repo_id, reponame, ownername, username, action, content, created_at, updated_at
// user 相关
Username string `json:"username"` // 用户名
Amount uint64 `json:"amount"` // 转账金额

View File

@ -16,7 +16,7 @@ type UploadPullReuqestOption struct {
Reponame string `json:"reponame"`
Ownername string `json:"ownername"`
Username string `json:"username"`
Content string `json:"message"`
Content string `json:"content"`
CommitShas []string `json:"commit_shas"`
}

View File

@ -0,0 +1,28 @@
package structs
type UploadPullReuqestCommentOption struct {
PullRequestCommentID string `json:"pull_request_comment_id"`
PullRequestCommentNumber uint64 `json:"pull_request_comment_number"`
ParentID string `json:"parent_id"`
PullRequestNumber uint64 `json:"pull_request_number"`
RepoID string `json:"repo_id"`
Reponame string `json:"reponame"`
Ownername string `json:"ownername"`
Username string `json:"username"`
Action string `json:"action"`
Content string `json:"content"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type ResponsePullRequestComment struct {
UploadPullReuqestCommentOption
Response
}
type ResponsePullRequestCommentArr struct {
UploadPullReuqestCommentOptionArr
Response
}
type UploadPullReuqestCommentOptionArr []UploadPullReuqestCommentOption

View File

@ -16,12 +16,14 @@ var (
ResRepoCurBalanceUpOverFlow = &Response{Status: 4, Message: "repo current balance up overflow when user adds amount!"} // username balance overflow when create repo!
ResRepoCurBalanceDownOverFlow = &Response{Status: 6, Message: "repo current balance less than 0 when user adds amount!"}
ResCommitUploadSucc = &Response{Status: 0, Message: "commit upload successfully!"}
ResCommitUploadUnsucc = &Response{Status: 7, Message: "commit upload unsuccessfully!"}
ResPushUploadSucc = &Response{Status: 0, Message: "push upload successfully!"}
ResPushUploadUnsucc = &Response{Status: 8, Message: "push upload unsuccessfully!"}
ResPullRequestUploadSucc = &Response{Status: 0, Message: "pull request upload successfully!"}
ResPullRequestUploadUnsucc = &Response{Status: 9, Message: "pull request upload unsuccessfully!"}
ResCommitUploadSucc = &Response{Status: 0, Message: "commit upload successfully!"}
ResCommitUploadUnsucc = &Response{Status: 7, Message: "commit upload unsuccessfully!"}
ResPushUploadSucc = &Response{Status: 0, Message: "push upload successfully!"}
ResPushUploadUnsucc = &Response{Status: 8, Message: "push upload unsuccessfully!"}
ResPullRequestUploadSucc = &Response{Status: 0, Message: "pull request upload successfully!"}
ResPullRequestUploadUnsucc = &Response{Status: 9, Message: "pull request upload unsuccessfully!"}
ResPullRequestCommentUploadSucc = &Response{Status: 0, Message: "pull request comment upload successfully!"}
ResPullRequestCommentUploadUnsucc = &Response{Status: 10, Message: "pull request comment upload unsuccessfully!"}
ResUserNotExisted = &Response{Status: 100, Message: "user not exist!"}
ResUserAddAmountSucc = &Response{Status: 0, Message: "user adds amount successfully!"}

View File

@ -82,6 +82,27 @@ func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger
repo.SelectPullRequestLatestInfo(ctx, opt.PullRequestID, logger)
case "query pull request all info":
repo.SelectPullRequestAllInfo(ctx, opt.PullRequestID, logger)
case "upload pull request comment info":
repo.UploadPullRequestCommentInfo(ctx,
api.UploadPullReuqestCommentOption{
PullRequestCommentID: opt.PullRequestCommentID,
PullRequestCommentNumber: opt.PullRequestCommentNumber,
PullRequestNumber: opt.PullRequestNumber,
RepoID: opt.RepoID,
ParentID: opt.ParentID,
Reponame: opt.Reponame,
Ownername: opt.Ownername,
Username: opt.Username,
Action: opt.Action,
CreatedAt: opt.CreatedAt,
UpdatedAt: opt.UpdatedAt,
Content: opt.Content,
},
logger)
case "query pull request comment latest info":
repo.SelectPullRequestCommentLatestInfo(ctx, opt.PullRequestCommentID, logger)
case "query pull request comment all info":
repo.SelectPullRequestCommentAllInfo(ctx, opt.PullRequestCommentID, logger)
case "query user balance of single repo":
user.SelectUserBalance(ctx, opt.Username, opt.TokenName, logger)
case "query user balance of all repos":

View File

@ -243,3 +243,114 @@ func SelectPullRequestAllInfo(ctx *macaron.Context, pullRequestID string, logger
UploadPullReuqestOptionArr: uploadPullReuqestOptionArr,
})
}
// 查询 pull request comment 最新数据
func SelectPullRequestCommentLatestInfo(ctx *macaron.Context, pullRequestCommentID string, logger *log.Logger) {
if pullRequestCommentID == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
// load the contract
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
_, strJSON, err := openSourceSession.SelectPullRequestCommentInfo(pullRequestCommentID) // call Insert API
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
fmt.Printf("pullRequestID: %v \n", strJSON)
var uploadPullRequestCommentOption api.UploadPullReuqestCommentOption
err = json.Unmarshal([]byte(strJSON), &uploadPullRequestCommentOption)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
ctx.JSON(http.StatusOK, &structs.ResponsePullRequestComment{
Response: structs.Response{
Status: 0,
Message: "query success!",
},
UploadPullReuqestCommentOption: uploadPullRequestCommentOption,
})
}
// 查询 pull request comment 所有数据
func SelectPullRequestCommentAllInfo(ctx *macaron.Context, pullRequestCommentID string, logger *log.Logger) {
if pullRequestCommentID == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
// load the contract
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
_, strJSONArr, err := openSourceSession.SelectPullRequestCommentAllInfo(pullRequestCommentID) // call Insert API
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
fmt.Printf("pullRequestID: %v \n", strJSONArr)
var uploadPullRequestCommentOption api.UploadPullReuqestCommentOption
var uploadPullReuqestCommentOptionArr api.UploadPullReuqestCommentOptionArr
for i := 0; i < len(strJSONArr); i++ {
err = json.Unmarshal([]byte(strJSONArr[i]), &uploadPullRequestCommentOption)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
uploadPullReuqestCommentOptionArr = append(uploadPullReuqestCommentOptionArr, uploadPullRequestCommentOption)
}
ctx.JSON(http.StatusOK, &structs.ResponsePullRequestCommentArr{
Response: structs.Response{
Status: 0,
Message: "query success!",
},
UploadPullReuqestCommentOptionArr: uploadPullReuqestCommentOptionArr,
})
}

View File

@ -176,3 +176,54 @@ func UploadPullRequestInfo(ctx *macaron.Context, opt api.UploadPullReuqestOption
}
ctx.JSON(http.StatusOK, structs.ResPullRequestUploadUnsucc)
}
// 上传 pull request comment 数据
func UploadPullRequestCommentInfo(ctx *macaron.Context, opt api.UploadPullReuqestCommentOption, logger *log.Logger) {
pullRequestCommentID := opt.PullRequestCommentID
bytesJson, err := json.Marshal(opt)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
// load the contract
contractAddress := common.HexToAddress(contract.ContractAddress)
instance, err := opensource.NewOpenSource(contractAddress, client)
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
openSourceSession := &opensource.OpenSourceSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
tx, receipt, err := openSourceSession.AddPullRequestCommentData(pullRequestCommentID, string(bytesJson)) // call Insert API
if err != nil {
ctx.JSON(http.StatusOK, api.UnknownErr(err))
return
}
logger.Printf("tx sent: %s\n", tx.Hash().Hex())
// insertedLines, err := strconv.Atoi(receipt.Output[2:])
code, err := parseOutput(opensource.OpenSourceABI, "addPullRequestCommentData", receipt)
if err != nil {
logger.Panic("error when transfer string to int: ", err)
}
if code.Int64() > 0 {
logger.Printf("inserted lines: %v\n", code)
ctx.JSON(http.StatusOK, structs.ResPullRequestCommentUploadSucc)
return
}
ctx.JSON(http.StatusOK, structs.ResPullRequestCommentUploadUnsucc)
}