Do not interpret "branch" placeholder in api command when GH_REPO is set (#7626)

* fix(api): do not interpret "branch" placeholder when `GH_REPO` is set

Before, we would always interpret the "branch" placeholder, typically
setting it to the currently checked-out branch in the repository in the
current working directory. It didn't make sense to do that when the
`GH_REPO` environment variable was specified because the repository
would likely be different from the one in the current working directory.

Now, we instead report an error if both `GH_REPO` environment variable
and `branch` placeholder are specified.
This commit is contained in:
Alex Petrov 2023-07-06 03:05:40 -04:00 committed by GitHub
parent 6f99fd8442
commit 343896fdac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@ -469,6 +470,11 @@ func fillPlaceholders(value string, opts *ApiOptions) (string, error) {
err = e
}
case "branch":
if os.Getenv("GH_REPO") != "" {
err = errors.New("unable to determine an appropriate value for the 'branch' placeholder")
return m
}
if branch, e := opts.Branch(); e == nil {
return branch
} else {

View file

@ -1096,10 +1096,11 @@ func Test_fillPlaceholders(t *testing.T) {
opts *ApiOptions
}
tests := []struct {
name string
args args
want string
wantErr bool
name string
args args
repoOverride bool
want string
wantErr bool
}{
{
name: "no changes",
@ -1236,9 +1237,26 @@ func Test_fillPlaceholders(t *testing.T) {
want: "{}{ownership}/{repository}",
wantErr: false,
},
{
name: "branch can't be filled when GH_REPO is set",
repoOverride: true,
args: args{
value: "repos/:owner/:repo/branches/:branch",
opts: &ApiOptions{
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("hubot", "robot-uprising"), nil
},
},
},
want: "repos/hubot/robot-uprising/branches/:branch",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.repoOverride {
t.Setenv("GH_REPO", "hubot/robot-uprising")
}
got, err := fillPlaceholders(tt.args.value, tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("fillPlaceholders() error = %v, wantErr %v", err, tt.wantErr)