aboutsummaryrefslogtreecommitdiff
path: root/brep/handler/submit/submit.bash.in
blob: 7826809bbaab86d4234e70dab78f8c4400451df5 (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
# file      : brep/handler/submit/submit.bash.in
# license   : MIT; see accompanying LICENSE file

# Utility functions useful for implementing package submission handlers.

if [ "$brep_handler_submit" ]; then
  return 0
else
  brep_handler_submit=true
fi

@import brep/handler/handler@

# Serialize the package submission result manifest to stdout and exit the
# (sub-)shell with the zero status.
#
reference= # Should be assigned later by the handler, when becomes available.

function exit_with_manifest () # <status> <message>
{
  trace_func "$@"

  local sts="$1"
  local msg="$2"

  manifest_serializer_start

  manifest_serialize ""        "1"    # Start of manifest.
  manifest_serialize "status"  "$sts"
  manifest_serialize "message" "$msg"

  if [ -n "$reference" ]; then
    manifest_serialize "reference" "$reference"
  elif [ "$sts" == "200" ]; then
    error "no reference for code $sts"
  fi

  manifest_serializer_finish
  run exit 0
}

# Verify archive is a valid package and extract its manifest into
# <manifest> file.
#
function extract_package_manifest () # <archive> <manifest>
{
  local arc="$1"
  local man="$2"

  # Pass the --deep option to make sure that the bootstrap buildfile is
  # present and the *-file manifest values are resolvable, so rep-create will
  # not fail due to this package down the road. Note that we also make sure
  # that all the manifest values are known (see bpkg-pkg-verify for details).
  #
  local cmd=(bpkg pkg-verify --deep --manifest "$arc")
  trace_cmd "${cmd[@]}"

  # Note that we used to just advise the user to run bpkg-pkg-verify locally
  # for the details on the potential failure. That, however, may not always be
  # helpful since the user can use a different version of the toolchain and so
  # may observe a different behavior. Thus, we add the bpkg-pkg-verify error
  # message to the response, turning it into an info. This way the user may
  # potentially see the following bdep-publish diagnostics:
  #
  # error: package archive is not valid
  #   info: unable to satisfy constraint (build2 >= 0.17.0-) for package libhello-1.0.0.tar.gz
  #   info: available build2 version is 0.16.0
  #   info: run bpkg pkg-verify for details
  #   info: reference: 308e155764c8
  #
  local e
  if ! e="$("${cmd[@]}" 2>&1 >"$man")"; then

    # Perform the sanity check to make sure that bpkg is runnable.
    #
    if ! run bpkg --version >/dev/null; then
      error "unable to run bpkg"
    fi

    # Note that bpkg-pkg-verify diagnostics may potentially contain the
    # archive absolute path. Let's sanitize this diagnostics by stripping the
    # archive directory path, if present. Also note that to use sed for that
    # we first need to escape the special regex characters and slashes in the
    # archive directory path (see sed's basic regular expressions for
    # details).
    #
    local d="$(sed 's/[[\.*^$/]/\\&/g' <<<"$(dirname "$arc")/")"

    e="$(sed -e "s/$d//g" -e 's/^error:/  info:/' <<<"$e")"
    e=$'package archive is not valid\n'"$e"$'\n  info: run bpkg pkg-verify for details'

    exit_with_manifest 400 "$e"
  fi
}

# Extract the revision part from the package version. Return 0 if the version
# doesn't contain revision.
#
function version_revision () # version
{
  local r
  r="$(sed -n -re 's%^(\+?[^+]+)(\+([0-9]+))?$%\3%p' <<<"$1")"

  if [ -z "$r" ]; then
    r="0"
  fi

  echo "$r"
}