diff --git a/app/gitopia.go b/app/gitopia.go index 491d75b..80c4d27 100644 --- a/app/gitopia.go +++ b/app/gitopia.go @@ -3,16 +3,14 @@ package app import ( "context" - "github.com/pkg/errors" - "github.com/spf13/viper" - + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/authz" "github.com/gitopia/git-server/logger" "github.com/gitopia/gitopia/x/gitopia/types" + "github.com/pkg/errors" + "github.com/spf13/viper" "github.com/tendermint/starport/starport/pkg/cosmosaccount" "github.com/tendermint/starport/starport/pkg/cosmosclient" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/authz" ) const ( @@ -37,7 +35,8 @@ func NewGitopiaClient(ctx context.Context, account string) (GitopiaClient, error client, err := cosmosclient.New(ctx, cosmosclient.WithNodeAddress(viper.GetString("tm_addr")), //cosmosclient.WithKeyringServiceName("cosmos"), // not suported on macos - cosmosclient.WithKeyringBackend(cosmosaccount.KeyringOS), + cosmosclient.WithKeyringBackend(cosmosaccount.KeyringTest), + cosmosclient.WithHome(viper.GetString("keyring_dir")), cosmosclient.WithAddressPrefix(GITOPIA_ACC_ADDRESS_PREFIX), ) if err != nil { diff --git a/config_dev.toml b/config_dev.toml index 0e4dd9a..e21162a 100644 --- a/config_dev.toml +++ b/config_dev.toml @@ -3,4 +3,5 @@ gitopia_grpc_url = "grpc.gitopia.dev:9090" git_dir = "/var/repos" attachment_dir = "/var/attachments" tm_addr = "http://grpc.gitopia.dev:26657" -key_name = "git-server" \ No newline at end of file +key_name = "git-server" +keyring_dir = "/home/ubuntu/git-server" \ No newline at end of file diff --git a/config_local.toml b/config_local.toml index 5387c85..04e25ba 100644 --- a/config_local.toml +++ b/config_local.toml @@ -3,4 +3,5 @@ gitopia_grpc_url = "localhost:9090" git_dir = "/var/repos" attachment_dir = "/var/attachments" tm_addr = "http://localhost:26657" -key_name = "git-server" \ No newline at end of file +key_name = "git-server" +keyring_dir = "" \ No newline at end of file diff --git a/config_prod.toml b/config_prod.toml index c1b3ab6..4c03b57 100644 --- a/config_prod.toml +++ b/config_prod.toml @@ -3,4 +3,5 @@ gitopia_grpc_url = "grpc.gitopia.com:9090" git_dir = "/var/repos" attachment_dir = "/var/attachments" tm_addr = "http://grpc.gitopia.com:26657" -key_name = "git-server" \ No newline at end of file +key_name = "git-server" +keyring_dir = "/home/ubuntu/git-server" \ No newline at end of file diff --git a/handler.go b/handler.go index f6f4423..822be42 100644 --- a/handler.go +++ b/handler.go @@ -22,25 +22,13 @@ import ( "google.golang.org/grpc/credentials/insecure" ) +const MAX_UPLOAD_SIZE = 2 * 1024 * 1024 * 1024 // 2GB + type uploadAttachmentResponse struct { Sha string `json:"sha"` Size int64 `json:"size"` } -type forkRepositoryPostBody struct { - SourceRepositoryID uint64 `json:"source_repository_id"` - TargetRepositoryID uint64 `json:"target_repository_id"` -} - -type forkRepositoryResponseData struct { - Forked bool `json:"forked"` -} - -type forkRepositoryResponse struct { - Data forkRepositoryResponseData `json:"data"` - Error string `json:"error"` -} - type pullRequestCheckResponseData struct { IsMergeable bool `json:"is_mergeable"` } @@ -50,17 +38,12 @@ type pullRequestCheckResponse struct { Error string `json:"error"` } -type pullRequestMergeResponseData struct { - Merged bool `json:"merged"` - MergeCommitSha string `json:"merge_commit_sha"` -} - -type pullRequestMergeResponse struct { - Data pullRequestMergeResponseData `json:"data"` - Error string `json:"error"` -} - func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) { + r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE) + if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil { + http.Error(w, "The uploaded file is too big. Please choose an file that's less than 2GB in size", http.StatusBadRequest) + return + } err := r.ParseMultipartForm(32 << 20) if err != nil { @@ -85,11 +68,19 @@ func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) { sha := sha256.New() _, err = io.Copy(io.MultiWriter(sha, tmpFile), file) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } attachmentDir := viper.GetString("attachment_dir") shaString := hex.EncodeToString(sha.Sum(nil)) filePath := fmt.Sprintf("%s/%s", attachmentDir, shaString) localFile, err := os.Create(filePath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } defer localFile.Close() tmpFile.Seek(0, io.SeekStart) @@ -108,8 +99,6 @@ func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) { } json.NewEncoder(w).Encode(resp) - - return } func getAttachmentHandler(w http.ResponseWriter, r *http.Request) { @@ -170,8 +159,6 @@ func getAttachmentHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } - - return } func (s *Server) pullRequestCommitsHandler(w http.ResponseWriter, r *http.Request) { diff --git a/handler/mergePullRequest.go b/handler/mergePullRequest.go index acbf971..25b7875 100644 --- a/handler/mergePullRequest.go +++ b/handler/mergePullRequest.go @@ -169,7 +169,17 @@ func (h *InvokeMergePullRequestEventHandler) Process(ctx context.Context, event return err } - message := fmt.Sprintf("Merge pull request #%v from %s/%s", resp.Iid, event.Creator, resp.Head.Branch) + headRepositoryName, err := h.gc.RepositoryName(ctx, resp.Head.RepositoryId) + if err != nil { + err = errors.WithMessage(err, "query error") + err2 := h.gc.UpdateTask(ctx, event.Creator, event.TaskId, types.StateFailure, err.Error()) + if err2 != nil { + return errors.WithMessage(err2, "update task error") + } + return err + } + + message := fmt.Sprintf("Merge pull request #%v from %s/%s", resp.Iid, headRepositoryName, resp.Head.Branch) quarantineRepoPath, err := utils.CreateQuarantineRepo(resp.Base.RepositoryId, resp.Head.RepositoryId, resp.Base.Branch, resp.Head.Branch) if err != nil {