This is an automated email from the ASF dual-hosted git repository.

zhangliang2022 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 83ababed fix: remove dependency on bitbucket ENV from `.env` file 
(#3027)
83ababed is described below

commit 83ababed588dabc4c972474797a6221bf8ecd640
Author: tsoc <[email protected]>
AuthorDate: Wed Sep 14 21:06:05 2022 +0800

    fix: remove dependency on bitbucket ENV from `.env` file (#3027)
    
    * fix: deal with conflict
    
    * fix: deal with conflict
---
 plugins/bitbucket/models/connection.go             |  5 +-
 .../migrationscripts/20220803_add_init_tables.go   | 62 ++++++++++++++--------
 .../models/migrationscripts/archived/connection.go | 51 +++++++++---------
 plugins/gitee/tasks/pr_review_collector.go         |  1 +
 4 files changed, 69 insertions(+), 50 deletions(-)

diff --git a/plugins/bitbucket/models/connection.go 
b/plugins/bitbucket/models/connection.go
index 70266995..5a1accb3 100644
--- a/plugins/bitbucket/models/connection.go
+++ b/plugins/bitbucket/models/connection.go
@@ -59,9 +59,8 @@ type ApiUserResponse struct {
 }
 
 type BitbucketConnection struct {
-       helper.RestConnection      `mapstructure:",squash"`
-       helper.BasicAuth           `mapstructure:",squash"`
-       RemotelinkCommitShaPattern string 
`gorm:"type:varchar(255);comment='golang regexp, the first group will be 
recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" 
json:"remotelinkCommitShaPattern"`
+       helper.RestConnection `mapstructure:",squash"`
+       helper.BasicAuth      `mapstructure:",squash"`
 }
 
 func (BitbucketConnection) TableName() string {
diff --git 
a/plugins/bitbucket/models/migrationscripts/20220803_add_init_tables.go 
b/plugins/bitbucket/models/migrationscripts/20220803_add_init_tables.go
index 885ac756..ca8b59d6 100644
--- a/plugins/bitbucket/models/migrationscripts/20220803_add_init_tables.go
+++ b/plugins/bitbucket/models/migrationscripts/20220803_add_init_tables.go
@@ -19,20 +19,22 @@ package migrationscripts
 
 import (
        "context"
+       "encoding/base64"
        "github.com/apache/incubator-devlake/config"
        "github.com/apache/incubator-devlake/errors"
        
"github.com/apache/incubator-devlake/plugins/bitbucket/models/migrationscripts/archived"
+       "github.com/apache/incubator-devlake/plugins/core"
        "gorm.io/gorm"
-       "gorm.io/gorm/clause"
+       "strings"
 )
 
 type addInitTables struct{}
 
 func (*addInitTables) Up(ctx context.Context, db *gorm.DB) errors.Error {
        err := db.Migrator().DropTable(
+               //history table
                &archived.BitbucketRepo{},
                &archived.BitbucketRepoCommit{},
-               &archived.BitbucketConnection{},
                &archived.BitbucketAccount{},
                &archived.BitbucketCommit{},
                &archived.BitbucketPullRequest{},
@@ -40,7 +42,6 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
                &archived.BitbucketPrComment{},
                &archived.BitbucketIssueComment{},
        )
-
        if err != nil {
                return errors.Convert(err)
        }
@@ -56,33 +57,52 @@ func (*addInitTables) Up(ctx context.Context, db *gorm.DB) 
errors.Error {
                &archived.BitbucketPrComment{},
                &archived.BitbucketIssueComment{},
        )
-
        if err != nil {
                return errors.Convert(err)
        }
 
-       v := config.GetConfig()
-       encKey := v.GetString("ENCODE_KEY")
-       endPoint := v.GetString("BITBUCKET_ENDPOINT")
-       bitbucketUsername := v.GetString("BITBUCKET_AUTH_USERNAME")
-       bitbucketAppPassword := v.GetString("BITBUCKET_AUTH_PASSWORD")
-       if encKey == "" || endPoint == "" || bitbucketUsername == "" || 
bitbucketAppPassword == "" {
-               return nil
-       } else {
-               conn := &archived.BitbucketConnection{}
-               conn.Name = "init bitbucket connection"
-               conn.ID = 1
-               conn.Endpoint = endPoint
-               conn.BasicAuth.Username = bitbucketUsername
-               conn.BasicAuth.Password = bitbucketAppPassword
-               conn.Proxy = v.GetString("BITBUCKET_PROXY")
-               conn.RateLimitPerHour = 
v.GetInt("BITBUCKET_API_REQUESTS_PER_HOUR")
+       var result *gorm.DB
+       var bitbucketConns []archived.BitbucketConnection
+       result = db.Find(&bitbucketConns)
+       if result.Error != nil {
+               return errors.Convert(result.Error)
+       }
 
-               err = db.Clauses(clause.OnConflict{DoNothing: 
true}).Create(conn).Error
+       for _, v := range bitbucketConns {
+               conn := &archived.BitbucketConnection{}
+               conn.ID = v.ID
+               conn.Name = v.Name
+               conn.Endpoint = v.Endpoint
+               conn.Proxy = v.Proxy
+               conn.RateLimitPerHour = v.RateLimitPerHour
 
+               c := config.GetConfig()
+               encKey := c.GetString(core.EncodeKeyEnvStr)
+               if encKey == "" {
+                       return errors.BadInput.New("bitbucket invalid encKey")
+               }
+               var auth string
+               if auth, err = core.Decrypt(encKey, 
v.BasicAuth.GetEncodedToken()); err != nil {
+                       return errors.Convert(err)
+               }
+               var pk []byte
+               pk, err = base64.StdEncoding.DecodeString(auth)
                if err != nil {
                        return errors.Default.Wrap(err, "error creating 
connection entry for BitBucket")
                }
+               originInfo := strings.Split(string(pk), ":")
+               if len(originInfo) == 2 {
+                       conn.Username = originInfo[0]
+                       conn.Password, err = core.Encrypt(encKey, originInfo[1])
+                       if err != nil {
+                               return errors.Convert(err)
+                       }
+                       // create
+                       tx := db.Create(&conn)
+                       if tx.Error != nil {
+                               return errors.Default.Wrap(tx.Error, "error 
adding connection to DB")
+                       }
+               }
        }
 
        return nil
diff --git a/plugins/bitbucket/models/migrationscripts/archived/connection.go 
b/plugins/bitbucket/models/migrationscripts/archived/connection.go
index dae6374d..cfd6f6e9 100644
--- a/plugins/bitbucket/models/migrationscripts/archived/connection.go
+++ b/plugins/bitbucket/models/migrationscripts/archived/connection.go
@@ -18,36 +18,36 @@ limitations under the License.
 package archived
 
 import (
-       "github.com/apache/incubator-devlake/plugins/helper"
+       "encoding/base64"
+       "fmt"
+       "github.com/apache/incubator-devlake/models/migrationscripts/archived"
 )
 
-type EpicResponse struct {
-       Id    int
-       Title string
-       Value string
+type BasicAuth struct {
+       Username string `mapstructure:"username" validate:"required" 
json:"username"`
+       Password string `mapstructure:"password" validate:"required" 
json:"password" encrypt:"yes"`
 }
 
-type TestConnectionRequest struct {
-       Endpoint         string `json:"endpoint"`
-       Proxy            string `json:"proxy"`
-       helper.BasicAuth `mapstructure:",squash"`
+func (ba BasicAuth) GetEncodedToken() string {
+       return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", 
ba.Username, ba.Password)))
+}
+
+type BaseConnection struct {
+       Name string `gorm:"type:varchar(100);uniqueIndex" json:"name" 
validate:"required"`
+       archived.Model
 }
 
-type BoardResponse struct {
-       Id    int
-       Title string
-       Value string
+type RestConnection struct {
+       BaseConnection   `mapstructure:",squash"`
+       Endpoint         string `mapstructure:"endpoint" validate:"required" 
json:"endpoint"`
+       Proxy            string `mapstructure:"proxy" json:"proxy"`
+       RateLimitPerHour int    `comment:"api request rate limit per hour" 
json:"rateLimit"`
 }
-type TransformationRules struct {
-       PrType               string `mapstructure:"prType" json:"prType"`
-       PrComponent          string `mapstructure:"prComponent" 
json:"prComponent"`
-       PrBodyClosePattern   string `mapstructure:"prBodyClosePattern" 
json:"prBodyClosePattern"`
-       IssueSeverity        string `mapstructure:"issueSeverity" 
json:"issueSeverity"`
-       IssuePriority        string `mapstructure:"issuePriority" 
json:"issuePriority"`
-       IssueComponent       string `mapstructure:"issueComponent" 
json:"issueComponent"`
-       IssueTypeBug         string `mapstructure:"issueTypeBug" 
json:"issueTypeBug"`
-       IssueTypeIncident    string `mapstructure:"issueTypeIncident" 
json:"issueTypeIncident"`
-       IssueTypeRequirement string `mapstructure:"issueTypeRequirement" 
json:"issueTypeRequirement"`
+
+type TestConnectionRequest struct {
+       Endpoint  string `json:"endpoint"`
+       Proxy     string `json:"proxy"`
+       BasicAuth `mapstructure:",squash"`
 }
 
 type ApiUserResponse struct {
@@ -59,9 +59,8 @@ type ApiUserResponse struct {
 }
 
 type BitbucketConnection struct {
-       helper.RestConnection      `mapstructure:",squash"`
-       helper.BasicAuth           `mapstructure:",squash"`
-       RemotelinkCommitShaPattern string 
`gorm:"type:varchar(255);comment='golang regexp, the first group will be 
recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" 
json:"remotelinkCommitShaPattern"`
+       RestConnection `mapstructure:",squash"`
+       BasicAuth      `mapstructure:",squash"`
 }
 
 func (BitbucketConnection) TableName() string {
diff --git a/plugins/gitee/tasks/pr_review_collector.go 
b/plugins/gitee/tasks/pr_review_collector.go
index 3c69d922..7d2dd412 100644
--- a/plugins/gitee/tasks/pr_review_collector.go
+++ b/plugins/gitee/tasks/pr_review_collector.go
@@ -33,6 +33,7 @@ import (
        "github.com/apache/incubator-devlake/plugins/gitee/models"
 )
 
+// gitee
 const RAW_PULL_REQUEST_REVIEW_TABLE = "gitee_api_pull_request_reviews"
 
 var CollectApiPullRequestReviewsMeta = core.SubTaskMeta{

Reply via email to