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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public GithubAuthController() {
"We're unable to get your access token, please try again");
}

var githubClient = new GitHubClient(new ProductHeaderValue("SupportHub"));
var githubClient = new GitHubClient(new ProductHeaderValue("GitCom"));

githubClient.Credentials = new Credentials(accessToken);

Expand Down
2 changes: 1 addition & 1 deletion App/AL/Controller/External/MyIntegrationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MyIntegrationsController() {
JObject githubUser = null;

if (githubToken != null) {
var githubClient = new GitHubClient(new ProductHeaderValue("SupportHub"));
var githubClient = new GitHubClient(new ProductHeaderValue("GitCom"));

githubClient.Credentials = new Credentials(githubToken.access_token);

Expand Down
2 changes: 1 addition & 1 deletion App/AL/Controller/Project/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace App.AL.Controller.Project {
public sealed class ProjectController : BaseController {
protected override IMiddleware[] Middleware() => new IMiddleware[] {};
protected override IMiddleware[] Middleware() => new IMiddleware[] { };

public ProjectController() {
Get("/api/v1/project/get", _ => {
Expand Down
84 changes: 84 additions & 0 deletions App/AL/Controller/Repo/Import/PublicImportController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Linq;
using App.AL.Utils.External.GitHub;
using App.DL.Enum;
using App.DL.External.GitHub;
using App.DL.Repository.Repo;
using App.PL.Transformer.Project;
using App.PL.Transformer.Repo;
using Micron.AL.Validation.Basic;
using Micron.DL.Middleware;
using Micron.DL.Module.Controller;
using Micron.DL.Module.Http;
using Micron.DL.Module.Validator;
using Nancy;
using Newtonsoft.Json.Linq;
using Octokit;

namespace App.AL.Controller.Repo.Import {
public class PublicImportController : BaseController {
protected override IMiddleware[] Middleware() => new IMiddleware[] {};

// TODO: add support for GitLab
public PublicImportController() {
Post("/api/v1/repository/submit/post", _ => {
var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
new ShouldHaveParameters(new[] {"url"}),
});
if (errors.Count > 0) return HttpResponse.Errors(errors);

var url = GetRequestStr("url");

Uri parsedUrl;
try {
parsedUrl = new Uri(url);
}
catch (Exception e) {
return HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Invalid url");
}

string host = parsedUrl.Host;

if (
!(new[] {"github.com"}.Contains(host)) || parsedUrl.Segments.Length < 3
) {
return HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Invalid hostname");
}

RepoServiceType serviceType = host == "github.com" ? RepoServiceType.GitHub : RepoServiceType.GitLab;

(DL.Model.Project.Project project, DL.Model.Repo.Repo repo) result = (null, null);

if (serviceType == RepoServiceType.GitHub) {
var githubClient = GitHubApi.Client();
Repository repo;
try {
repo = githubClient.Repository.Get(
parsedUrl.Segments[1].Replace("/", ""),
parsedUrl.Segments[2].Replace("/", "")
).Result;
}
catch (Exception e) {
return HttpResponse.Error(HttpStatusCode.NotFound, "GitHub repository does not exist");
}

var existingRepo = RepoRepository.Find(repo.Id.ToString(), serviceType);

if (existingRepo != null) {
return HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Project is already imported",
new JObject() {
["project"] = new ProjectTransformer().Transform(existingRepo.Project()),
});
}

result = GitHubRepositoriesUtils.ImportProject(null, repo.Id.ToString());
}

return HttpResponse.Data(new JObject() {
["project"] = new ProjectTransformer().Transform(result.project),
["repository"] = new RepoTransformer().Transform(result.repo)
});
});
}
}
}
8 changes: 5 additions & 3 deletions App/AL/Tasks/Project/ProjectSetUp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public static class ProjectSetUp {
"after each task is finished all funding will be disctributed across all participants based on their effort";

public static void Run(DL.Model.Project.Project project, User creator) {
ProjectTeamMemberRepository.CreateAndGet(project, creator);
ProjectAliasRepository.Create(project, creator.login);
if (creator != null) {
ProjectTeamMemberRepository.CreateAndGet(project, creator);
UserLibraryItemRepository.FindOrCreate(project.Creator(), project);
}
ProjectAliasRepository.Create(project, project.name);
ProjectWorkUtils.SetUp(project);

// Basic boards set up
Expand All @@ -25,7 +28,6 @@ public static void Run(DL.Model.Project.Project project, User creator) {
CardRepository.CreateAndGet(
"Example card", DefaultCardDescription, 1, todoColumn, creator
);
UserLibraryItemRepository.FindOrCreate(project.Creator(), project);
}
}
}
19 changes: 11 additions & 8 deletions App/AL/Utils/External/GitHub/GitHubRepositoriesUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,27 @@ public static JArray GetOrgMembers(string orgName) {

public static (Project project, Repo repo) ImportProject(User me, string originId) {
var externalRepo = GetClient().Repository.Get(Convert.ToInt64(originId)).Result;
User creator = null;

if (me != null) {
var token = me.ServiceAccessToken(ServiceType.GitHub);

if (token.origin_user_id == "") {
GitHubUserUtils.UpdateOriginUserId(me);
}

var token = me.ServiceAccessToken(ServiceType.GitHub);
var originUserId = token.origin_user_id;

if (token.origin_user_id == "") {
GitHubUserUtils.UpdateOriginUserId(me);
creator = externalRepo.Owner.Id == Convert.ToInt64(originUserId) ? me : null;
}

Repo repository = RepoRepository.CreateAndGet(
me, externalRepo.Name, externalRepo.HtmlUrl, RepoServiceType.GitHub, externalRepo.Id.ToString()
);

var originUserId = token.origin_user_id;

User creator = externalRepo.Owner.Id == Convert.ToInt64(originUserId) ? me : null;

var project = ProjectRepository.FindOrCreate(repository.title, creator, repository);

project.UpdateCol("description", externalRepo.Description);
if (externalRepo.Description != null) project.UpdateCol("description", externalRepo.Description);

return (project, repository);
}
Expand Down
2 changes: 1 addition & 1 deletion App/AL/Utils/External/GitHub/GitHubUserUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static (bool finished, string originUserId) UpdateOriginUserId(User me) {
return (false, "");
}

var client = new GitHubClient(new ProductHeaderValue("SupportHub"));
var client = new GitHubClient(new ProductHeaderValue("GitCom"));
client.Credentials = new Credentials(serviceAccessToken.access_token);

var githubUser = client.User.Current().Result;
Expand Down
13 changes: 13 additions & 0 deletions App/DL/External/GitHub/GitHubApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Micron.DL.Module.Config;
using Octokit;

namespace App.DL.External.GitHub {
public static class GitHubApi {
public static GitHubClient Client() {
var client = new GitHubClient(new ProductHeaderValue("GitCom"));
var githubToken = AppConfig.GetConfiguration("auth:external:github:token");
if (githubToken != null) client.Credentials = new Credentials(githubToken);
return client;
}
}
}
4 changes: 2 additions & 2 deletions App/DL/Model/Board/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static int Create(string name, string description, ProjectModel project,
return ExecuteScalarInt(
@"INSERT INTO boards(guid, name, description, project_id, creator_id)
VALUES (@guid, @name, @description, @project_id, @creator_id); SELECT currval('boards_id_seq');"
, new {guid = Guid.NewGuid().ToString(), name, description, project_id = project.id, creator_id = creator.id}
, new {guid = Guid.NewGuid().ToString(), name, description, project_id = project.id, creator_id = creator?.id}
);
}

Expand All @@ -70,7 +70,7 @@ public Board Save() {

public ProjectModel Project() => ProjectModel.Find(project_id);

public UserModel User() => UserModel.Find(creator_id);
public UserModel Creator() => UserModel.Find(creator_id);

public BoardColumn.BoardColumn[] Columns(int limit = 10)
=> Connection().Query<BoardColumn.BoardColumn>(
Expand Down
5 changes: 4 additions & 1 deletion App/DL/Model/Card/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public static int Create(
return ExecuteScalarInt(
@"INSERT INTO cards(guid, name, description, column_order, column_id, creator_id)
VALUES (@guid, @name, @description, @column_order, @column_id, @creator_id); SELECT currval('cards_id_seq');"
, new {guid = Guid.NewGuid().ToString(), name, description, column_order = columnOrder, column_id = column.id, creator_id = creator.id}
, new {
guid = Guid.NewGuid().ToString(), name, description, column_order = columnOrder,
column_id = column.id, creator_id = creator?.id
}
);
}

Expand Down
9 changes: 6 additions & 3 deletions App/DL/Model/Repo/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace App.DL.Model.Repo {
public class Repo : Micron.DL.Model.Model {
public int id;

public int creator_id;
public int? creator_id;

public string guid;

Expand All @@ -26,7 +26,10 @@ public class Repo : Micron.DL.Model.Model {

public DateTime created_at;

public UserModel Creator() => UserRepository.Find(creator_id);
public UserModel Creator() {
int creatorId = creator_id ?? 0;
return creatorId > 0 ? UserRepository.Find(creatorId) : null;
}

public static Repo Find(int id)
=> Connection().Query<Repo>(
Expand Down Expand Up @@ -66,7 +69,7 @@ public static int Create(
VALUES (@creator_id, @guid, @title, @repo_url, '{serviceType.ToString()}', @origin_id);
SELECT currval('repositories_id_seq');"
, new {
creator_id = creator.id, guid = Guid.NewGuid().ToString(), title, repo_url = repoUrl,
creator_id = creator?.id, guid = Guid.NewGuid().ToString(), title, repo_url = repoUrl,
origin_id = originId
}
);
Expand Down
2 changes: 1 addition & 1 deletion App/DL/Repository/Board/BoardRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static BoardModel Find(ProjectModel project, UserModel creator) {
return BoardModel.Find(project, creator);
}

public static BoardModel CreateAndGet(string name, string description, ProjectModel project, UserModel user) {
public static BoardModel CreateAndGet(string name, string description, ProjectModel project, UserModel user = null) {
return BoardModel.Find(BoardModel.Create(name, description, project, user));
}

Expand Down
2 changes: 1 addition & 1 deletion App/PL/Transformer/Board/BoardTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override JObject Transform(object obj) {
["name"] = item.name,
["description"] = item.description,
["project_guid"] = item.Project().guid,
["user_guid"] = item.User().guid,
["user_guid"] = item.Creator()?.guid,
["created_at"] = item.created_at.ToString("d"),
["updated_at"] = item.updated_at.ToString("d")
};
Expand Down
2 changes: 1 addition & 1 deletion App/PL/Transformer/Card/CardTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override JObject Transform(object obj)
["guid"] = item.guid,
["name"] = item.name,
["description"] = item.description,
["creator_guid"] = item.Creator().guid,
["creator_guid"] = item.Creator()?.guid,
["column_order"] = item.column_order,
["column_guid"] = item.Column().guid,
["created_at"] = item.created_at,
Expand Down
2 changes: 1 addition & 1 deletion App/PL/Transformer/User/Badge/UserBadgeTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public override JObject Transform(object obj) {
var item = (UserBadge) obj;
return new JObject() {
["guid"] = item.guid,
["user_guid"] = item.User().id,
["user_guid"] = item.User().guid,
["badge"] = item.badge,
["created_at"] = item.created_at.ToString("d"),
};
Expand Down
1 change: 1 addition & 0 deletions App/PL/Transformer/User/UserTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace App.PL.Transformer.User {
public class UserTransformer : BaseTransformer {
public override JObject Transform(object obj) {
var item = (UserModel) obj;
if (item == null) return null;
return new JObject {
["guid"] = item.guid,
["login"] = item.login,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Phinx\Migration\AbstractMigration;

class AlterReposMakeCreatorIdNullable extends AbstractMigration
{
public function change()
{
$this->getAdapter()->commitTransaction();
$this->execute("ALTER TABLE repositories ALTER COLUMN creator_id DROP NOT NULL;");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Phinx\Migration\AbstractMigration;

class AlterBoardsAndCardsMakeCreatorIdNullable extends AbstractMigration
{
public function change()
{
$this->execute("ALTER TABLE boards ALTER COLUMN creator_id DROP NOT NULL;");
$this->execute("ALTER TABLE cards ALTER COLUMN creator_id DROP NOT NULL;");
}
}