Skip to content

Commit 8a8cc1a

Browse files
authored
Better handling of git actor (#424)
1 parent 593f261 commit 8a8cc1a

File tree

7 files changed

+30
-24
lines changed

7 files changed

+30
-24
lines changed

.github/workflows/prep-release.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ on:
2424
type: boolean
2525
jobs:
2626
prep_release:
27+
permissions:
28+
contents: write
2729
runs-on: ubuntu-latest
2830
strategy:
2931
fail-fast: true
@@ -40,7 +42,7 @@ jobs:
4042
id: prep-release
4143
uses: jupyter-server/jupyter_releaser/.github/actions/prep-release@v2
4244
with:
43-
token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
45+
token: ${{ secrets.GITHUB_TOKEN }}
4446
version_spec: ${{ github.event.inputs.version_spec }}
4547
post_version_spec: ${{ github.event.inputs.post_version_spec }}
4648
target: ${{ github.event.inputs.target }}

.github/workflows/publish-release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ on:
1717

1818
jobs:
1919
publish_release:
20+
permissions:
21+
contents: write
2022
runs-on: ubuntu-latest
2123
strategy:
2224
fail-fast: true
@@ -33,7 +35,7 @@ jobs:
3335
id: populate-release
3436
uses: jupyter-server/jupyter_releaser/.github/actions/populate-release@v2
3537
with:
36-
token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
38+
token: ${{ secrets.GITHUB_TOKEN }}
3739
target: ${{ github.event.inputs.target }}
3840
branch: ${{ github.event.inputs.branch }}
3941
release_url: ${{ github.event.inputs.release_url }}
@@ -48,7 +50,7 @@ jobs:
4850
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4951
uses: jupyter-server/jupyter-releaser/.github/actions/finalize-release@v2
5052
with:
51-
token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
53+
token: ${{ secrets.GITHUB_TOKEN }}
5254
target: ${{ github.event.inputs.target }}
5355
release_url: ${{ steps.populate-release.outputs.release_url }}
5456

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ To install the latest release locally, make sure you have
2121

2222
## Checklist for Adoption
2323

24-
See the [adoption docs](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/convert_repo.html).
24+
See the [adoption guides](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/index.html).
2525

2626
## Actions
2727

2828
GitHub actions scripts are available to draft a changelog, draft a release, publish a release, and check a release.
2929

3030
See the [action details documentation](https://jupyter-releaser.readthedocs.io/en/latest/background/theory.html#action-details) for more information.
3131

32-
The actions can be run on a [fork](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/convert_repo_from_releaser.html#) of `jupyter_releaser` and target multiple
33-
repositories, or run as workflows on the [source repositories](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/convert_repo_from_repo), using
32+
The actions can be run on a [fork](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/convert_repo_from_releaser.html) of `jupyter_releaser` and target multiple
33+
repositories, or run as workflows on the [source repositories](https://jupyter-releaser.readthedocs.io/en/latest/how_to_guides/convert_repo_from_repo.html), using
3434
shared credentials.

docs/source/how_to_guides/convert_repo_from_repo.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ See [checklist](#Checklist-for-Adoption) below for details:
1414

1515
## Checklist for Adoption
1616

17-
- [ ] Add a [GitHub Access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with access to target GitHub repo to run GitHub Actions, saved as
18-
`ADMIN_GITHUB_TOKEN` in the [repository secrets](https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository).
19-
The token needs to have `public_repo` and `repo:status` permissions.
2017
- [ ] Add access token for the [PyPI registry](https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/#saving-credentials-on-github) stored as `PYPI_TOKEN`.
2118
_Note_ For security reasons, it is recommended that you scope the access
2219
to a single repository.
2320
- [ ] If needed, add access token for [npm](https://docs.npmjs.com/creating-and-viewing-access-tokens), saved as `NPM_TOKEN`.
24-
- [ ] Enable tag protection for all tags (`*`), to ensure that only users
25-
with admin write permissions can publish witht he shared credentials.
21+
- [ ] Ensure that only trusted users with 2FA have admin access to the
22+
repository, since they will be able to trigger releases.
2623
- [ ] Switch to Markdown Changelog
2724
- We recommend [MyST](https://myst-parser.readthedocs.io/en/latest/?badge=latest), especially if some of your docs are in reStructuredText.
2825
- Can use `pandoc -s changelog.rst -o changelog.md` and some hand edits as needed.

jupyter_releaser/lib.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,18 @@ def prep_git(ref, branch, repo, auth, username, url):
410410
"""Set up git"""
411411
repo = repo or util.get_repo()
412412

413-
user_name = ""
414413
try:
415-
user_name = util.run("git config --global user.email")
414+
util.run("git config --global user.email")
415+
has_git_config = True
416416
except Exception:
417-
pass
417+
has_git_config = False
418418

419-
if not user_name:
420-
# Use email address for the GitHub Actions bot
419+
if not has_git_config:
420+
# Default to the GitHub Actions bot
421421
# https://github.community/t/github-actions-bot-email-address/17204/6
422-
util.run(
423-
'git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"'
424-
)
425-
util.run('git config --global user.name "GitHub Action"')
422+
git_user_name = username or "41898282+github-actions[bot]"
423+
util.run(f'git config --global user.email "{git_user_name}@users.noreply.github.com"')
424+
util.run(f'git config --global user.name "{git_user_name}"')
426425

427426
# Set up the repository
428427
checkout_dir = os.environ.get("RH_CHECKOUT_DIR", util.CHECKOUT_NAME)

jupyter_releaser/tests/test_cli.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,18 @@ def test_prep_git_full(py_package, tmp_path, mocker, runner):
9494
os.mkdir(util.CHECKOUT_NAME)
9595

9696
runner(["prep-git"], env=env)
97+
9798
mock_run.assert_has_calls(
9899
[
100+
call("echo before-prep-git >> 'log.txt'"),
99101
call("git config --global user.email"),
100-
call(
101-
'git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"'
102-
),
103-
call('git config --global user.name "GitHub Action"'),
104102
call("git init .jupyter_releaser_checkout"),
105103
call("git remote add origin https://snuffy:[email protected]/baz/bar.git"),
106104
call(f"{GIT_FETCH_CMD} --tags --force"),
107105
call(f"{GIT_FETCH_CMD} +refs/pull/42:refs/pull/42"),
108106
call(f"{GIT_FETCH_CMD} refs/pull/42"),
109107
call("git checkout -B foo refs/pull/42"),
108+
call("git symbolic-ref -q HEAD"),
110109
]
111110
)
112111

jupyter_releaser/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,13 @@ def prepare_environment(fetch_draft_release=True):
558558
auth = os.environ.get("GITHUB_ACCESS_TOKEN", "")
559559
gh = get_gh_object(dry_run=dry_run, owner=owner, repo=repo_name, token=auth)
560560

561+
# Ensure the user is an admin.
562+
if not dry_run:
563+
user = gh.users.get_authenticated()["login"]
564+
collab_level = gh.repos.get_collaborator_permission_level(user)
565+
if not collab_level["permission"] == "admin":
566+
raise RuntimeError(f"User {user} does not have admin permission")
567+
561568
# Get the latest draft release if none is given.
562569
release_url = os.environ.get("RH_RELEASE_URL")
563570
log(f"Environment release url was {release_url}")

0 commit comments

Comments
 (0)