Skip to content
Snippets Groups Projects
Verified Commit 66f3b2b1 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Podpora chobotnice

parent ae2acb59
No related branches found
No related tags found
No related merge requests found
Pipeline #8223 passed
**
!lib
!public
!script
!templates
!r_v_vote.conf
_work
docker-compose.yaml
image: docker:19.03.1 image: docker:20.10.9
variables: variables:
DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_CERTDIR: "/certs"
IMAGE_TAG: $CI_REGISTRY_IMAGE:latest
services: services:
- docker:19.03.1-dind - docker:20.10.9-dind
before_script: before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
...@@ -13,5 +12,8 @@ before_script: ...@@ -13,5 +12,8 @@ before_script:
build: build:
stage: build stage: build
script: script:
- docker build -t $IMAGE_TAG . - VERSION=`cat VERSION`
- docker push $IMAGE_TAG - docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$VERSION --tag $CI_REGISTRY_IMAGE:latest .
- docker push $CI_REGISTRY_IMAGE:$VERSION
- docker push $CI_REGISTRY_IMAGE:latest
FROM docker-registry.pirati.cz/ramaseuski/docker-mojolicious FROM alpine:latest
RUN apk update && apk add \
wget \
make \
gcc \
perl-dev \
perl-mojolicious \
perl-json \
perl-yaml \
perl-io-socket-ssl \
perl-app-cpanminus
RUN cpanm GraphQL::Client
ADD . /opt/rvvote ADD . /opt/rvvote
USER daemon
EXPOSE 3000 EXPOSE 3000
WORKDIR /opt/rvvote WORKDIR /opt/rvvote
CMD ./script/rvvote daemon CMD ./script/rvvote daemon
1.1.0
package RVVote::Chobotnice;
use strict;
use warnings;
use utf8;
use GraphQL::Client;
use YAML;
our $VERSION = '0.01';
sub new {
my $classname = shift;
my $url = shift;
my $self = {};
$self->{client} = GraphQL::Client->new(url => $url);
bless ($self, $classname);
return $self;
}
sub client {
my $self = shift;
return $self->{client};
}
sub get_group_members {
my $self = shift;
my $group_id = shift;
my $query = qq[
query MyQuery {
group(id: "$group_id") {
memberships {
person {
username
displayName
officialLastName
}
}
}
}
];
my $res = $self->client->execute($query);
return undef if $res->{errors};
return undef if ! $res->{data};
my @members = ();
MEMBER:
foreach my $member ( @{ $res->{data}{group}{memberships} } ) {
push @members, {
fullname => $member->{person}{displayName},
user_id => lc($member->{person}{username}),
lastname => $member->{person}{officialLastName},
};
}
return sort { $a->{lastname} cmp $b->{lastname} } @members;
}
1;
package RVVote::Controller::RV; package RVVote::Controller::RV;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use RVVote::GraphAPI; use RVVote::IAPI;
use RVVote::Chobotnice;
use MIME::Base64 qw(decode_base64url); use MIME::Base64 qw(decode_base64url);
use JSON; use JSON;
...@@ -11,7 +12,7 @@ sub index { ...@@ -11,7 +12,7 @@ sub index {
sub members { sub members {
my $c = shift; my $c = shift;
my @members = (); my @members = ();
if ($c->param('election')) { if ($c->param('election')) {
...@@ -25,11 +26,17 @@ sub members { ...@@ -25,11 +26,17 @@ sub members {
} }
} @{ $election } } @{ $election }
}; };
} }
if ( ! scalar @members ) { if ( ! scalar @members ) {
my $gapi = RVVote::GraphAPI->new($c->config->{graph_api}{url}); if ( $ENV{USERS_BACKEND} eq 'iapi' ) {
@members = $gapi->get_group_members($c->config->{graph_api}{rv_gid}); my $iapi = RVVote::IAPI->new($c->config->{iapi}{url});
@members = $iapi->get_group_members($c->config->{iapi}{rv_gid});
}
elsif ( $ENV{USERS_BACKEND} eq 'chobotnice' ) {
my $chobotnice = RVVote::Chobotnice->new($c->config->{chobotnice}{url});
@members = $chobotnice->get_group_members($c->config->{chobotnice}{rv_gid});
}
} }
$c->render( json => \@members ); $c->render( json => \@members );
......
package RVVote::GraphAPI; package RVVote::IAPI;
use strict; use strict;
use warnings; use warnings;
...@@ -28,32 +28,27 @@ sub get_group_members { ...@@ -28,32 +28,27 @@ sub get_group_members {
my $self = shift; my $self = shift;
my $group_id = shift; my $group_id = shift;
my $res = $self->ua->get( join '/', ( my $res = $self->ua->get(
$self->{base_url}, sprintf($self->{base_url}, $group_id)
$group_id, )->result();
'members'
))->result();
return undef if ! $res->is_success; return undef if ! $res->is_success;
return undef if ! ref $res->json eq 'ARRAY'; return undef if ! ref $res->json eq 'ARRAY';
my @members = (); my @members = ();
MEMBER: MEMBER:
foreach my $member ( @{ $res->json } ) { foreach my $member ( @{ $res->json } ) {
my $res2 = $self->ua->get( join '/', ( my (undef, $lastname) = split /\s+/, $member->{displayname};
$self->{base_url}, push @members, {
'user', fullname => $member->{displayname},
$member->{username_clean}, user_id => $member->{username},
))->result(); lastname => $lastname,
next MEMBER if ! $res2->is_success; };
next MEMBER if ! ref $res2->json eq 'ARRAY';
$member = $res2->json;
(undef, $member->{lastname}) = split /\./, $member->{username_clean};
push @members, $member;
} }
return sort { $a->{lastname} cmp $b->{lastname} } @members; return sort { $a->{lastname} cmp $b->{lastname} } @members;
}; }
1;
{ {
secrets => ['ac605478557638569200e0e5333891c04929159a'], secrets => ['ac605478557638569200e0e5333891c04929159a'],
graph_api => { iapi => {
url => 'https://graph.pirati.cz', url => 'https://iapi.pirati.cz/v1/groups/%d/members',
rv_gid => 'deadbeef-babe-f002-000000000404', rv_gid => 404,
} },
chobotnice => {
url => 'https://chobotnice.pirati.cz/graphql/',
rv_gid => 'R3JvdXBUeXBlOjUzMg==',
},
} }
...@@ -32,15 +32,15 @@ function evaluate() { ...@@ -32,15 +32,15 @@ function evaluate() {
var a = $('form').serializeArray(); var a = $('form').serializeArray();
$.each(a, function() { $.each(a, function() {
var id = this.name.split('_'); var id = this.name.split('_');
bills.push({ bills.push({
"id": id[1], "id": id[1],
"value": this.value, "value": this.value,
"voter": $('input[name='+this.name+']').attr('data-fullname'), "voter": $('input[name="'+this.name+'"]').attr('data-fullname'),
}); });
}); });
$.ajax({ $.ajax({
url: '/api/calculate/', url: '/api/calculate/',
data: JSON.stringify(bills), data: JSON.stringify(bills),
contentType: 'application/json', contentType: 'application/json',
type: "POST", type: "POST",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment