|
1 | 1 | package porcelain
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/Sirupsen/logrus" |
4 | 8 | "github.com/netlify/open-api/go/models"
|
5 | 9 | "github.com/netlify/open-api/go/plumbing/operations"
|
6 | 10 | "github.com/netlify/open-api/go/porcelain/context"
|
7 | 11 | )
|
8 | 12 |
|
| 13 | +const ( |
| 14 | + ticketingTimeout = time.Minute * 5 |
| 15 | +) |
| 16 | + |
9 | 17 | // Create a login ticket to authenticate a user
|
10 | 18 | func (n *Netlify) CreateTicket(ctx context.Context, clientID string) (*models.Ticket, error) {
|
11 | 19 | params := operations.NewCreateTicketParams().WithClientID(clientID)
|
@@ -39,3 +47,33 @@ func (n *Netlify) ExchangeTicket(ctx context.Context, ticketID string) (*models.
|
39 | 47 |
|
40 | 48 | return resp.Payload, nil
|
41 | 49 | }
|
| 50 | + |
| 51 | +func (n *Netlify) WaitUntilTicketAuthorized(ctx context.Context, ticket *models.Ticket) (*models.Ticket, error) { |
| 52 | + authInfo := context.GetAuthInfo(ctx) |
| 53 | + ticker := time.NewTicker(2 * time.Second) |
| 54 | + defer ticker.Stop() |
| 55 | + |
| 56 | + params := operations.NewShowTicketParams().WithTicketID(ticket.ID) |
| 57 | + start := time.Now() |
| 58 | + for t := range ticker.C { |
| 59 | + resp, err := n.Netlify.Operations.ShowTicket(params, authInfo) |
| 60 | + if err != nil { |
| 61 | + time.Sleep(3 * time.Second) |
| 62 | + continue |
| 63 | + } |
| 64 | + context.GetLogger(ctx).WithFields(logrus.Fields{ |
| 65 | + "ticket_id": ticket.ID, |
| 66 | + "authorized": resp.Payload.Authorized, |
| 67 | + }).Debug("Waiting until deploy ready") |
| 68 | + |
| 69 | + if resp.Payload.Authorized { |
| 70 | + return resp.Payload, nil |
| 71 | + } |
| 72 | + |
| 73 | + if t.Sub(start) > ticketingTimeout { |
| 74 | + return nil, fmt.Errorf("Error: the authorization process timed out") |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return ticket, nil |
| 79 | +} |
0 commit comments