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 6683569
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 66 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 := tg.client.FetchRemoteContent(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 := tg.client.GetWorkflowPath()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package github
import (
"context"
"fmt"
"os"

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

"github.com/merico-dev/stream/pkg/util/log"
)
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/github/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,37 @@ func (c *Client) GetWorkflowPath() (string, error) {

return resp.Request.URL.Path, nil
}

func (c *Client) FetchRemoteContent(wsFiles []string) ([]string, map[string]error, error) {
var filesInRemoteDir = make([]string, 0)
_, dirContent, resp, err := c.Repositories.GetContents(
c.Context,
c.Owner,
c.Repo,
".github/workflows",
&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
}

0 comments on commit 6683569

Please sign in to comment.