post-update.srht (1878B)
- #!/usr/bin/env perl
- # SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: AGPL-3.0-only
- use strict;
- use utf8;
- no warnings; # Wide Character…
- use LWP::UserAgent;
- use JSON::MaybeXS;
- use Cwd;
- use File::Spec;
- my $json = JSON::MaybeXS->new(utf8 => 1);
- my $base = getcwd();
- $base =~ s/.git$//;
- my $workdir = "${base}.work";
- my $project = (File::Spec->splitpath($base))[2];
- sub new_job {
- my ($manifest_file) = @_;
- my $manifest_rel = (File::Spec->splitpath($manifest_file))[2];
- my $manifest = '';
- open FILE, '<', $manifest_file or die $!;
- while (<FILE>) {
- $manifest .= $_;
- }
- my $ua = LWP::UserAgent->new;
- $ua->agent("git post-update hook <https://hacktivis.me/git/utils/>");
- my $query = <<'EOF';
- mutation submit($manifest: String!, $tags: [String!], $visibility: Visibility) {
- submit(manifest: $manifest, tags: $tags, visibility: $visibility) {
- id
- owner { canonicalName }
- }
- }
- EOF
- my $job = {
- "query" => "$query",
- "variables" => {
- "manifest" => "$manifest",
- "tags" => [ "${project}", "${manifest_rel}" ],
- "visibility" => "PUBLIC",
- },
- };
- my $res = $ua->post(
- $ENV{SRHT_HOST} . "/query",
- Authorization => 'Bearer ' . $ENV{SRHT_TOKEN},
- Content_Type => 'application/json',
- Content => encode_json $job,
- );
- if (not $res->is_success) {
- print $res->message;
- print $res->content;
- die $!;
- } else {
- print $res->content;
- }
- }
- if (not(defined($ENV{'SRHT_HOST'}) && defined($ENV{'SRHT_TOKEN'}))) {
- print 'Environment variables $SRHT_HOST and $SRHT_TOKEN are undefined';
- exit 1;
- }
- if (-d "${workdir}/.builds") {
- # TODO: Shuffle manifests, take 4
- my @manifests = <"${workdir}/.builds/*.yml">;
- for (@manifests) {
- new_job $_;
- }
- } elsif (-e "${workdir}/.build.yml") {
- new_job "${workdir}/.build.yml";
- } else {
- print "No build manifest found";
- }