Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c31d1a

Browse files
committed
Minor refactoring autolink create help and logic
- simplified and wrapped `gh repo autolink create` and `gh repo autolink` long help usage docs - simplified success message, brought into alignment with other commands
1 parent 965782a commit 7c31d1a

File tree

3 files changed

+33
-63
lines changed

3 files changed

+33
-63
lines changed

pkg/cmd/repo/autolink/autolink.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ func NewCmdAutolink(f *cmdutil.Factory) *cobra.Command {
1313
Use: "autolink <command>",
1414
Short: "Manage autolink references",
1515
Long: heredoc.Docf(`
16-
Work with GitHub autolink references.
17-
18-
GitHub autolinks require admin access to configure and can be found at
19-
https://github.com/{owner}/{repo}/settings/key_links.
20-
Use %[1]sgh repo autolink list --web%[1]s to open this page for the current repository.
21-
22-
For more information about GitHub autolinks, see https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/configuring-autolinks-to-reference-external-resources
23-
`, "`"),
16+
Autolinks link issues, pull requests, commit messages, and release descriptions to external third-party services.
17+
18+
Autolinks require %[1]sadmin%[1]s role to view or manage.
19+
20+
For more information, see <https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/configuring-autolinks-to-reference-external-resources>
21+
`, "`"),
2422
}
2523
cmdutil.EnableRepoOverride(cmd, f)
2624

pkg/cmd/repo/autolink/create/create.go

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,35 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*createOptions) error) *cobra.Co
4040
Long: heredoc.Docf(`
4141
Create a new autolink reference for a repository.
4242
43-
Autolinks automatically generate links to external resources when they appear in an issue, pull request, or commit.
43+
The %[1]skeyPrefix%[1]s argument specifies the prefix that will generate a link when it is appended by certain characters.
4444
45-
The %[1]skeyPrefix%[1]s specifies the prefix that will generate a link when it is appended by certain characters.
45+
The %[1]surlTemplate%[1]s argument specifies the target URL that will be generated when the keyPrefix is found, which
46+
must contain %[1]s<num>%[1]s variable for the reference number.
4647
47-
The %[1]surlTemplate%[1]s specifies the target URL that will be generated when the keyPrefix is found. The %[1]surlTemplate%[1]s must contain %[1]s<num>%[1]s for the reference number. %[1]s<num>%[1]s matches different characters depending on the whether the autolink is specified as numeric or alphanumeric.
48+
By default, autolinks are alphanumeric with %[1]s--numeric%[1]s flag used to create a numeric autolink.
4849
49-
By default, the command will create an alphanumeric autolink. This means that the %[1]s<num>%[1]s in the %[1]surlTemplate%[1]s will match alphanumeric characters %[1]sA-Z%[1]s (case insensitive), %[1]s0-9%[1]s, and %[1]s-%[1]s. To create a numeric autolink, use the %[1]s--numeric%[1]s flag. Numeric autolinks only match against numeric characters. If the template contains multiple instances of %[1]s<num>%[1]s, only the first will be replaced.
50+
The %[1]s<num>%[1]s variable behavior differs depending on whether the autolink is alphanumeric or numeric:
5051
51-
If you are using a shell that applies special meaning to angle brackets, you will need to escape these characters in the %[1]surlTemplate%[1]s or place quotation marks around the whole argument.
52-
53-
Only repository administrators can create autolinks.
52+
- alphanumeric: matches %[1]sA-Z%[1]s (case insensitive), %[1]s0-9%[1]s, and %[1]s-%[1]s
53+
- numeric: matches %[1]s0-9%[1]s
54+
55+
If the template contains multiple instances of %[1]s<num>%[1]s, only the first will be replaced.
5456
`, "`"),
5557
Example: heredoc.Doc(`
56-
# Create an alphanumeric autolink to example.com for the key prefix "TICKET-". This will generate a link to https://example.com/TICKET?query=123abc when "TICKET-123abc" appears.
57-
$ gh repo autolink create TICKET- https://example.com/TICKET?query=<num>
58-
59-
# Create a numeric autolink to example.com for the key prefix "STORY-". This will generate a link to https://example.com/STORY?id=123 when "STORY-123" appears.
60-
$ gh repo autolink create STORY- https://example.com/STORY?id=<num> --numeric
58+
# Create an alphanumeric autolink to example.com for the key prefix "TICKET-".
59+
# Generates https://example.com/TICKET?query=123abc from "TICKET-123abc".
60+
$ gh repo autolink create TICKET- "https://example.com/TICKET?query=<num>"
6161
62+
# Create a numeric autolink to example.com for the key prefix "STORY-".
63+
# Generates https://example.com/STORY?id=123 from "STORY-123".
64+
$ gh repo autolink create STORY- "https://example.com/STORY?id=<num>" --numeric
6265
`),
6366
Args: cmdutil.ExactArgs(2, "Cannot create autolink: keyPrefix and urlTemplate arguments are both required"),
6467
Aliases: []string{"new"},
6568
RunE: func(c *cobra.Command, args []string) error {
6669
opts.BaseRepo = f.BaseRepo
67-
httpClient, err := f.HttpClient()
6870

71+
httpClient, err := f.HttpClient()
6972
if err != nil {
7073
return err
7174
}
@@ -82,7 +85,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*createOptions) error) *cobra.Co
8285
},
8386
}
8487

