Skip to content

Commit fc7df7c

Browse files
committed
Add nudge message with magic link to create new Trusted Publisher
1 parent 0ab0b79 commit fc7df7c

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ WORKDIR /app
2727
COPY LICENSE.md .
2828
COPY twine-upload.sh .
2929
COPY print-hash.py .
30+
COPY print-pkg-name.py .
3031
COPY oidc-exchange.py .
3132
COPY attestations.py .
3233

print-pkg-name.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pathlib
2+
import sys
3+
4+
from packaging import utils
5+
6+
7+
def debug(msg: str):
8+
print(f'::debug::{msg.title()}', file=sys.stderr)
9+
10+
11+
packages_dir = pathlib.Path(sys.argv[1]).resolve().absolute()
12+
13+
wheel_file_names = [
14+
f.name for f in packages_dir.iterdir() if f.suffix == '.whl'
15+
]
16+
sdist_file_names = [
17+
f.name for f in packages_dir.iterdir() if f.suffix == '.gz'
18+
]
19+
20+
# Parse the package name from the distribution files and print it. On error,
21+
# don't print anything.
22+
if wheel_file_names:
23+
try:
24+
print(utils.parse_wheel_filename(wheel_file_names[0])[0])
25+
except utils.InvalidWheelFilename:
26+
debug(f'Invalid wheel filename: {wheel_file_names[0]}')
27+
elif sdist_file_names:
28+
try:
29+
print(utils.parse_sdist_filename(sdist_file_names[0])[0])
30+
except utils.InvalidSdistFilename:
31+
debug(f'Invalid sdist filename: {sdist_file_names[0]}')

requirements/runtime.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ requests
1212
# NOTE: Used to generate attestations.
1313
pypi-attestations ~= 0.0.11
1414
sigstore ~= 3.2.0
15+
16+
# NOTE: Used to detect the PyPI package name from the distribution files
17+
packaging

twine-upload.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ INPUT_SKIP_EXISTING="$(get-normalized-input 'skip-existing')"
4141
INPUT_PRINT_HASH="$(get-normalized-input 'print-hash')"
4242
INPUT_ATTESTATIONS="$(get-normalized-input 'attestations')"
4343

44+
REPOSITORY_NAME="$(echo ${GITHUB_REPOSITORY} | cut -d'/' -f2)"
45+
WORKFLOW_FILENAME="$(echo ${GITHUB_WORKFLOW_REF} | cut -d'/' -f5- | cut -d'@' -f1)"
46+
PACKAGE_NAME="$(python /app/print-pkg-name.py ${INPUT_PACKAGES_DIR%%/})"
47+
4448
PASSWORD_DEPRECATION_NUDGE="::error title=Password-based uploads disabled::\
4549
As of 2024, PyPI requires all users to enable Two-Factor \
4650
Authentication. This consequently requires all users to switch \
@@ -64,6 +68,20 @@ The workflow was run with 'attestations: true' input, but the specified \
6468
repository URL does not support PEP 740 attestations. As a result, the \
6569
attestations input is ignored."
6670

71+
if [[ ! "${INPUT_REPOSITORY_URL}" =~ pypi\.org || -z "${PACKAGE_NAME}" ]] ; then
72+
TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE=""
73+
else
74+
if [[ "${INPUT_REPOSITORY_URL}" =~ test\.pypi\.org ]] ; then
75+
INDEX_URL="https://test.pypi.org"
76+
else
77+
INDEX_URL="https://pypi.org"
78+
fi
79+
TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE="::warning title=Create a Trusted Publisher::\
80+
A new Trusted Publisher for the currently running publishing workflow can be created \
81+
by accessing the following link while logged-in as a maintainer of the package: \
82+
${INDEX_URL}/manage/project/${PACKAGE_NAME}/settings/publishing/?provider=github&owner=${GITHUB_REPOSITORY_OWNER}&repository=${REPOSITORY_NAME}&workflow_filename=${WORKFLOW_FILENAME}"
83+
fi
84+
6785
[[ "${INPUT_USER}" == "__token__" && -z "${INPUT_PASSWORD}" ]] \
6886
&& TRUSTED_PUBLISHING=true || TRUSTED_PUBLISHING=false
6987

@@ -96,6 +114,7 @@ elif [[ "${INPUT_USER}" == '__token__' ]]; then
96114

97115
if [[ "${INPUT_REPOSITORY_URL}" =~ pypi\.org ]]; then
98116
echo "${TRUSTED_PUBLISHING_NUDGE}"
117+
echo "${TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE}"
99118
fi
100119
else
101120
echo \
@@ -105,6 +124,7 @@ else
105124
if [[ "${INPUT_REPOSITORY_URL}" =~ pypi\.org ]]; then
106125
echo "${PASSWORD_DEPRECATION_NUDGE}"
107126
echo "${TRUSTED_PUBLISHING_NUDGE}"
127+
echo "${TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE}"
108128
exit 1
109129
fi
110130
fi

0 commit comments

Comments
 (0)