提交 8a117d51 编写于 作者: LinuxSuRen's avatar LinuxSuRen

Fix that cannot create credentials

上级 98c10918
......@@ -20,7 +20,8 @@ type CredentialCreateOption struct {
Secret string
Type string
Scope string
Type string
RoundTripper http.RoundTripper
}
......@@ -33,11 +34,13 @@ func init() {
i18n.T("The store name of Jenkins credentials"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Type, "type", "", "basic",
i18n.T("The type of Jenkins credentials which could be: basic, secret"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.ID, "id", "", "",
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Scope, "scope", "", "GLOBAL",
i18n.T("The scope of Jenkins credentials which might be GLOBAL or SYSTEM"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.ID, "credential-id", "", "",
i18n.T("The ID of Jenkins credentials"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Username, "username", "", "",
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Username, "credential-username", "", "",
i18n.T("The Username of Jenkins credentials"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Password, "password", "", "",
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Password, "credential-password", "", "",
i18n.T("The Password of Jenkins credentials"))
credentialCreateCmd.Flags().StringVarP(&credentialCreateOption.Description, "desc", "", "",
i18n.T("The Description of Jenkins credentials"))
......@@ -74,6 +77,7 @@ var credentialCreateCmd = &cobra.Command{
Username: credentialCreateOption.Username,
Password: credentialCreateOption.Password,
Credential: client.Credential{
Scope: credentialCreateOption.Scope,
ID: credentialCreateOption.ID,
Description: credentialCreateOption.Description,
},
......@@ -82,6 +86,7 @@ var credentialCreateCmd = &cobra.Command{
err = jClient.CreateSecret(credentialCreateOption.Store, client.StringCredentials{
Secret: credentialCreateOption.Secret,
Credential: client.Credential{
Scope: credentialCreateOption.Scope,
ID: credentialCreateOption.ID,
Description: credentialCreateOption.Description,
},
......
......@@ -70,7 +70,9 @@ var _ = Describe("credential create command", func() {
})
It("should success with user name and password", func() {
credential := client.UsernamePasswordCredential{}
credential := client.UsernamePasswordCredential{
Credential: client.Credential{Scope: "GLOBAL"},
}
client.PrepareForCreateUsernamePasswordCredential(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", store, credential)
......@@ -81,7 +83,9 @@ var _ = Describe("credential create command", func() {
})
It("should success with secret", func() {
credential := client.StringCredentials{}
credential := client.StringCredentials{
Credential: client.Credential{Scope: "GLOBAL"},
}
client.PrepareForCreateSecretCredential(roundTripper, "http://localhost:8080/jenkins",
"admin", "111e3a2f0231198855dceaff96f20540a9", store, credential)
......
......@@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"go.uber.org/zap"
"io"
"io/ioutil"
"log"
......@@ -162,9 +163,8 @@ func (j *JenkinsCore) ErrorHandle(statusCode int, data []byte) (err error) {
} else {
err = fmt.Errorf("unexpected status code: %d", statusCode)
}
if j.Debug {
ioutil.WriteFile("debug.html", data, 0664)
}
logger.Debug("get response", zap.String("data", string(data)))
return
}
......
......@@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"fmt"
"go.uber.org/zap"
"net/url"
"strings"
......@@ -32,6 +33,8 @@ func (c *CredentialsManager) Delete(store, id string) (err error) {
func (c *CredentialsManager) Create(store, credential string) (err error) {
api := fmt.Sprintf("/credentials/store/%s/domain/_/createCredentials", store)
logger.Debug("create credential", zap.String("api", api), zap.String("payload", credential))
formData := url.Values{}
formData.Add("json", fmt.Sprintf(`{"credentials": %s}`, credential))
payload := strings.NewReader(formData.Encode())
......@@ -55,7 +58,6 @@ func (c *CredentialsManager) CreateUsernamePassword(store string, cred UsernameP
func (c *CredentialsManager) CreateSecret(store string, cred StringCredentials) (err error) {
var payload []byte
cred.Class = "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"
cred.Scope = "GLOBAL"
if payload, err = json.Marshal(cred); err == nil {
err = c.Create(store, string(payload))
}
......
......@@ -68,7 +68,9 @@ var _ = Describe("job test", func() {
Context("CreateSecret", func() {
It("should success", func() {
cred := client.StringCredentials{}
cred := client.StringCredentials{
Credential: client.Credential{Scope: "GLOBAL"},
}
client.PrepareForCreateSecretCredential(roundTripper, credentialsManager.URL,
"", "", store, cred)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册