85-
cmd.Flags().BoolVarP(&opts.Numeric, "numeric", "n", false, "Mark autolink as non-alphanumeric")
88+
cmd.Flags().BoolVarP(&opts.Numeric, "numeric", "n", false, "Mark autolink as numeric")
8689

8790
return cmd
8891
}
@@ -92,48 +95,24 @@ func createRun(opts *createOptions) error {
9295
if err != nil {
9396
return err
9497
}
95-
cs := opts.IO.ColorScheme()
96-
97-
isAlphanumeric := !opts.Numeric
9898

9999
request := AutolinkCreateRequest{
100100
KeyPrefix: opts.KeyPrefix,
101101
URLTemplate: opts.URLTemplate,
102-
IsAlphanumeric: isAlphanumeric,
102+
IsAlphanumeric: !opts.Numeric,
103103
}
104104

105105
autolink, err := opts.AutolinkClient.Create(repo, request)
106-
107106
if err != nil {
108-
return fmt.Errorf("%s %w", cs.Red("error creating autolink:"), err)
109-
}
110-
111-
msg := successMsg(autolink, repo, cs)
112-
fmt.Fprint(opts.IO.Out, msg)
113-
114-
return nil
115-
}
116-
117-
func successMsg(autolink *shared.Autolink, repo ghrepo.Interface, cs *iostreams.ColorScheme) string {
118-
autolinkType := "Numeric"
119-
if autolink.IsAlphanumeric {
120-
autolinkType = "Alphanumeric"
107+
return fmt.Errorf("error creating autolink: %w", err)
121108
}
122109

123-
createdMsg := fmt.Sprintf(
124-
"%s %s autolink created in %s (id: %d)",
110+
cs := opts.IO.ColorScheme()
111+
fmt.Fprintf(opts.IO.Out,
112+
"%s Created repository autolink %d on %s\n",
125113
cs.SuccessIconWithColor(cs.Green),
126-
autolinkType,
127-
ghrepo.FullName(repo),
128114
autolink.ID,
129-
)
115+
ghrepo.FullName(repo))
130116

131-
autolinkMapMsg := cs.Bluef(
132-
" %s%s → %s",
133-
autolink.KeyPrefix,
134-
"<num>",
135-
autolink.URLTemplate,
136-
)
137-
138-
return fmt.Sprintf("%s\n%s\n", createdMsg, autolinkMapMsg)
117+
return nil
139118
}

pkg/cmd/repo/autolink/create/create_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"testing"
88

9-
"github.com/MakeNowJust/heredoc"
109
"github.com/cli/cli/v2/internal/browser"
1110
"github.com/cli/cli/v2/internal/ghrepo"
1211
"github.com/cli/cli/v2/pkg/cmd/repo/autolink/shared"
@@ -133,10 +132,7 @@ func TestCreateRun(t *testing.T) {
133132
URLTemplate: "https://example.com/TICKET?query=<num>",
134133
},
135134
stubCreator: stubAutoLinkCreator{},
136-
wantStdout: heredoc.Doc(`
137-
✓ Alphanumeric autolink created in OWNER/REPO (id: 1)
138-
TICKET-<num> → https://example.com/TICKET?query=<num>
139-
`),
135+
wantStdout: "✓ Created repository autolink 1 on OWNER/REPO\n",
140136
},
141137
{
142138
name: "success, numeric",
@@ -146,10 +142,7 @@ func TestCreateRun(t *testing.T) {
146142
Numeric: true,
147143
},
148144
stubCreator: stubAutoLinkCreator{},
149-
wantStdout: heredoc.Doc(`
150-
✓ Numeric autolink created in OWNER/REPO (id: 1)
151-
TICKET-<num> → https://example.com/TICKET?query=<num>
152-
`),
145+
wantStdout: "✓ Created repository autolink 1 on OWNER/REPO\n",
153146
},
154147
{
155148
name: "client error",
@@ -180,7 +173,7 @@ func TestCreateRun(t *testing.T) {
180173
if tt.expectedErr != nil {
181174
require.Error(t, err)
182175
assert.ErrorIs(t, err, tt.expectedErr)
183-
assert.Equal(t, err.Error(), tt.errMsg)
176+
assert.Equal(t, tt.errMsg, err.Error())
184177
} else {
185178
require.NoError(t, err)
186179
}

0 commit comments

Comments
 (0)