summaryrefslogtreecommitdiff
path: root/bin/buildssrht-preupload
blob: ebae98f20caf76f34021e85121ebcc1248b1506c (plain)
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
#!/usr/bin/env perl

# Copyright (C) 2020 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use 5.028;
use strict;
use warnings;

use JSON;
use YAML;
$YAML::UseBlock = 1;
use REST::Client;
use API::GitForge qw(forge_access_token);
use Getopt::Long;
use Git::Wrapper;
use Cwd;
use Term::UI;

my $term = Term::ReadLine->new("brand");
my $git  = Git::Wrapper->new(getcwd);

my $user = "~spwhitton";    # TODO could fetch from API
my $dist = "unstable";
my ($remote, $clone_uri);
my $quilt = "auto";
GetOptions
  "distribution|d=s" => \$dist,
  "remote=s"         => \$remote,
  "quilt=s"          => \$quilt;
unless ($remote) {
    # see if there is a git.sr.ht remote; use that if possible as
    # clone to builds.sr.ht will be faster
    foreach my $r ($git->remote) {
        my ($u) = $git->remote("get-url", $r);
        $u =~ /git\.sr\.ht/ and $remote = $r, $clone_uri = $u;
    }
}
$remote ||= "origin";
$clone_uri or ($clone_uri) = $git->remote("get-url", $remote);

chomp(my $source  = `dpkg-parsechangelog -SSource`);
chomp(my $version = `dpkg-parsechangelog -SVersion`);
$source  or die "couldn't determine source package name";
$version or die "couldn't determine source package version";
my $note = "Pre-upload checks for $source $version";

$git->fetch($remote);
my ($branch) = $git->symbolic_ref("HEAD");
my ($commit) = $git->rev_parse({ verify => 1 }, "HEAD");
$git->for_each_ref({ contains => $branch }, "refs/remotes/$remote")
  or die "remote $remote doesn't appear to have a copy of your HEAD";

exit
  unless $term->ask_yn(
    prompt =>
"will run the build for $branch cloning from $remote with note \"$note\", "
      . "dist $dist, with --quilt=$quilt",
    default => "y"
  );

my $schroot = "$dist-amd64-sbuild";
my $changes = "${source}_${version}_multi.changes";

my $manifest->{image} = "debian/unstable";
$manifest->{sources} = [$clone_uri];
$manifest->{packages}
  = ["autopkgtest", "devscripts", "dgit", "lintian", "piuparts", "sbuild"];

$manifest->{tasks} = [
    map +({ $_->[0] => join "\n", $_->[1]->@*, "" }),
    [
        setup => [
            "cd $source",
            "git checkout $commit",
            "git deborig || origtargz",
            join(
                " ",
                qw(sudo
                  sbuild-createchroot
                  --command-prefix=eatmydata
                  --include=eatmydata),
                $dist,
                "/srv/chroot/$schroot"
            ),
            "sudo sbuild-adduser \$USER"
        ]
    ],
    [
        build => [
            "cd $source",
            "dgit --quilt=$quilt sbuild -d $dist --no-run-lintian"
        ]
    ],
    [lintian     => ["lintian $changes"]],
    [autopkgtest => ["autopkgtest $changes -- schroot $schroot"]],
    [piuparts => ["sudo piuparts --no-eatmydata --schroot $schroot $changes"]]
];

my $client = REST::Client->new;
$client->setHost("https://builds.sr.ht");
$client->addHeader("Authorization", "token " . forge_access_token "sr.ht");
$client->addHeader("Content-Type",  "application/json");
$client->addHeader("charset",       "UTF-8");

$client->POST(
    "/api/jobs",
    encode_json {
        note          => $note,
        "access:read" => [$user],
        manifest      => YAML::Dump $manifest
    });
my $resp   = decode_json $client->responseContent;
my $job_id = $resp->{id};

system "xdg-open", "https://builds.sr.ht/$user/job/$job_id#bottom";

# $client->GET("/api/jobs");
# my $jobs = decode_json $client->responseContent;
# my ($job) = grep $_->{id} == $job_id, $jobs->{results}->@*;
# my $job_host = $job->{runner};
# system "ssh", "-t", "builds@" . $job_host, "connect", $job_id;