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
1 change: 0 additions & 1 deletion App/AL/AppBoot.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using App.DL.Module.Cache;
using Micron.DL.Model;

namespace App.AL {
public static class AppBoot {
Expand Down
60 changes: 27 additions & 33 deletions App/AL/Schedule/Project/Post/SyncReleases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,40 @@ public SyncReleases() {
var githubToken = AppConfig.GetConfiguration("auth:external:github:token");
if (githubToken != null) githubClient.Credentials = new Credentials(githubToken);

int pageIndex = 1;
var repos = Repo.Paginate(pageIndex);
var repos = Repo.GetRandom(50);

while (repos.Length > 0) {
foreach (var repo in repos) {
foreach (var repo in repos) {
try {
if (repo.service_type != RepoServiceType.GitHub) continue;
var splitUrl = repo.repo_url.Split("/");
IEnumerable<Release> releases;
try {
if (repo.service_type != RepoServiceType.GitHub) continue;
var splitUrl = repo.repo_url.Split("/");
IEnumerable<Release> releases;
try {
releases = githubClient.Repository.Release.GetAll(
splitUrl[3], splitUrl[4]
).Result.OrderBy(x => x.Id);
}
catch (Exception e) {
continue; // ignored
}
releases = githubClient.Repository.Release.GetAll(
splitUrl[3], splitUrl[4]
).Result.OrderBy(x => x.Id);
}
catch (Exception e) {
continue; // ignored
}

foreach (var release in releases) {
if (release.Body.Length < 100) continue;
foreach (var release in releases) {
if (release.Body.Length < 100) continue;

var existingPost = ProjectPost.FindBy("origin_id", release.Id.ToString());
if (existingPost != null) continue;
var existingPost = ProjectPost.FindBy("origin_id", release.Id.ToString());
if (existingPost != null) continue;

var post = ProjectPost.Create(
repo.Project(), $"Released {release.Name}", release.Body
);
post.UpdateCol("origin_id", release.Id.ToString());
post.UpdateCol(
"created_at", release.PublishedAt.Value.ToUnixTimeSeconds().ToString()
);
}
}
catch (Exception e) {
SentrySdk.CaptureException(e);
var post = ProjectPost.Create(
repo.Project(), $"Released {release.Name}", release.Body
);
post.UpdateCol("origin_id", release.Id.ToString());
post.UpdateCol(
"created_at", release.PublishedAt.Value.ToUnixTimeSeconds().ToString()
);
}
}

++pageIndex;
repos = Repo.Paginate(pageIndex);
catch (Exception e) {
SentrySdk.CaptureException(e);
}
}
});
JobsPool.Get().Push(task);
Expand Down
27 changes: 27 additions & 0 deletions App/AL/Utils/External/Discord/DiscordWebhooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Micron.DL.Module.Config;

namespace App.AL.Utils.External.Discord {
public static class DiscordWebhooks {
public static void SendEvent(string type, string content) {
Task.Run(() => {
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");

var webhookUrl = AppConfig.GetConfiguration($"external:discord:webhook_tokens:{type}");

if (string.IsNullOrEmpty(webhookUrl)) return;

var response = client.PostAsync(
webhookUrl,
new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("content", content),
})
).Result;
});
}
}
}
11 changes: 9 additions & 2 deletions App/DL/Model/Repo/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static Repo Find(string originId, RepoServiceType type)
$"SELECT * FROM repositories WHERE origin_id = @origin_id AND service_type = '{type.ToString()}' LIMIT 1",
new {origin_id = originId}
).FirstOrDefault();

public static Repo[] GetRandom(int limit = 10)
=> Connection().Query<Repo>(
"SELECT * FROM repositories ORDER BY random() LIMIT @limit", new {limit}
).ToArray();

public static Repo[] Paginate(int page, int size = 20)
=> Connection().Query<Repo>(
Expand Down Expand Up @@ -84,8 +89,10 @@ public Repo Save() {
return this;
}

public Octokit.Repository GithubRepo() =>
GitHubApi.Client().Repository.Get(Convert.ToInt64(origin_id)).Result;
public Octokit.Repository GithubRepo() {
var originId = Convert.ToInt64(origin_id == "" ? "0" : origin_id);
return GitHubApi.Client().Repository.Get(originId).Result;
}

public Project.Project Project() => Model.Project.Project.FindBy("repository_id", id);

Expand Down
7 changes: 5 additions & 2 deletions App/DL/Repository/Alias/ProjectAliasRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public static ProjectAlias Create(Model.Project.Project project) {
var alias = PrepareAlias(project.name);

var creator = project.Creator();
var owner = creator != null ?
PrepareAlias(creator.login) : PrepareAlias(project.Repository().GithubRepo().Owner.Login);

var owner = creator != null ? PrepareAlias(creator.login) : "_";
#if !DEBUG
if (creator == null) owner = PrepareAlias(project.Repository().GithubRepo().Owner.Login);
#endif

var newAlias = alias;
var postfix = 0;
Expand Down
5 changes: 4 additions & 1 deletion App/DL/Repository/Funding/InvoiceRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using App.AL.Utils.External.Discord;
using App.DL.Enum;
using App.DL.Model.Funding;
using App.DL.Repository.User;
Expand All @@ -18,7 +19,9 @@ public static Invoice Create(
UserBalanceRepository.FindOrCreate(user, currencyType);
break;
}
return Find(Invoice.Create(user, entityId, entityType, amount, currencyType, status, wallet));
var invoice = Find(Invoice.Create(user, entityId, entityType, amount, currencyType, status, wallet));
DiscordWebhooks.SendEvent("system-events", $"New invoice #{invoice.id} was created for user: {invoice.user_id}");
return invoice;
}

public static Invoice UpdateStatus(Invoice invoice, InvoiceStatus status) {
Expand Down
7 changes: 5 additions & 2 deletions App/DL/Repository/User/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using App.AL.Utils.External.Discord;
using App.DL.Enum;
using App.DL.Model.Funding;
using App.DL.Model.User;
Expand Down Expand Up @@ -58,7 +59,7 @@ public static UserModel FindOrCreateByEmailAndLogin(
}

user ??= Create(email, login, password);

UserBadge.Create(user, "Early adopter");

int tokenRegisterBonus = System.Convert.ToInt32(
Expand All @@ -78,7 +79,9 @@ public static UserModel FindOrCreateByEmailAndLogin(
}

public static UserModel Create(string email, string login, string password) {
return Find(UserModel.Create(email, login, password));
var newUserId = UserModel.Create(email, login, password);
DiscordWebhooks.SendEvent("system-events", $"New user #{newUserId} just signed up! Email: {email}");
return Find(newUserId);
}
}
}
2 changes: 1 addition & 1 deletion docker/build_and_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ docker build -t registry.gitlab.com/mxss/gitcom-api/gitcom-backend:"$1" -f docke
echo "Pushing image to registy"
docker push registry.gitlab.com/mxss/gitcom-api/gitcom-backend:"$1"

echo "Done. version $1 is built and pushed."
echo "Done. version $1 is built and pushed."