2019-09-16 19:36:27 +00:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-01-26 14:29:11 +00:00
|
|
|
"time"
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2019-09-24 12:42:46 +00:00
|
|
|
"github.com/volker-raschek/docker-hub-description-updater/pkg/types"
|
2019-09-16 19:36:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dockerHubAPI = "https://hub.docker.com/v2"
|
|
|
|
)
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
type Hub struct {
|
|
|
|
client *http.Client
|
|
|
|
credentials *types.LoginCredentials
|
|
|
|
token *types.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepository returns a repository struct
|
|
|
|
func (h *Hub) GetRepository(namespace string, name string) (*types.Repository, error) {
|
2019-09-16 19:36:27 +00:00
|
|
|
|
|
|
|
if len(namespace) <= 0 {
|
|
|
|
return nil, errorNoNamespaceDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) <= 0 {
|
|
|
|
return nil, errorNoRepositoryDefined
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
rawURL := fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, namespace, name)
|
|
|
|
url, err := url.Parse(rawURL)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseURL, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
|
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
if h.token == nil {
|
|
|
|
token, err := h.getToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.token = token
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
2020-01-26 14:29:11 +00:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("JWT %v", h.token.Token))
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
resp, err := h.client.Do(req)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToSendRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repository := new(types.Repository)
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := jsonDecoder.Decode(repository); err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return repository, nil
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
func (h *Hub) getToken() (*types.Token, error) {
|
2019-09-16 19:36:27 +00:00
|
|
|
loginBuffer := new(bytes.Buffer)
|
|
|
|
jsonEncoder := json.NewEncoder(loginBuffer)
|
2020-01-26 14:29:11 +00:00
|
|
|
if err := jsonEncoder.Encode(h.credentials); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
rawURL := fmt.Sprintf("%v/users/login/", dockerHubAPI)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, rawURL, loginBuffer)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
resp, err := h.client.Do(req)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token := new(types.Token)
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := jsonDecoder.Decode(token); err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
// PatchRepository updates the docker hub repository
|
|
|
|
func (h *Hub) PatchRepository(repository *types.Repository) (*types.Repository, error) {
|
2019-09-16 19:36:27 +00:00
|
|
|
|
|
|
|
if len(repository.Namespcace) <= 0 {
|
|
|
|
return nil, errorNoNamespaceDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(repository.Name) <= 0 {
|
|
|
|
return nil, errorNoRepositoryDefined
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
if h.token == nil {
|
|
|
|
token, err := h.getToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.token = token
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
}
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
rawURL := fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, repository.Namespcace, repository.Name)
|
|
|
|
patchURL, err := url.Parse(rawURL)
|
2019-11-12 19:11:40 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseURL, err)
|
2019-11-12 19:11:40 +00:00
|
|
|
}
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
data := &url.Values{}
|
2019-09-16 19:36:27 +00:00
|
|
|
data.Set("full_description", repository.FullDescription)
|
2020-01-26 14:29:11 +00:00
|
|
|
patchURL.RawQuery = data.Encode()
|
2019-09-16 19:36:27 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPatch, patchURL.String(), nil)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
2020-01-26 14:29:11 +00:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("JWT %v", h.token.Token))
|
2019-11-13 08:40:02 +00:00
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
resp, err := h.client.Do(req)
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2020-01-26 14:29:11 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-01-26 14:29:11 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
patchedRepository := new(types.Repository)
|
2019-11-12 19:11:40 +00:00
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
2020-01-26 14:29:11 +00:00
|
|
|
if err := jsonDecoder.Decode(h.token); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return patchedRepository, nil
|
|
|
|
}
|
2020-01-26 14:29:11 +00:00
|
|
|
|
|
|
|
func New(credentials *types.LoginCredentials) *Hub {
|
|
|
|
return &Hub{
|
|
|
|
client: &http.Client{
|
|
|
|
Timeout: time.Second * 15,
|
|
|
|
},
|
|
|
|
credentials: credentials,
|
|
|
|
}
|
|
|
|
}
|