logo

utils

Old programs, got split in utils-std and utils-extra git clone https://hacktivis.me/git/utils.git

post-update.srht (1878B)


  1. #!/usr/bin/env perl
  2. # SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. use strict;
  5. use utf8;
  6. no warnings; # Wide Character…
  7. use LWP::UserAgent;
  8. use JSON::MaybeXS;
  9. use Cwd;
  10. use File::Spec;
  11. my $json = JSON::MaybeXS->new(utf8 => 1);
  12. my $base = getcwd();
  13. $base =~ s/.git$//;
  14. my $workdir = "${base}.work";
  15. my $project = (File::Spec->splitpath($base))[2];
  16. sub new_job {
  17. my ($manifest_file) = @_;
  18. my $manifest_rel = (File::Spec->splitpath($manifest_file))[2];
  19. my $manifest = '';
  20. open FILE, '<', $manifest_file or die $!;
  21. while (<FILE>) {
  22. $manifest .= $_;
  23. }
  24. my $ua = LWP::UserAgent->new;
  25. $ua->agent("git post-update hook <https://hacktivis.me/git/utils/>");
  26. my $query = <<'EOF';
  27. mutation submit($manifest: String!, $tags: [String!], $visibility: Visibility) {
  28. submit(manifest: $manifest, tags: $tags, visibility: $visibility) {
  29. id
  30. owner { canonicalName }
  31. }
  32. }
  33. EOF
  34. my $job = {
  35. "query" => "$query",
  36. "variables" => {
  37. "manifest" => "$manifest",
  38. "tags" => [ "${project}", "${manifest_rel}" ],
  39. "visibility" => "PUBLIC",
  40. },
  41. };
  42. my $res = $ua->post(
  43. $ENV{SRHT_HOST} . "/query",
  44. Authorization => 'Bearer ' . $ENV{SRHT_TOKEN},
  45. Content_Type => 'application/json',
  46. Content => encode_json $job,
  47. );
  48. if (not $res->is_success) {
  49. print $res->message;
  50. print $res->content;
  51. die $!;
  52. } else {
  53. print $res->content;
  54. }
  55. }
  56. if (not(defined($ENV{'SRHT_HOST'}) && defined($ENV{'SRHT_TOKEN'}))) {
  57. print 'Environment variables $SRHT_HOST and $SRHT_TOKEN are undefined';
  58. exit 1;
  59. }
  60. if (-d "${workdir}/.builds") {
  61. # TODO: Shuffle manifests, take 4
  62. my @manifests = <"${workdir}/.builds/*.yml">;
  63. for (@manifests) {
  64. new_job $_;
  65. }
  66. } elsif (-e "${workdir}/.build.yml") {
  67. new_job "${workdir}/.build.yml";
  68. } else {
  69. print "No build manifest found";
  70. }