Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for status_file in put #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ empty commit to the PR*.
|----------------------------|----------|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `path` | Yes | `pull-request` | The name given to the resource in a GET step. |
| `status` | No | `SUCCESS` | Set a status on a commit. One of `SUCCESS`, `PENDING`, `FAILURE` and `ERROR`. |
| `status_file` | No | `my-output/status.txt` | Path to file containing a status on a commit. See `status` for valid values |
| `base_context` | No | `concourse-ci` | Base context (prefix) used for the status context. Defaults to `concourse-ci`. |
| `context` | No | `unit-test` | A context to use for the status, which is prefixed by `base_context`. Defaults to `status`. |
| `comment` | No | `hello world!` | A comment to add to the pull request. |
Expand Down
30 changes: 24 additions & 6 deletions out.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, err
}

// Set status if specified
if p := request.Params; p.Status != "" {
if p := request.Params; p.Status != "" || p.StatusFile != "" {
status := p.Status
description := p.Description

// Set status from a file
if p.StatusFile != "" {
content, err := ioutil.ReadFile(filepath.Join(inputDir, p.StatusFile))
if err != nil {
return nil, fmt.Errorf("failed to read status file: %s", err)
}
status = strings.TrimSpace(string(content))
err = validateStatus(status)
if err != nil {
return nil, err
}
}

// Set description from a file
if p.DescriptionFile != "" {
content, err := ioutil.ReadFile(filepath.Join(inputDir, p.DescriptionFile))
Expand All @@ -49,7 +63,7 @@ func Put(request PutRequest, manager Github, inputDir string) (*PutResponse, err
description = string(content)
}

if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, safeExpandEnv(p.Context), p.Status, safeExpandEnv(p.TargetURL), description); err != nil {
if err := manager.UpdateCommitStatus(version.Commit, p.BaseContext, safeExpandEnv(p.Context), status, safeExpandEnv(p.TargetURL), description); err != nil {
return nil, fmt.Errorf("failed to set status: %s", err)
}
}
Expand Down Expand Up @@ -111,6 +125,7 @@ type PutParameters struct {
TargetURL string `json:"target_url"`
DescriptionFile string `json:"description_file"`
Description string `json:"description"`
StatusFile string `json:"status_file"`
Status string `json:"status"`
CommentFile string `json:"comment_file"`
Comment string `json:"comment"`
Expand All @@ -123,21 +138,24 @@ func (p *PutParameters) Validate() error {
return nil
}
// Make sure we are setting an allowed status
return validateStatus(p.Status)
}

func validateStatus(status string) error {
var allowedStatus bool

status := strings.ToLower(p.Status)
normalizedStatus := strings.ToLower(status)
allowed := []string{"success", "pending", "failure", "error"}

for _, a := range allowed {
if status == a {
if normalizedStatus == a {
allowedStatus = true
}
}

if !allowedStatus {
return fmt.Errorf("unknown status: %s", p.Status)
return fmt.Errorf("unknown status: %s", status)
}

return nil
}

Expand Down