Skip to content

Commit

Permalink
feat: allow to update a PR even without modification rights.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 10, 2021
1 parent ae4061e commit 40a29db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
11 changes: 0 additions & 11 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,6 @@ func (r Repository) process(ctx context.Context, pr *github.PullRequest) error {

// Need to be up to date?
if needUpdate {
if !pr.GetMaintainerCanModify() && !isOnMainRepository(pr) {
repo, _, err := r.client.Repositories.Get(ctx, r.owner, r.name)
if err != nil {
return fmt.Errorf("unable to get repository information about %s/%s: %w", r.owner, r.name, err)
}

if !repo.GetPrivate() && !repo.GetFork() {
return errors.New("the contributor doesn't allow maintainer modification (GitHub option)")
}
}

if upToDateBranch {
err := r.merge(ctx, pr, mergeMethod)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/repository/repository_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (r Repository) getMergeMethod(pr *github.PullRequest) (string, error) {
}

func (r Repository) merge(ctx context.Context, pr *github.PullRequest, mergeMethod string) error {
if !pr.GetMaintainerCanModify() && !isOnMainRepository(pr) && mergeMethod == conf.MergeMethodFastForward {
// note: it's not possible to edit a PR from an organization.
return fmt.Errorf("the use of the merge method [%s] is impossible when a branch from an organization "+
"or if the contributor doesn't allow maintainer modification (GitHub option)", mergeMethod)
}

log.Printf("MERGE(%s)\n", mergeMethod)

err := r.removeLabel(ctx, pr, r.markers.MergeInProgress)
Expand Down
15 changes: 15 additions & 0 deletions pkg/repository/repository_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ func (r *Repository) update(ctx context.Context, pr *github.PullRequest) error {
log.Println(err)
}

// use GitHub API (update button)
if !pr.GetMaintainerCanModify() && !isOnMainRepository(pr) {
if r.dryRun {
log.Println("Updated via a merge with the GitHub API.")
return nil
}

_, _, err := r.client.PullRequests.UpdateBranch(ctx, pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), nil)
if err != nil {
return fmt.Errorf("update branch: %w", err)
}

return nil
}

return r.cloneAndUpdate(ctx, pr)
}

Expand Down

0 comments on commit 40a29db

Please sign in to comment.