Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion backend/plugins/azuredevops_go/api/azuredevops/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Client) GetUserProfile() (Profile, errors.Error) {
return Profile{}, errors.Internal.Wrap(err, "failed to read user accounts")
}

if res.StatusCode == 302 || res.StatusCode == 401 {
if res.StatusCode == 203 || res.StatusCode == 401 {
return Profile{}, errors.Unauthorized.New("failed to read user profile")
}

Expand Down Expand Up @@ -152,6 +152,15 @@ func (c *Client) GetProjects(args GetProjectsArgs) ([]Project, errors.Error) {
if err != nil {
return nil, err
}

if res.StatusCode == 203 || res.StatusCode == 401 {
return nil, errors.Unauthorized.New("failed to read projects")
}

if res.StatusCode != 200 {
return nil, errors.Internal.New(fmt.Sprintf("failed to read projects, upstream api call failed with (%v)", res.StatusCode))
}

err = api.UnmarshalResponse(res, &data)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Basic OnRlc3QtdG9rZW4=
Content-Type: application/json; charset=utf-8; api-version=7.1

Basic OmludmFsaWQtdG9rZW4=
StatusCode: 302
Body: <html><head><title>Object moved</title></head><body>
StatusCode: 203
Body: <html><head><title>Azure DevOps Services | Sign In</title></head><body>
Content-Type: text/html; charset=utf-8
Location: https://app.vssps.visualstudio.com/_signin

Expand Down
59 changes: 41 additions & 18 deletions backend/plugins/azuredevops_go/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package api

import (
"context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
Expand Down Expand Up @@ -52,17 +53,10 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
},
AzuredevopsConn: conn,
}
vsc := azuredevops.NewClient(&connection, nil, "https://app.vssps.visualstudio.com/")

_, err := vsc.GetUserProfile()
body, err := testConnection(context.TODO(), connection)
if err != nil {
return nil, err
return nil, plugin.WrapTestConnectionErrResp(basicRes, err)
}

body := AzuredevopsTestConnResponse{}
body.Success = true
body.Message = "success"

return &plugin.ApiResourceOutput{Body: body, Status: http.StatusOK}, nil
}

Expand All @@ -75,21 +69,15 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/azuredevops/connections/{connectionId}/test [POST]
func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
connection, err := dsHelper.ConnApi.FindByPk(input)
connection, err := dsHelper.ConnApi.GetMergedConnection(input)
if err != nil {
return nil, errors.BadInput.Wrap(err, "can't read connection from database")
}

vsc := azuredevops.NewClient(connection, nil, "https://app.vssps.visualstudio.com/")
_, err = vsc.GetUserProfile()
body, err := testConnection(context.TODO(), *connection)
if err != nil {
return nil, err
return nil, plugin.WrapTestConnectionErrResp(basicRes, err)
}

body := AzuredevopsTestConnResponse{}
body.Success = true
body.Message = "success"

return &plugin.ApiResourceOutput{Body: body, Status: http.StatusOK}, nil
}

Expand Down Expand Up @@ -155,3 +143,38 @@ func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ConnApi.GetDetail(input)
}

func testConnection(ctx context.Context, connection models.AzuredevopsConnection) (*AzuredevopsTestConnResponse, errors.Error) {
// validate
if vld != nil {
if err := vld.Struct(connection); err != nil {
return nil, errors.Default.Wrap(err, "error validating connection")
}
}
apiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &connection)
if err != nil {
return nil, err
}

vsc := azuredevops.NewClient(&connection, apiClient, "https://app.vssps.visualstudio.com/")
org := connection.Organization

if org == "" {
_, err = vsc.GetUserProfile()
} else {
args := azuredevops.GetProjectsArgs{
OrgId: org,
}
_, err = vsc.GetProjects(args)
}
if err != nil {
return nil, err
}

connection = connection.Sanitize()
body := AzuredevopsTestConnResponse{}
body.Success = true
body.Message = "success"

return &body, nil
}
2 changes: 2 additions & 0 deletions backend/plugins/azuredevops_go/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

var vld *validator.Validate
var basicRes context.BasicRes

var dsHelper *api.DsHelper[models.AzuredevopsConnection, models.AzuredevopsRepo, models.AzuredevopsScopeConfig]
var raProxy *api.DsRemoteApiProxyHelper[models.AzuredevopsConnection]
Expand All @@ -34,6 +35,7 @@ var raScopeSearch *api.DsRemoteApiScopeSearchHelper[models.AzuredevopsConnection

func Init(br context.BasicRes, p plugin.PluginMeta) {
vld = validator.New()
basicRes = br
dsHelper = api.NewDataSourceHelper[
models.AzuredevopsConnection,
models.AzuredevopsRepo,
Expand Down
24 changes: 15 additions & 9 deletions backend/plugins/azuredevops_go/api/remote_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func listAzuredevopsRemoteScopes(
err errors.Error,
) {

org := connection.Organization
vsc := azuredevops.NewClient(connection, apiClient, "https://app.vssps.visualstudio.com")

if groupId == "" {
return listAzuredevopsProjects(vsc, page)
return listAzuredevopsProjects(vsc, page, org)
}

id := strings.Split(groupId, idSeparator)
Expand All @@ -76,18 +77,23 @@ func listAzuredevopsRemoteScopes(
return children, nextPage, nil
}

func listAzuredevopsProjects(vsc azuredevops.Client, _ AzuredevopsRemotePagination) (
func listAzuredevopsProjects(vsc azuredevops.Client, _ AzuredevopsRemotePagination, org string) (
children []dsmodels.DsRemoteApiScopeListEntry[models.AzuredevopsRepo],
nextPage *AzuredevopsRemotePagination,
err errors.Error) {

profile, err := vsc.GetUserProfile()
if err != nil {
return nil, nil, err
}
accounts, err := vsc.GetUserAccounts(profile.Id)
if err != nil {
return nil, nil, err
var accounts azuredevops.AccountResponse
if org == "" {
profile, err := vsc.GetUserProfile()
if err != nil {
return nil, nil, err
}
accounts, err = vsc.GetUserAccounts(profile.Id)
if err != nil {
return nil, nil, err
}
} else {
accounts = append(accounts, azuredevops.Account{AccountName: org})
}

g, _ := errgroup.WithContext(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion backend/plugins/azuredevops_go/models/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (at *AzuredevopsAccessToken) SetupAuthentication(req *http.Request) errors.
type AzuredevopsConn struct {
//api.RestConnection `mapstructure:",squash"`
AzuredevopsAccessToken `mapstructure:",squash"`
Organization string
Organization string `json:"organization"`
//Endpoint string `mapstructure:"endpoint" json:"endpoint"`
Proxy string `mapstructure:"proxy" json:"proxy"`
//RateLimitPerHour int `comment:"api request rate limit per hour" json:"rateLimitPerHour"`
Expand Down
12 changes: 11 additions & 1 deletion config-ui/src/api/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const test = (
| 'proxy'
| 'dbUrl'
| 'companyId'
| 'organization'
>
>,
): Promise<IConnectionTestResult> =>
Expand All @@ -61,6 +62,15 @@ export const testOld = (
plugin: string,
payload: Pick<
IConnectionAPI,
'endpoint' | 'authMethod' | 'username' | 'password' | 'token' | 'appId' | 'secretKey' | 'proxy' | 'dbUrl'
| 'endpoint'
| 'authMethod'
| 'username'
| 'password'
| 'token'
| 'appId'
| 'secretKey'
| 'proxy'
| 'dbUrl'
| 'organization'
>,
): Promise<IConnectionOldTestResult> => request(`/plugins/${plugin}/test`, { method: 'post', data: payload });
1 change: 1 addition & 0 deletions config-ui/src/features/connections/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const transformConnection = (plugin: string, connection: IConnectionAPI):
proxy: connection.proxy,
enableGraphql: connection.enableGraphql,
rateLimitPerHour: connection.rateLimitPerHour,
organization: connection.organization,
};
};

Expand Down
2 changes: 2 additions & 0 deletions config-ui/src/plugins/components/connection-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => {
proxy: isEqual(connection?.proxy, values.proxy) ? undefined : values.proxy,
dbUrl: isEqual(connection?.dbUrl, values.dbUrl) ? undefined : values.dbUrl,
companyId: isEqual(connection?.companyId, values.companyId) ? undefined : values.companyId,
organization: isEqual(connection?.organization, values.organization) ? undefined : values.organization,
})
: API.connection.testOld(
plugin,
Expand All @@ -89,6 +90,7 @@ export const ConnectionForm = ({ plugin, connectionId, onSuccess }: Props) => {
'tenantType',
'dbUrl',
'companyId',
'organization',
]),
),
{
Expand Down
17 changes: 10 additions & 7 deletions config-ui/src/plugins/register/azure/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DOC_URL } from '@/release';
import { IPluginConfig } from '@/types';

import Icon from './assets/icon.svg?react';
import { BaseURL } from './connection-fields';
import { BaseURL, ConnectionOrganization } from './connection-fields';

export const AzureConfig: IPluginConfig = {
plugin: 'azuredevops',
Expand Down Expand Up @@ -88,13 +88,16 @@ export const AzureGoConfig: IPluginConfig = {
{
key: 'token',
label: 'Personal Access Token',
subLabel: (
<span>
<ExternalLink link={DOC_URL.PLUGIN.AZUREDEVOPS.AUTH_TOKEN}>Learn about how to create a PAT</ExternalLink>{' '}
Please select ALL ACCESSIBLE ORGANIZATIONS for the Organization field when you create the PAT.
</span>
),
},
({ initialValues, values, setValues }: any) => (
<ConnectionOrganization
initialValue={initialValues}
label="Personal Access Token Scope"
key="ado-organization"
value={values.organization}
setValue={(value) => setValues({ organization: value })}
/>
),
'proxy',
{
key: 'rateLimitPerHour',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
*/

export * from './base-url';
export * from './organization';
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.
*
*/

import React, { useEffect, useState } from 'react';
import { Input, Radio, type RadioChangeEvent } from 'antd';

import { Block, ExternalLink } from '@/components';
import { DOC_URL } from '@/release';

interface Props {
initialValue: OrganizationSettings;
value: string;
label?: string;
setValue: (value: string) => void;
}

interface OrganizationSettings {
organization: string;
scoped: boolean;
}

export const ConnectionOrganization = ({ label, initialValue, value, setValue }: Props) => {
const [settings, setSettings] = useState<OrganizationSettings>({ scoped: false, organization: '' });

useEffect(() => {
const org = initialValue.organization || '';
setValue(org);

setSettings({ organization: initialValue.organization, scoped: org !== '' });
}, [initialValue.organization]);

const handleChange = (e: RadioChangeEvent) => {
const scoped = e.target.value;
if (scoped) {
setValue(settings.organization);
} else {
setValue('');
}
setSettings({ ...settings, scoped });
};

const handleChangeValue = (e: React.ChangeEvent<HTMLInputElement>) => {
const organization = e.target.value;
setValue(organization);
setSettings({ ...settings, organization });
};

return (
<>
<Block title={label || 'Personal Access Token Scope'}>
<p>
If you are using an organization-scoped token, please enter the organization. Otherwise make sure to create an
unscoped token.{' '}
{DOC_URL.PLUGIN.AZUREDEVOPS.AUTH_TOKEN !== '' && (
<ExternalLink link={DOC_URL.PLUGIN.AZUREDEVOPS.AUTH_TOKEN}>Learn about how to create a PAT</ExternalLink>
)}
</p>
<Radio.Group value={settings.scoped} onChange={handleChange}>
<Radio value={false}>Unscoped</Radio>
<Radio value={true}>Scoped</Radio>
</Radio.Group>
</Block>
<Block>
<Input
style={{ width: 386 }}
placeholder="Your organization"
value={value}
onChange={handleChangeValue}
disabled={!settings.scoped}
/>
</Block>
</>
);
};
2 changes: 2 additions & 0 deletions config-ui/src/types/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IConnectionAPI {
companyId?: number;
proxy: string;
rateLimitPerHour?: number;
organization?: string;
}

export interface IConnectionTestResult {
Expand Down Expand Up @@ -85,4 +86,5 @@ export interface IConnection {
companyId?: number;
proxy: string;
rateLimitPerHour?: number;
organization?: string;
}