You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by "klesh (via GitHub)" <gi...@apache.org> on 2023/02/17 02:00:57 UTC

[GitHub] [incubator-devlake] klesh commented on a diff in pull request #4435: feat: bamboo connection

klesh commented on code in PR #4435:
URL: https://github.com/apache/incubator-devlake/pull/4435#discussion_r1109192509


##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {

Review Comment:
   I believe this is unnecessary, the `api.AccessToken` has already been implemented this way.
   You don't need to overwrite it unless it works differently



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {

Review Comment:
   Is this for Test Connection? PrepareApiClient is designed for APIs that require a Session Token (i.e. Zentao and Feishu).
   However, I think this may be a good idea, we can try if it works for whole system.



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {
+	header := http.Header{}
+	header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+
+	res, err := apiClient.Get("", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))

Review Comment:
   why 500? 500 means the server has a fatal error, this is caused by user input, should be a 400



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {
+	header := http.Header{}
+	header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+
+	res, err := apiClient.Get("", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))
+	}
+	resBody1 := &ApiXMLResourcesResponse{}
+
+	if res.StatusCode != http.StatusOK {
+		return errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", res.StatusCode))
+	}
+
+	err = api.UnmarshalResponseXML(res, resBody1)
+
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("UnmarshalResponse failed %s", err.Error()))
+	}
+
+	if resBody1.Link.Href != conn.Endpoint {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Response Data error for connection endpoint: %s, it should like: http://{domain}/rest/api/latest/", conn.Endpoint))

Review Comment:
   no 500



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {
+	header := http.Header{}
+	header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+
+	res, err := apiClient.Get("", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))
+	}
+	resBody1 := &ApiXMLResourcesResponse{}
+
+	if res.StatusCode != http.StatusOK {
+		return errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", res.StatusCode))
+	}
+
+	err = api.UnmarshalResponseXML(res, resBody1)
+
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("UnmarshalResponse failed %s", err.Error()))
+	}
+
+	if resBody1.Link.Href != conn.Endpoint {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Response Data error for connection endpoint: %s, it should like: http://{domain}/rest/api/latest/", conn.Endpoint))
+	}
+
+	res, err = apiClient.Get("repository", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))

Review Comment:
   same as above



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {
+	header := http.Header{}
+	header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+
+	res, err := apiClient.Get("", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))
+	}
+	resBody1 := &ApiXMLResourcesResponse{}
+
+	if res.StatusCode != http.StatusOK {
+		return errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", res.StatusCode))
+	}
+
+	err = api.UnmarshalResponseXML(res, resBody1)
+
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("UnmarshalResponse failed %s", err.Error()))
+	}
+
+	if resBody1.Link.Href != conn.Endpoint {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Response Data error for connection endpoint: %s, it should like: http://{domain}/rest/api/latest/", conn.Endpoint))
+	}
+
+	res, err = apiClient.Get("repository", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))
+	}
+	resBody2 := &ApiRepository{}
+
+	if res.StatusCode != http.StatusOK {
+		return errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", res.StatusCode))
+	}
+
+	err = api.UnmarshalResponse(res, resBody2)
+
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("UnmarshalResponse repository failed %s", err.Error()))

Review Comment:
   same as above



##########
backend/plugins/bamboo/models/connection.go:
##########
@@ -0,0 +1,134 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package models
+
+import (
+	"encoding/xml"
+	"fmt"
+	"net/http"
+
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+	"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
+)
+
+type BambooConnection struct {
+	api.BaseConnection `mapstructure:",squash"`
+	BambooConn         `mapstructure:",squash"`
+}
+
+// TODO Please modify the following code to fit your needs
+// This object conforms to what the frontend currently sends.
+type BambooConn struct {
+	api.RestConnection `mapstructure:",squash"`
+	//TODO you may need to use helper.BasicAuth instead of helper.AccessToken
+	api.AccessToken `mapstructure:",squash"`
+}
+
+// this function is used to rewrite the same function of AccessToken
+func (conn *BambooConn) SetupAuthentication(request *http.Request) errors.Error {
+	request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+	return nil
+}
+
+// PrepareApiClient test api and set the IsPrivateToken,version,UserId and so on.
+func (conn *BambooConn) PrepareApiClient(apiClient apihelperabstract.ApiClientAbstract) errors.Error {
+	header := http.Header{}
+	header.Set("Authorization", fmt.Sprintf("Bearer %v", conn.Token))
+
+	res, err := apiClient.Get("", nil, header)
+	if err != nil {
+		return errors.HttpStatus(500).New(fmt.Sprintf("Get failed %s", err.Error()))
+	}
+	resBody1 := &ApiXMLResourcesResponse{}

Review Comment:
   can we use some meaningful names?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org