Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): Update local and remote defaults to main branch #2738

Merged
merged 9 commits into from
Mar 15, 2023

Conversation

nellh
Copy link
Contributor

@nellh nellh commented Dec 15, 2022

This updates some mismatched master branch usage to always use main.

  • On new commits, this will rename the master branch to main if required before making the commit. Updates symbolic HEAD reference so that new operations implicitly use the main branch.
  • When pushing to GitHub, both main and master branches are pushed. Sets the default branch and repo description/name on pushes.
  • New test that creates a default master branch Datalad repo and then updates it to use main on git_commit.

@nellh nellh requested a review from effigies December 15, 2022 18:46
ses = Github(DATALAD_GITHUB_TOKEN)
org = ses.get_organization(DATALAD_GITHUB_ORG)
repo = org.get_repo(dataset_id)
repo.edit(default_branch="main", description="OpenNeuro dataset",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it reasonably un-clunky to put the dataset name here? That would make the listing in https://github.com/OpenNeuroDatasets much more informative.

Suggested change
repo.edit(default_branch="main", description="OpenNeuro dataset",
repo.edit(default_branch="main", description=f"OpenNeuro dataset {dataset_id}: {description['Name']}",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As part of the changes for metadata tagging on GitHub I was planning on loading some information from OpenNeuro's API service and updating it here.

That said, we could just read the dataset_description.json inline here and use the name from that if it exists, not sure how soon we'll get back to those GitHub metadata changes with other priorities.

@nellh nellh force-pushed the main-branch-fixes branch from 41e412c to 6949383 Compare December 15, 2022 19:38
# Make sure the main branch is used, update if needed
master_branch = repo.branches.get('master')
if master_branch:
main_branch = master_branch.rename('main', True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance there's a dataset with both master and main? It looks like Branch.rename(force=True) will clobber, but if both are present, are we confident that master is authoritative and not main? I could imagine a situation where someone with git access pushes a stale master branch to a dataset that's had master renamed to main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now there are no main branch datasets and OpenNeuro cannot edit them without these fixes. Maybe instead we block master for new pushes? I could add that here.

Comment on lines 44 to 52
# Make sure the main branch is used, update if needed
master_branch = repo.branches.get('master')
if master_branch:
main_branch = master_branch.rename('main', True)
# Abort the commit if this didn't work
if not main_branch.is_head():
raise Exception('Unable to rename master branch to main')
# Always update the symbolic reference for HEAD
repo.references['HEAD'].set_target('refs/heads/main')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of this?

Suggested change
# Make sure the main branch is used, update if needed
master_branch = repo.branches.get('master')
if master_branch:
main_branch = master_branch.rename('main', True)
# Abort the commit if this didn't work
if not main_branch.is_head():
raise Exception('Unable to rename master branch to main')
# Always update the symbolic reference for HEAD
repo.references['HEAD'].set_target('refs/heads/main')
HEAD = repo.references["HEAD"]
# Make sure the main branch is used, update if needed
if HEAD.target == "refs/heads/master":
main_branch = repo.branches.get('master').rename('main', True)
# Abort the commit if this didn't work
if not main_branch.is_head():
raise Exception('Unable to rename master branch to main')
# Always update the symbolic reference for HEAD
HEAD.set_target('refs/heads/main')
# Remove master if it's still around
if "master" in repo.branches:
repo.branches["master"].delete()

@nellh
Copy link
Contributor Author

nellh commented Dec 15, 2022

Okay, adding two more test cases:

  • Test for unusual state where the default is some other branch and the commit should be aborted.
  • Test for existing dataset with history including a tag to validate the rename preserves history in that case.

One more fix:

  • Verify if master is head before renaming unless it's an empty branch.

@nellh nellh force-pushed the main-branch-fixes branch from 8f02133 to db19a45 Compare March 14, 2023 18:23
@codecov
Copy link

codecov bot commented Mar 15, 2023

Codecov Report

Merging #2738 (df8a33d) into master (9b86070) will decrease coverage by 0.01%.
The diff coverage is 69.04%.

@@            Coverage Diff             @@
##           master    #2738      +/-   ##
==========================================
- Coverage   75.89%   75.88%   -0.01%     
==========================================
  Files         411      411              
  Lines       67157    67193      +36     
  Branches     2041     2041              
==========================================
+ Hits        50967    50991      +24     
- Misses      16139    16151      +12     
  Partials       51       51              
Impacted Files Coverage Δ
services/datalad/datalad_service/common/github.py 31.57% <25.00%> (-7.56%) ⬇️
services/datalad/datalad_service/common/git.py 84.90% <90.90%> (+1.57%) ⬆️
services/datalad/datalad_service/common/bids.py 100.00% <100.00%> (ø)
services/datalad/datalad_service/tasks/dataset.py 93.33% <100.00%> (ø)
services/datalad/datalad_service/tasks/publish.py 52.94% <100.00%> (ø)

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@nellh nellh merged commit fb3b5f3 into master Mar 15, 2023
@nellh nellh deleted the main-branch-fixes branch March 15, 2023 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants