diff --git a/README.md b/README.md index d46077ff..79a8c30c 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/out.go b/out.go index cd6245c9..15358b98 100644 --- a/out.go +++ b/out.go @@ -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)) @@ -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) } } @@ -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"` @@ -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 }