blob: fa604e3e898091900fde7a2231c9013ac62b3450 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#!perl
use strict;
use warnings;
use autodie;
use JSON qw(decode_json encode_json);
use LWP::UserAgent qw();
use Path::Tiny qw(path);
$| = 1;
my $gitlab_group_path = $ENV{GITLAB_GROUP_PATH} or die "GITLAB_GROUP_PATH is not set";
my $gitlab_url = $ENV{GITLAB_URL} or die "GITLAB_URL is not set";
my $gitlab_token = $ENV{GITLAB_TOKEN} or die "GITLAB_TOKEN is not set";
my $slack_workspace = $ENV{SLACK_WORKSPACE} or die "SLACK_WORKSPACE is not set";
my $slack_logs_dir = $ENV{SLACK_LOGS_DIR} // q{};
my $gitlab_graphql_api = "${gitlab_url}/api/graphql";
die "Usage: $0 <filter>" if scalar @ARGV != 1;
my $filter = qr/$ARGV[0]/;
my %slack_emojis;
my %gitlab_emojis;
my $ua = LWP::UserAgent->new(timeout => 5);
$ua->default_header('Authorization' => "Bearer $gitlab_token");
$ua->default_header('Content-Type' => 'application/json; charset=utf-8');
if (!$slack_logs_dir) {
if ($^O eq "darwin") {
my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
$slack_logs_dir = "/Users/$username/Library/Application Support/Slack/logs/default";
} else {
die "SLACK_LOGS_DIR is not set";
}
}
if (!path($slack_logs_dir)->exists or !path($slack_logs_dir)->is_dir) {
die "Slack logs directory not found: $slack_logs_dir";
}
foreach (path($slack_logs_dir)->children(qr/\.log$/)) {
open my ($log_file), $_;
foreach (<$log_file>) {
if (m{fetch (https://emoji\.slack-edge\.com/$slack_workspace/([a-zA-Z-_0-9]+)\/\w+\.(png|gif|jpg))}gm) {
$slack_emojis{$2} = $1;
}
}
close $log_file;
}
{
my $next_cursor = "";
my $has_next_page = 1;
while($has_next_page) {
my $content = encode_json({
query => qq{
{
group(fullPath: "$gitlab_group_path") {
customEmoji(first: 50, after: "$next_cursor") {
nodes { id name url }
pageInfo { endCursor hasNextPage }
}
}
}
}
});
my $res = $ua->post( $gitlab_graphql_api, Content => $content );
die "failed" if $res->is_error or $res->code != 200;
my $data = decode_json($res->decoded_content);
$next_cursor = $data->{data}{group}{customEmoji}{pageInfo}{endCursor};
$has_next_page = $data->{data}{group}{customEmoji}{pageInfo}{hasNextPage};
foreach(@{$data->{data}{group}{customEmoji}{nodes}}) {
$gitlab_emojis{$_->{name}} = {
url => $_->{url},
id => $_->{id},
};
}
}
}
foreach (sort keys %slack_emojis) {
next if $_ !~ $filter;
print "Emoji: $_\n";
if (exists $gitlab_emojis{$_}) {
print " - already exists in GitLab\n";
if ($slack_emojis{$_} eq $gitlab_emojis{$_}{url}) {
print " - skipping, URL is the same\n";
next;
}
my $content = encode_json({
query => qq{
mutation {
destroyCustomEmoji(input: {
id: "$gitlab_emojis{$_}{id}"
}) { errors }
}
}
});
my $res = $ua->post( $gitlab_graphql_api, Content => $content );
if ($res->is_error or $res->code != 200) {
print " - failed to delete\n";
next;
}
my $data = decode_json($res->decoded_content);
if ($data->{errors}) {
print " - failed to delete\n";
next;
}
}
my $content = encode_json({
query => qq{
mutation {
createCustomEmoji(input: {
groupPath: "$gitlab_group_path",
name: "$_",
url: "$slack_emojis{$_}"
}) { errors }
}
}
});
my $res = $ua->post( $gitlab_graphql_api, Content => $content );
if ($res->is_error or $res->code != 200) {
print " - failed to create\n";
} else {
my $data = decode_json($res->decoded_content);
if ($data->{errors}) {
print " - failed to create\n";
} else {
print " - created\n";
}
}
}
|