Skip to content

Commit 00be62c

Browse files
committed
sync issues background job
1 parent 5431406 commit 00be62c

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using App.AL.Middleware.Schedule;
5+
using App.DL.External.GitHub;
6+
using App.DL.Model.Card;
7+
using App.DL.Module.Schedule;
8+
using App.DL.Repository.Card;
9+
using Micron.DL.Middleware;
10+
using Micron.DL.Module.Controller;
11+
using Micron.DL.Module.Http;
12+
using Newtonsoft.Json.Linq;
13+
using Octokit;
14+
using Sentry;
15+
16+
namespace App.AL.Schedule.Sync.Issues {
17+
public class SyncIssues : BaseController {
18+
protected override IMiddleware[] Middleware() => new IMiddleware[] {
19+
new ScheduleAuth(),
20+
};
21+
22+
public SyncIssues() {
23+
Post("/api/v1/schedule/issues/sync/start", _ => {
24+
var task = Task.Run(() => {
25+
var githubClient = GitHubApi.Client();
26+
27+
int pageIndex = 1;
28+
var projects = DL.Model.Project.Project.Paginate(pageIndex);
29+
30+
while (projects.Length > 0) {
31+
foreach (var project in projects) {
32+
var board = project.Boards().First(x => x.name == "Development");
33+
if (board == null) continue;
34+
35+
var todoColumn = board.Columns().First(c => c.name == "TODO");
36+
if (todoColumn == null) continue;
37+
38+
try {
39+
var originId = project.Repository().origin_id;
40+
Console.WriteLine($"getting issues for repo {originId}");
41+
var issues = githubClient.Issue.GetAllForRepository(
42+
Convert.ToInt64(originId)
43+
, new ApiOptions() {
44+
PageSize = 100
45+
}
46+
).Result;
47+
foreach (var issue in issues) {
48+
try {
49+
Console.WriteLine($"Checking issue {issue.HtmlUrl}");
50+
var existingCard = Card.FindBy("origin_id", issue.Id.ToString());
51+
if (existingCard != null) continue;
52+
var card = CardRepository.CreateAndGet(
53+
issue.Title, issue.Body ?? "", 1, todoColumn, null
54+
);
55+
card.UpdateCol("origin_id", issue.Id.ToString());
56+
}
57+
catch (Exception e) {
58+
Console.WriteLine(e.Message);
59+
}
60+
}
61+
}
62+
catch (Exception e) {
63+
Console.WriteLine(e.Message);
64+
SentrySdk.CaptureException(e);
65+
}
66+
}
67+
68+
++pageIndex;
69+
projects = DL.Model.Project.Project.Paginate(pageIndex);
70+
}
71+
72+
Console.WriteLine("Finished!");
73+
});
74+
JobsPool.Get().Push(task);
75+
return HttpResponse.Data(new JObject());
76+
});
77+
}
78+
}
79+
}

App/DL/Model/Card/Card.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Card : Micron.DL.Model.Model {
2424

2525
public int column_id;
2626

27+
public string origin_id;
28+
2729
public DateTime created_at;
2830

2931
public DateTime updated_at;
@@ -46,6 +48,11 @@ public static Card Find(string name, UserModel creator, BoardColumnModel column)
4648
}
4749
).FirstOrDefault();
4850
}
51+
52+
public static Card FindBy(string col, string val)
53+
=> Connection().Query<Card>(
54+
$"SELECT * FROM cards WHERE {col} = @val LIMIT 1", new {val}
55+
).FirstOrDefault();
4956

5057
public static int Create(
5158
string name, string description, int columnOrder, BoardColumnModel column, UserModel creator
@@ -69,6 +76,22 @@ public Card Save() {
6976
);
7077
return this;
7178
}
79+
80+
public Card UpdateCol(string col, string val) {
81+
ExecuteSql(
82+
$"UPDATE cards SET {col} = @val, updated_at = CURRENT_TIMESTAMP WHERE id = @id",
83+
new {val, id}
84+
);
85+
return this;
86+
}
87+
88+
public Card UpdateCol(string col, int val) {
89+
ExecuteSql(
90+
$"UPDATE cards SET {col} = @val, updated_at = CURRENT_TIMESTAMP WHERE id = @id",
91+
new {val, id}
92+
);
93+
return this;
94+
}
7295

7396
public Card Refresh() => Find(id);
7497

App/DL/Model/Project/Project.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Project : Micron.DL.Model.Model {
2929
public string description;
3030

3131
public int repository_id;
32-
32+
3333
public DateTime created_at;
3434

3535
public DateTime updated_at;
@@ -156,6 +156,8 @@ public int StarsCount() => ExecuteScalarInt(
156156
"SELECT COUNT(*) FROM user_projects_library WHERE project_id = @id", new {id}
157157
);
158158

159+
public bool IsConfirmed() => creator_id > 0;
160+
159161
public Image.Image[] Images() => ProjectImage.Get(this).Select(x => x.Image()).ToArray();
160162

161163
public ProjectPost[] Posts() => ProjectPost.Get(this);

App/PL/Transformer/Project/ProjectTransformer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public override JObject Transform(object obj) {
1313
["creator_guid"] = item.Creator()?.guid,
1414
["base_uri"] = null,
1515
["stars_count"] = item.StarsCount(), // TODO: optimize
16+
["confirmed"] = item.IsConfirmed(),
1617
["created_at"] = item.created_at.ToString("d"),
1718
["updated_at"] = item.created_at.ToString("d")
1819
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using App.DL.Repository.Project;
2+
using App.PL.Transformer.Project;
3+
using NUnit.Framework;
4+
using Tests.Testing;
5+
using Tests.Utils.Fake.Repo;
6+
7+
namespace Tests.App.PL.Transformer.Project {
8+
public class ProjectTransformerTests : BaseTestFixture {
9+
[Test]
10+
public void Transform_ProjectWithoutCreator_WithoutExceptions() {
11+
var project = ProjectRepository.FindOrCreate("some proj", null, RepoFaker.Create());
12+
new ProjectTransformer().Transform(project);
13+
}
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
4+
use Phinx\Migration\AbstractMigration;
5+
6+
class AlterCardsAddOriginId extends AbstractMigration
7+
{
8+
public function change()
9+
{
10+
$table = $this->table('cards');
11+
$table->addColumn('origin_id', 'string', ['null' => true])
12+
->save();
13+
}
14+
}

0 commit comments

Comments
 (0)