Skip to content

Commit

Permalink
refactor(github): decouple trellogithub and "github.com/google/go-git…
Browse files Browse the repository at this point in the history
…hub/v42/github"

closes devstream-io#307
  • Loading branch information
warren830 committed Mar 13, 2022
1 parent b9b60b8 commit fe208ac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 64 deletions.
63 changes: 1 addition & 62 deletions internal/pkg/plugin/trellogithub/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package trellogithub

import (
"fmt"
"net/http"
"strings"

githubx "github.com/google/go-github/v42/github"
"github.com/spf13/viper"

"github.com/merico-dev/stream/pkg/util/github"
"github.com/merico-dev/stream/pkg/util/log"
"github.com/merico-dev/stream/pkg/util/mapz"
)

// VerifyWorkflows get the workflows with names "wf1.yml", "wf2.yml", then:
Expand All @@ -24,7 +19,7 @@ func (tg *TrelloGithub) VerifyWorkflows(workflows []*github.Workflow) (map[strin
}

fmt.Printf("Workflow files: %v", wsFiles)
filesInRemoteDir, rMap, err := tg.FetchRemoteContent(wsFiles)
filesInRemoteDir, rMap, err := github.FetchRemoteContent(tg.ctx, tg.options.Owner, tg.options.Repo, ".github/workflows", wsFiles)
if err != nil {
return nil, err
}
Expand All @@ -35,40 +30,6 @@ func (tg *TrelloGithub) VerifyWorkflows(workflows []*github.Workflow) (map[strin
return tg.CompareFiles(wsFiles, filesInRemoteDir), nil
}

func (tg *TrelloGithub) FetchRemoteContent(wsFiles []string) ([]string, map[string]error, error) {
var filesInRemoteDir = make([]string, 0)
_, dirContent, resp, err := tg.client.Repositories.GetContents(
tg.ctx,
tg.options.Owner,
tg.options.Repo,
".github/workflows",
&githubx.RepositoryContentGetOptions{},
)

// error reason is not 404
if err != nil && !strings.Contains(err.Error(), "404") {
log.Errorf("GetContents failed with error: %s.", err)
return nil, nil, err
}
// StatusCode == 404
if resp.StatusCode == http.StatusNotFound {
log.Error("GetContents returned with status code 404.")
retMap := mapz.FillMapWithStrAndError(wsFiles, fmt.Errorf("not found"))
return nil, retMap, nil
}
// StatusCode != 200
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("got some error: %s", resp.Status)
}
// StatusCode == 200
log.Info("GetContents return with status code 200.")
for _, f := range dirContent {
log.Infof("Found remote file: %s.", f.GetName())
filesInRemoteDir = append(filesInRemoteDir, f.GetName())
}
return filesInRemoteDir, nil, nil
}

// AddTrelloIdSecret add trello ids to secret
func (tg *TrelloGithub) AddTrelloIdSecret(trelloId *TrelloItemId) error {
ghOptions := &github.Option{
Expand Down Expand Up @@ -117,25 +78,3 @@ func (tg *TrelloGithub) AddTrelloIdSecret(trelloId *TrelloItemId) error {

return nil
}

func (tg *TrelloGithub) GetWorkflowPath() (string, error) {
_, _, resp, err := tg.client.Repositories.GetContents(
tg.ctx,
tg.options.Owner,
tg.options.Repo,
".github/workflows",
&githubx.RepositoryContentGetOptions{},
)

// error reason is not 404
if err != nil && !strings.Contains(err.Error(), "404") {
return "", err
}

// error reason is 404
if resp.StatusCode == http.StatusNotFound {
return "", err
}

return resp.Request.URL.Path, nil
}
3 changes: 1 addition & 2 deletions internal/pkg/plugin/trellogithub/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package trellogithub
import (
"bytes"
"fmt"

"text/template"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (tg *TrelloGithub) buildReadState(api *Api) (map[string]interface{}, error)
return nil, err
}

path, err := tg.GetWorkflowPath()
path, err := github.GetWorkflowPath(tg.ctx, tg.options.Owner, tg.options.Repo, ".github/workflows")
if err != nil {
return nil, err
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/util/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package github
import (
"context"
"fmt"
"net/http"
"os"
"strings"

"github.com/google/go-github/v42/github"
"golang.org/x/oauth2"

"github.com/merico-dev/stream/pkg/util/log"
"github.com/merico-dev/stream/pkg/util/mapz"
)

const (
Expand Down Expand Up @@ -99,3 +102,59 @@ func NewClient(option *Option) (*Client, error) {

return client, nil
}

func FetchRemoteContent(ctx context.Context, owner string, repo string, path string, wsFiles []string) ([]string, map[string]error, error) {
var filesInRemoteDir = make([]string, 0)
_, dirContent, resp, err := client.Repositories.GetContents(
ctx,
owner,
repo,
path,
&github.RepositoryContentGetOptions{},
)

// error reason is not 404
if err != nil && !strings.Contains(err.Error(), "404") {
log.Errorf("GetContents failed with error: %s.", err)
return nil, nil, err
}
// StatusCode == 404
if resp.StatusCode == http.StatusNotFound {
log.Error("GetContents returned with status code 404.")
retMap := mapz.FillMapWithStrAndError(wsFiles, fmt.Errorf("not found"))
return nil, retMap, nil
}
// StatusCode != 200
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("got some error: %s", resp.Status)
}
// StatusCode == 200
log.Info("GetContents return with status code 200.")
for _, f := range dirContent {
log.Infof("Found remote file: %s.", f.GetName())
filesInRemoteDir = append(filesInRemoteDir, f.GetName())
}
return filesInRemoteDir, nil, nil
}

func GetWorkflowPath(ctx context.Context, owner string, repo string, path string) (string, error) {
_, _, resp, err := client.Repositories.GetContents(
ctx,
owner,
repo,
path,
&github.RepositoryContentGetOptions{},
)

// error reason is not 404
if err != nil && !strings.Contains(err.Error(), "404") {
return "", err
}

// error reason is 404
if resp.StatusCode == http.StatusNotFound {
return "", err
}

return resp.Request.URL.Path, nil
}

0 comments on commit fe208ac

Please sign in to comment.