|
| 1 | +// Copyright (c) 2026 Circle Internet Services, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | +// |
| 21 | +// SPDX-License-Identifier: MIT |
| 22 | + |
| 23 | +// Package open implements the "circleci open" command. |
| 24 | +package open |
| 25 | + |
| 26 | +import ( |
| 27 | + "fmt" |
| 28 | + "net/url" |
| 29 | + "strings" |
| 30 | + |
| 31 | + "github.com/MakeNowJust/heredoc" |
| 32 | + "github.com/pkg/browser" |
| 33 | + "github.com/spf13/cobra" |
| 34 | + |
| 35 | + clierrors "github.com/CircleCI-Public/circleci-cli-v2/internal/errors" |
| 36 | + "github.com/CircleCI-Public/circleci-cli-v2/internal/gitremote" |
| 37 | +) |
| 38 | + |
| 39 | +// ProjectURL builds the CircleCI pipelines URL for the given project slug. |
| 40 | +func ProjectURL(slug string) (string, error) { |
| 41 | + parts := strings.SplitN(slug, "/", 3) |
| 42 | + if len(parts) != 3 { |
| 43 | + return "", fmt.Errorf("invalid slug: %q", slug) |
| 44 | + } |
| 45 | + return fmt.Sprintf("https://app.circleci.com/pipelines/%s/%s/%s", |
| 46 | + url.PathEscape(parts[0]), |
| 47 | + url.PathEscape(parts[1]), |
| 48 | + url.PathEscape(parts[2]), |
| 49 | + ), nil |
| 50 | +} |
| 51 | + |
| 52 | +// NewOpenCmd returns the "circleci open" command. |
| 53 | +func NewOpenCmd() *cobra.Command { |
| 54 | + return &cobra.Command{ |
| 55 | + Use: "open", |
| 56 | + Short: "Open the current project in the browser", |
| 57 | + Long: heredoc.Doc(` |
| 58 | + Open the CircleCI pipelines page for the current project in your |
| 59 | + default web browser. |
| 60 | +
|
| 61 | + The project is inferred from the current git repository's remote. |
| 62 | + Supports GitHub, Bitbucket, and GitLab remotes. |
| 63 | + `), |
| 64 | + Example: heredoc.Doc(` |
| 65 | + # Open pipelines for the current repo |
| 66 | + $ circleci open |
| 67 | + `), |
| 68 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 69 | + info, err := gitremote.Detect() |
| 70 | + if err != nil { |
| 71 | + return clierrors.New("git.detect_failed", |
| 72 | + "Could not detect project from git remote", err.Error()). |
| 73 | + WithSuggestions( |
| 74 | + "Run from inside a git repository with a GitHub, Bitbucket, or GitLab remote", |
| 75 | + ). |
| 76 | + WithExitCode(clierrors.ExitBadArguments) |
| 77 | + } |
| 78 | + |
| 79 | + projectURL, err := ProjectURL(info.Slug) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + return browser.OpenURL(projectURL) |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
0 commit comments