Skip to content
This repository has been archived by the owner on Jul 17, 2018. It is now read-only.

Commit

Permalink
Mask req debug info by default and improve error info
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Rees committed May 10, 2018
1 parent 4f64739 commit dbe3cd2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions ci/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ jobs:
- task: publish-draft-release
file: devtools-boshrelease/ci/tasks/publish-draft-release/publish-draft-release.yml
params:
GITHUB_ACCESS_KEY: ((github_access_key))
OWNER: finkit
REPO: devtools-boshrelease
VERSION: version/version
Expand Down
50 changes: 34 additions & 16 deletions ci/tasks/publish-draft-release/publish-draft-release.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"io/ioutil"
"net/http"
"os"
"strconv"
)

const (
gitHubUrl string = "https://api.github.com/repos/"
gitHubCredentialsVariable string = "GITHUB_ACCESS_KEY"
debugMessagesVariable string = "DEBUG_MESSAGES_ENABLED"
getReleasesMethodType string = "GET"
editReleaseMethodType string = "PATCH"
defaultVersion string = "latest"
Expand All @@ -21,12 +23,13 @@ const (
)

var (
owner string
repo string
branch string
version string
description string
credentials string = os.Getenv(gitHubCredentialsVariable)
owner string
repo string
branch string
version string
description string
credentials string = os.Getenv(gitHubCredentialsVariable)
debugMessages bool
)

type IdAndTag struct {
Expand Down Expand Up @@ -63,10 +66,12 @@ func sendRequest(methodType string, url string, body io.Reader, bodySize int64)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.ContentLength = bodySize

if body == nil {
os.Stdout.WriteString(fmt.Sprintf("Sending request to %s: %+v\n", url, req))
} else {
os.Stdout.WriteString(fmt.Sprintf("Sending request to %s with data %s: %+v\n", url, body, req))
if debugMessages {
if body == nil {
os.Stdout.WriteString(fmt.Sprintf("Sending request to %s: %+v\n", url, req))
} else {
os.Stdout.WriteString(fmt.Sprintf("Sending request to %s with data %s: %+v\n", url, body, req))
}
}

resp, err := http.DefaultClient.Do(req)
Expand All @@ -83,7 +88,8 @@ func sendRequest(methodType string, url string, body io.Reader, bodySize int64)
}

func getReleaseId() (int64, error) {
code, bodyBytes, err := sendRequest(getReleasesMethodType, getReleaseApiUrl(), bytes.NewBuffer([]byte{}), 0)
url := getReleaseApiUrl()
code, bodyBytes, err := sendRequest(getReleasesMethodType, url, bytes.NewBuffer([]byte{}), 0)

if err != nil {
return 0, err
Expand All @@ -92,20 +98,20 @@ func getReleaseId() (int64, error) {
if code == http.StatusOK {
fmt.Printf(fmt.Sprintf("Got releases on branch %s: %s\n", branch, string(bodyBytes)))
} else {
return 0, fmt.Errorf("error getting releases for version %s with response code %d", getReleaseApiUrl(), code)
return 0, fmt.Errorf("error getting releases for version %s with response code %d", url, code)
}

var releases []IdAndTag
json.Unmarshal(bodyBytes, &releases)
os.Stdout.WriteString(fmt.Sprintf("Response from %s: %v\n", getReleaseApiUrl(), releases))
os.Stdout.WriteString(fmt.Sprintf("Response from %s: %v\n", url, releases))

for _, release := range releases {
if release.TagName == version {
return release.Id, err
}
}

return 0, err
return 0, fmt.Errorf("No ID found from %s using version %s", url, version)
}

func editRelease(id int64, body io.Reader, bodySize int64) (int, []byte, error) {
Expand All @@ -116,7 +122,11 @@ func publishDraftRelease() error {
id, err := getReleaseId()

if err != nil {
return fmt.Errorf("error getting releases on %s", getReleaseApiUrl())
return fmt.Errorf("error getting releases: %s", err)
}

if id == 0 {
return fmt.Errorf("error getting releases - no ID found")
}

release := Release{
Expand Down Expand Up @@ -197,7 +207,15 @@ func main() {
description = os.Args[5]
}

err := publishDraftRelease()
var err error

debugMessages, err = strconv.ParseBool(os.Getenv(debugMessagesVariable))

if err != nil {
debugMessages = false
}

err = publishDraftRelease()

if err != nil {
os.Stderr.WriteString(fmt.Sprintf("Failed to publish draft release - %s\n", err))
Expand Down

0 comments on commit dbe3cd2

Please sign in to comment.