Skip to content

Commit

Permalink
Orb init (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Simmer authored Sep 28, 2020
1 parent bb01183 commit 6b7fd00
Show file tree
Hide file tree
Showing 9 changed files with 648 additions and 28 deletions.
36 changes: 34 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -398,6 +399,11 @@ type OrbCategory struct {
Name string `json:"name"`
}

type FollowedProject struct {
Followed bool `json:"followed"`
Message string `json:"message"`
}

// #nosec
func loadYaml(path string) (string, error) {
var err error
Expand Down Expand Up @@ -679,7 +685,7 @@ func CreateNamespace(cl *graphql.Client, name string, organizationName string, o
return createNSResponse, nil
}

func getNamespace(cl *graphql.Client, name string) (*GetNamespaceResponse, error) {
func GetNamespace(cl *graphql.Client, name string) (*GetNamespaceResponse, error) {
var response GetNamespaceResponse

query := `
Expand Down Expand Up @@ -746,7 +752,7 @@ func createOrbWithNsID(cl *graphql.Client, name string, namespaceID string) (*Cr

// CreateOrb creates (reserves) an orb within a namespace
func CreateOrb(cl *graphql.Client, namespace string, name string) (*CreateOrbResponse, error) {
response, err := getNamespace(cl, namespace)
response, err := GetNamespace(cl, namespace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1377,3 +1383,29 @@ func ListOrbCategories(cl *graphql.Client) (*OrbCategoriesForListing, error) {
}
return &orbCategories, nil
}

// FollowProject initiates an API request to follow a specific project on
// CircleCI. Project slugs are case-sensitive.
func FollowProject(restEndpoint string, vcs string, owner string, projectName string, cciToken string) (FollowedProject, error) {
requestPath := fmt.Sprintf("%s/api/v1.1/project/%s/%s/%s/follow", restEndpoint, vcs, owner, projectName)
r, err := http.NewRequest(http.MethodPost, requestPath, nil)
if err != nil {
return FollowedProject{}, err
}
r.Header.Set("Content-Type", "application/json; charset=utf-8")
r.Header.Set("Accept", "application/json; charset=utf-8")
r.Header.Set("Circle-Token", cciToken)
client := http.Client{}
response, err := client.Do(r)
if err != nil {
return FollowedProject{}, err
}

var fr FollowedProject
err = json.NewDecoder(response.Body).Decode(&fr)
if err != nil {
return FollowedProject{}, err
}

return fr, nil
}
54 changes: 54 additions & 0 deletions cmd/follow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/git"
"github.com/CircleCI-Public/circleci-cli/settings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type options struct {
cfg *settings.Config
}

func followProject(opts options) error {

remote, err := git.InferProjectFromGitRemotes()

if err != nil {
return errors.Wrap(err, errorMessage)
}

vcsShort := "gh"
if remote.VcsType == "BITBUCKET" {
vcsShort = "bb"
}
res, err := api.FollowProject(opts.cfg.Host, vcsShort, remote.Organization, remote.Project, opts.cfg.Token)
if err != nil {
return err
}
if res.Followed {
fmt.Println("Project successfully followed!")
} else if res.Message == "Project not found" {
fmt.Println("Unable to determine project slug for CircleCI (slug is case sensitive).")
}

return nil
}

func followProjectCommand(config *settings.Config) *cobra.Command {
opts := options{
cfg: config,
}
followCommand := &cobra.Command{
Use: "follow",
Short: "Attempt to follow the project for the current git repository.",
RunE: func(_ *cobra.Command, _ []string) error {
return followProject(opts)
},
}
return followCommand
}
Loading

0 comments on commit 6b7fd00

Please sign in to comment.