Skip to content

Commit

Permalink
add ado server url to gei migrate-repo (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-smith authored Apr 27, 2022
1 parent 8f1cbf9 commit a6f94e2
Show file tree
Hide file tree
Showing 23 changed files with 361 additions and 134 deletions.
45 changes: 25 additions & 20 deletions src/Octoshift/AdoApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ namespace OctoshiftCLI
public class AdoApi
{
private readonly AdoClient _client;
private readonly string _adoBaseUrl;

public AdoApi(AdoClient client) => _client = client;
public AdoApi(AdoClient client, string adoServerUrl)
{
_client = client;
_adoBaseUrl = adoServerUrl?.TrimEnd('/');
}

public virtual async Task<string> GetUserId()
{
Expand Down Expand Up @@ -59,7 +64,7 @@ public virtual async Task<string> GetOrganizationId(string userId, string adoOrg

public virtual async Task<IEnumerable<string>> GetTeamProjects(string org)
{
var url = $"https://dev.azure.com/{org}/_apis/projects?api-version=6.1-preview";
var url = $"{_adoBaseUrl}/{org}/_apis/projects?api-version=6.1-preview";
var data = await _client.GetWithPagingAsync(url);
return data.Select(x => (string)x["name"]).ToList();
}
Expand All @@ -68,7 +73,7 @@ public virtual async Task<IEnumerable<string>> GetTeamProjects(string org)

public virtual async Task<IEnumerable<(string Id, string Name, bool IsDisabled)>> GetRepos(string org, string teamProject)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/git/repositories?api-version=6.1-preview.1";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories?api-version=6.1-preview.1";
var data = await _client.GetWithPagingAsync(url);
return data.Select(x => ((string)x["id"], (string)x["name"], ((string)x["isDisabled"]).ToBool()))
.ToList();
Expand All @@ -95,7 +100,7 @@ public virtual async Task<string> GetGithubAppId(string org, string githubOrg, I

private async Task<string> GetTeamProjectGithubAppId(string org, string githubOrg, string teamProject)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4";
var response = await _client.GetWithPagingAsync(url);

var endpoint = response.FirstOrDefault(x => ((string)x["type"]).Equals("GitHub", StringComparison.OrdinalIgnoreCase) && ((string)x["name"]).Equals(githubOrg, StringComparison.OrdinalIgnoreCase));
Expand All @@ -105,7 +110,7 @@ private async Task<string> GetTeamProjectGithubAppId(string org, string githubOr

public virtual async Task<string> GetGithubHandle(string org, string teamProject, string githubToken)
{
var url = $"https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -137,7 +142,7 @@ public virtual async Task<string> GetGithubHandle(string org, string teamProject

public virtual async Task<(string connectionId, string endpointId, string connectionName, IEnumerable<string> repoIds)> GetBoardsGithubConnection(string org, string teamProject)
{
var url = $"https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -178,7 +183,7 @@ public virtual async Task<string> GetGithubHandle(string org, string teamProject

public virtual async Task<string> CreateBoardsGithubEndpoint(string org, string teamProjectId, string githubToken, string githubHandle, string endpointName)
{
var url = $"https://dev.azure.com/{org}/{teamProjectId}/_apis/serviceendpoint/endpoints?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/{teamProjectId}/_apis/serviceendpoint/endpoints?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -207,7 +212,7 @@ public virtual async Task<string> CreateBoardsGithubEndpoint(string org, string

public virtual async Task AddRepoToBoardsGithubConnection(string org, string teamProject, string connectionId, string connectionName, string endpointId, IEnumerable<string> repoIds)
{
var url = $"https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -245,14 +250,14 @@ public virtual async Task AddRepoToBoardsGithubConnection(string org, string tea

public virtual async Task<string> GetTeamProjectId(string org, string teamProject)
{
var url = $"https://dev.azure.com/{org}/_apis/projects/{teamProject}?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/projects/{teamProject}?api-version=5.0-preview.1";
var response = await _client.GetAsync(url);
return (string)JObject.Parse(response)["id"];
}

public virtual async Task<string> GetRepoId(string org, string teamProject, string repo)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/git/repositories/{repo}?api-version=4.1";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repo}?api-version=4.1";
try
{
var response = await _client.GetAsync(url);
Expand All @@ -261,7 +266,7 @@ public virtual async Task<string> GetRepoId(string org, string teamProject, stri
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// The repo may be disabled, can still get the ID by getting it from the repo list
url = $"https://dev.azure.com/{org}/{teamProject}/_apis/git/repositories?api-version=4.1";
url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories?api-version=4.1";

var response = await _client.GetWithPagingAsync(url);

Expand All @@ -271,14 +276,14 @@ public virtual async Task<string> GetRepoId(string org, string teamProject, stri

public virtual async Task<IEnumerable<string>> GetPipelines(string org, string teamProject, string repoId)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/build/definitions?repositoryId={repoId}&repositoryType=TfsGit";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/build/definitions?repositoryId={repoId}&repositoryType=TfsGit";
var response = await _client.GetWithPagingAsync(url);
return response.Select(x => (string)x["name"]).ToList();
}

public virtual async Task<int> GetPipelineId(string org, string teamProject, string pipeline)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/build/definitions";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/build/definitions";
var response = await _client.GetWithPagingAsync(url);

var result = response.Single(x => ((string)x["name"]).Trim().ToUpper() == pipeline.Trim().ToUpper());
Expand All @@ -287,7 +292,7 @@ public virtual async Task<int> GetPipelineId(string org, string teamProject, str

public virtual async Task ShareServiceConnection(string adoOrg, string adoTeamProject, string adoTeamProjectId, string serviceConnectionId)
{
var url = $"https://dev.azure.com/{adoOrg}/_apis/serviceendpoint/endpoints/{serviceConnectionId}?api-version=6.0-preview.4";
var url = $"{_adoBaseUrl}/{adoOrg}/_apis/serviceendpoint/endpoints/{serviceConnectionId}?api-version=6.0-preview.4";

var payload = new[]
{
Expand All @@ -307,7 +312,7 @@ public virtual async Task ShareServiceConnection(string adoOrg, string adoTeamPr

public virtual async Task<(string DefaultBranch, string Clean, string CheckoutSubmodules)> GetPipeline(string org, string teamProject, int pipelineId)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/build/definitions/{pipelineId}?api-version=6.0";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/build/definitions/{pipelineId}?api-version=6.0";

var response = await _client.GetAsync(url);
var data = JObject.Parse(response);
Expand All @@ -330,7 +335,7 @@ public virtual async Task ShareServiceConnection(string adoOrg, string adoTeamPr

public virtual async Task ChangePipelineRepo(string adoOrg, string teamProject, int pipelineId, string defaultBranch, string clean, string checkoutSubmodules, string githubOrg, string githubRepo, string connectedServiceId)
{
var url = $"https://dev.azure.com/{adoOrg}/{teamProject}/_apis/build/definitions/{pipelineId}?api-version=6.0";
var url = $"{_adoBaseUrl}/{adoOrg}/{teamProject}/_apis/build/definitions/{pipelineId}?api-version=6.0";

var response = await _client.GetAsync(url);
var data = JObject.Parse(response);
Expand Down Expand Up @@ -378,7 +383,7 @@ public virtual async Task ChangePipelineRepo(string adoOrg, string teamProject,

public virtual async Task<string> GetBoardsGithubRepoId(string org, string teamProject, string teamProjectId, string endpointId, string githubOrg, string githubRepo)
{
var url = $"https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -412,7 +417,7 @@ public virtual async Task<string> GetBoardsGithubRepoId(string org, string teamP

public virtual async Task CreateBoardsGithubConnection(string org, string teamProject, string endpointId, string repoId)
{
var url = $"https://dev.azure.com/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1";

var payload = new
{
Expand Down Expand Up @@ -451,7 +456,7 @@ public virtual async Task CreateBoardsGithubConnection(string org, string teamPr

public virtual async Task DisableRepo(string org, string teamProject, string repoId)
{
var url = $"https://dev.azure.com/{org}/{teamProject}/_apis/git/repositories/{repoId}?api-version=6.1-preview.1";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repoId}?api-version=6.1-preview.1";

var payload = new { isDisabled = true };
await _client.PatchAsync(url, payload);
Expand All @@ -471,7 +476,7 @@ public virtual async Task LockRepo(string org, string teamProjectId, string repo
{
var gitReposNamespace = "2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87";

var url = $"https://dev.azure.com/{org}/_apis/accesscontrolentries/{gitReposNamespace}?api-version=6.1-preview.1";
var url = $"{_adoBaseUrl}/{org}/_apis/accesscontrolentries/{gitReposNamespace}?api-version=6.1-preview.1";

var payload = new
{
Expand Down
7 changes: 5 additions & 2 deletions src/Octoshift/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Octoshift.Models;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Models;

namespace OctoshiftCLI
Expand Down Expand Up @@ -142,20 +143,22 @@ public virtual async Task<string> GetOrganizationId(string org)
return (string)data["data"]["organization"]["id"];
}

public virtual async Task<string> CreateAdoMigrationSource(string orgId)
public virtual async Task<string> CreateAdoMigrationSource(string orgId, string adoServerUrl)
{
var url = $"{_apiUrl}/graphql";

var query = "mutation createMigrationSource($name: String!, $url: String!, $ownerId: ID!, $type: MigrationSourceType!)";
var gql = "createMigrationSource(input: {name: $name, url: $url, ownerId: $ownerId, type: $type}) { migrationSource { id, name, url, type } }";

adoServerUrl = adoServerUrl.HasValue() ? adoServerUrl : "https://dev.azure.com";

var payload = new
{
query = $"{query} {{ {gql} }}",
variables = new
{
name = "Azure DevOps Source",
url = "https://dev.azure.com",
url = adoServerUrl,
ownerId = orgId,
type = "AZURE_DEVOPS"
},
Expand Down
2 changes: 1 addition & 1 deletion src/OctoshiftCLI.IntegrationTests/AdoToGithub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AdoToGithub(ITestOutputHelper output)
var adoToken = Environment.GetEnvironmentVariable("ADO_PAT");
_adoHttpClient = new HttpClient();
var adoClient = new AdoClient(logger, _adoHttpClient, new VersionChecker(_versionClient), adoToken);
var adoApi = new AdoApi(adoClient);
var adoApi = new AdoApi(adoClient, "https://dev.azure.com");

var githubToken = Environment.GetEnvironmentVariable("GH_PAT");
_githubHttpClient = new HttpClient();
Expand Down
Loading

0 comments on commit a6f94e2

Please sign in to comment.