Skip to content

Commit

Permalink
Merge pull request #1 from troygeiger/include_organizations
Browse files Browse the repository at this point in the history
Allows retrieval and creation of repositories of organizations.
  • Loading branch information
maikebing committed Feb 25, 2019
2 parents ad6129d + 55e025c commit 7c29c1f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Gitea.API/v1/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ public HttpClient CreateBaseClient()
}

newClient.BaseAddress = BaseUrl;
newClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// I found that if the Authentication header was not first, the API would not except the request.
Authorizer.PrepareClient(newClient);
newClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


return newClient;
}
Expand Down
12 changes: 11 additions & 1 deletion Gitea.API/v1/Repositories/RepositoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class RepositoryBuilder : IDisposable
/// Stores the properties for the request.
/// </summary>
protected IDictionary<string, object> _properties = new Dictionary<string, object>();
string _ownerName;
bool _isOrg = false;

public RepositoryBuilder(User user)
{
Expand Down Expand Up @@ -115,6 +117,14 @@ public RepositoryBuilder MakeReadme(string readme)

return this;
}

public RepositoryBuilder Owner(string name, bool isOrganization)
{
_ownerName = name;
_isOrg = isOrganization;

return this;
}

/// <summary>
/// Starts migration.
Expand All @@ -125,7 +135,7 @@ public async Task<Repository> Start()
using (var rest = User.Endpoint.Client.CreateBaseClient())
{
var resp = await rest.PostAsync(
"user/repos",
_isOrg ? $"org/{_ownerName}/repos" : "user/repos",
new StringContent(ToJson(), Encoding.UTF8, "application/json")
);

Expand Down
29 changes: 29 additions & 0 deletions Gitea.API/v1/Users/RepositoriesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,38 @@ await resp.Content.ReadAsStringAsync()
}
}

resp = await rest.GetAsync("user/orgs");
var orgs = JsonConvert.DeserializeObject<IEnumerable<User>>(await resp.Content.ReadAsStringAsync());
foreach (User org in orgs)
{
resp = await rest.GetAsync($"orgs/{org.Username}/repos");
repoList = JsonConvert.DeserializeObject<IEnumerable<Repository>>
(
await resp.Content.ReadAsStringAsync()
);
foreach (Repository repository in repoList)
{
repository.Owner.Endpoint = User.Endpoint;
userRepos.Add(repository);
}

}

return userRepos;
}
}

public async Task<User[]> GetUserOrginizationsAsync()
{

using (var rest = User.Endpoint.Client.CreateBaseClient())
{
var resp = await rest.GetAsync("user/orgs");
var orgsJson = JsonConvert.DeserializeObject<List<User>>(await resp.Content.ReadAsStringAsync());
return orgsJson.ToArray();
}

}

/// <summary>
/// Starts migrating an external repository.
Expand Down

0 comments on commit 7c29c1f

Please sign in to comment.