Skip to content

Commit

Permalink
feat: add logic to create repo hash (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp authored Feb 5, 2020
1 parent b51dab9 commit 2c2a435
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
23 changes: 0 additions & 23 deletions api/authentication.go → api/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/base64"
"fmt"
"net/http"
"strings"

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/token"
Expand All @@ -21,28 +20,6 @@ import (
"github.com/google/uuid"
)

// Login represents the API handler to
// process a user logging in to Vela.
func Login(c *gin.Context) {
// check if request was a POST
if strings.EqualFold(c.Request.Method, "POST") {
// assume all POST requests are coming from the CLI
AuthenticateCLI(c)

return
}

// capture an error if present
err := c.Request.FormValue("error")
if len(err) > 0 {
// redirect to initial login screen with error code
c.Redirect(http.StatusTemporaryRedirect, "/login/error?code="+err)
}

// redirect to our authentication handler
c.Redirect(http.StatusTemporaryRedirect, "/authenticate")
}

// Authenticate represents the API handler to
// process a user logging in to Vela from
// the API or UI.
Expand Down
34 changes: 34 additions & 0 deletions api/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package api

import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

// Login represents the API handler to
// process a user logging in to Vela.
func Login(c *gin.Context) {
// check if request was a POST
if strings.EqualFold(c.Request.Method, "POST") {
// assume all POST requests are coming from the CLI
AuthenticateCLI(c)

return
}

// capture an error if present
err := c.Request.FormValue("error")
if len(err) > 0 {
// redirect to initial login screen with error code
c.Redirect(http.StatusTemporaryRedirect, "/login/error?code="+err)
}

// redirect to our authentication handler
c.Redirect(http.StatusTemporaryRedirect, "/authenticate")
}
37 changes: 37 additions & 0 deletions api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"encoding/base64"
"fmt"
"net/http"
"strconv"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/go-vela/types/library"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -72,6 +74,22 @@ func CreateRepo(c *gin.Context) {
input.SetAllowPush(true)
}

// create unique id for the repo
uid, err := uuid.NewRandom()
if err != nil {
retErr := fmt.Errorf("unable to create UID for repo %s: %w", input.GetFullName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

input.SetHash(
base64.StdEncoding.EncodeToString(
[]byte(uid.String()),
),
)

// ensure repo is allowed to be activated
if !checkWhitelist(input, whitelist) {
retErr := fmt.Errorf("unable to activate repo: %s is not on whitelist", input.GetFullName())
Expand Down Expand Up @@ -309,6 +327,25 @@ func UpdateRepo(c *gin.Context) {
r.SetAllowPush(true)
}

// set hash for repo if no hash is already set
if len(r.GetHash()) == 0 {
// create unique id for the repo
uid, err := uuid.NewRandom()
if err != nil {
retErr := fmt.Errorf("unable to create UID for repo %s: %w", input.GetFullName(), err)

util.HandleError(c, http.StatusServiceUnavailable, retErr)

return
}

r.SetHash(
base64.StdEncoding.EncodeToString(
[]byte(uid.String()),
),
)
}

// send API call to update the repo
err = database.FromContext(c).UpdateRepo(r)
if err != nil {
Expand Down

0 comments on commit 2c2a435

Please sign in to comment.