Skip to content

Commit

Permalink
Still working on create pipeline part
Browse files Browse the repository at this point in the history
  • Loading branch information
michelvocks committed Mar 6, 2018
1 parent 1ec0992 commit 5e9fc46
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 25 deletions.
16 changes: 14 additions & 2 deletions frontend/client/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ html {
background-color: #3f3d49;
}
.content-article label {
.label {
color: whitesmoke;
font-weight: normal;
}
Expand All @@ -132,7 +132,19 @@ html {
}
.title-text {
border-bottom: 1px solid;
border-bottom: 1px whitesmoke solid;
padding-bottom: 8px;
text-align: center;
color: #ff651d !important;
}
.modal-z-index {
z-index: 1025;
}
@media screen and (min-width: 768px) {
.modal-content {
width: 480px; /* either % (e.g. 60%) or px (400px) */
}
}
</style>
10 changes: 0 additions & 10 deletions frontend/client/components/layout/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,6 @@ export default {
height: 50px;
}
@media screen and (min-width: 768px) {
.modal-content {
width: 480px; /* either % (e.g. 60%) or px (400px) */
}
}
.navbar-start {
padding-left: 240px;
}
Expand Down Expand Up @@ -269,10 +263,6 @@ export default {
}
}
.modal-z-index {
z-index: 1025;
}
.sign-in-text {
font-size: 20px;
font-weight: bold;
Expand Down
147 changes: 136 additions & 11 deletions frontend/client/views/pipelines/create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
<div class="tile">
<div class="tile is-parent is-vertical">
<article class="tile is-child notification content-article">
<p class="title title-text">Compile from source</p>
<!--<p class="title title-text">Compile from source</p>-->
<div class="content">
<label class="label">Copy the link of your <strong>git repo</strong> here.</label>
<p class="control has-icons-left">
<input class="input is-medium input-bar" v-focus type="text" placeholder="Link to git repo ...">
<input class="input is-medium input-bar" v-focus v-model.lazy="gitURL" type="text" placeholder="Link to git repo ...">
<span class="icon is-small is-left">
<i class="fa fa-git"></i>
</span>
<span style="color: red;" v-if="gitNeedAuth">You are not authorized. Invalid username and/or password!</span>
<span style="color: red;" v-if="gitInvalidURL">Invalid link to git repo provided!</span>
<div>
<span>Branch:</span>
<div class="select is-fullwidth" v-if="gitBranches.length > 0">
<select v-model="gitBranchSelected">
<option v-for="branch in gitBranches" :key="branch" :value="branch">{{ branch }}</option>
</select>
</div>
</div>
</p>
<p class="control">
<a class="button">
<a class="button" v-on:click="showCredentialsModal">
<span class="icon">
<i class="fa fa-github"></i>
<i class="fa fa-certificate"></i>
</span>
<span>Add credentials</span>
</a>
<a class="button" style="margin-left: 20px;">
<span class="icon">
<i class="fa fa-github"></i>
</span>
<span>Add private key</span>
</a>
</p>
<hr>
<label class="label">Type the name of your pipeline. You can put your pipelines into folders by defining a path. For example <strong>MyFolder/MyAwesomePipeline</strong>.</label>
<p class="control has-icons-left">
<input class="input is-medium input-bar" type="text" placeholder="Pipeline name ...">
<input class="input is-medium input-bar" v-model="pipelineName" type="text" placeholder="Pipeline name ...">
<span class="icon is-small is-left">
<i class="fa fa-book"></i>
</span>
Expand All @@ -47,14 +51,135 @@
</div>
</div>
</div>

<!-- Credentials modal -->
<modal :visible="gitCredentialsModal" class="modal-z-index" @close="close">
<div class="box credentials-modal">
<div class="block credentials-modal-content">
<div class="credentials-modal-content">
<p class="control has-icons-left" style="padding-bottom: 5px;">
<input class="input is-medium input-bar" v-focus type="text" v-model="gitUsername" placeholder="Username">
<span class="icon is-small is-left">
<i class="fa fa-user-circle"></i>
</span>
</p>
<p class="control has-icons-left">
<input class="input is-medium input-bar" type="password" v-model="gitPassword" placeholder="Password">
<span class="icon is-small is-left">
<i class="fa fa-lock"></i>
</span>
</p>
</div>
<hr>
<div class="block credentials-modal-content">
<p class="control">
<label class="label">SSH Private Key:</label>
<textarea class="textarea input-bar" v-model="gitPrivateKey"></textarea>
</p>
</div>
<div class="credentials-modal-content">
<button class="button is-primary" v-on:click="close">Add Credentials</button>
</div>
</div>
</div>
</modal>
</div>
</template>

<script>
import { Modal } from 'vue-bulma-modal'
export default {
data () {
return {
gitURL: '',
gitNeedAuth: false,
gitInvalidURL: false,
gitCredentialsModal: false,
gitUsername: '',
gitPassword: '',
gitPrivateKey: '',
gitBranches: [
'Master'
],
gitBranchSelected: '',
pipelineName: ''
}
},
components: {
Modal
},
watch: {
gitURL: function () {
// lets check if we can access the git repo
var gitrepo = {
giturl: this.gitURL,
gituser: this.gitUsername,
gitpassword: this.gitPassword
}
this.$http.post('/api/v1/pipelines/gitlsremote', gitrepo)
.then((response) => {
// Reset error message before
this.gitNeedAuth = false
this.gitInvalidURL = false
// Get branches and set to master if available
this.gitBranches = response.data.branches
for (var i = 0; i < this.gitBranches.length; i++) {
if (this.gitBranches[i] === 'refs/heads/master') {
this.gitBranchSelected = this.gitBranches[i]
}
}
// if we cannot find master
if (!this.gitBranchSelected) {
this.gitBranchSelected = this.gitBranches[0]
}
console.log(response.data)
})
.catch((error) => {
// Need authentication
if (error.response && error.response.status === 403) {
this.gitNeedAuth = true
this.gitInvalidURL = false
} else if (error.response && error.response.status === 400) {
this.gitInvalidURL = true
this.gitNeedAuth = false
}
console.log(error.response.data)
})
}
},
methods: {
close () {
this.gitCredentialsModal = false
this.$emit('close')
},
showCredentialsModal () {
this.gitCredentialsModal = true
}
}
}
</script>

<style lang="scss" scoped>
.credentials-modal {
text-align: center;
background-color: #2a2735;
}
.credentials-modal-content {
margin: auto;
padding: 10px;
}
</style>
15 changes: 15 additions & 0 deletions gaia.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ type User struct {
JwtExpiry int64 `json:"jwtexpiry"`
}

// Pipeline represents a single pipeline
type Pipeline struct {
Name string `json:"pipelinename"`
Repo GitRepo `json:"gitrepo"`
}

// GitRepo represents a single git repository
type GitRepo struct {
URL string `json:"giturl"`
Username string `json:"gituser"`
Password string `json:"gitpassword"`
PrivateKey string `json:"gitkey"`
Branches []string `json:"branches"`
}

// Config holds all config options
type Config struct {
ListenPort string
Expand Down
5 changes: 3 additions & 2 deletions handlers/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ func UserLogin(ctx iris.Context) {
if err := ctx.ReadJSON(u); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
cfg.Logger.Error("error reading json during UserLogin", "error", err.Error())
return
}

// Authenticate user
user, err := storeService.UserAuth(u)
if err != nil {
cfg.Logger.Error("error during UserAuth: %s", err)
cfg.Logger.Error("error during UserAuth", "error", err.Error())
ctx.StatusCode(iris.StatusInternalServerError)
return
}
Expand All @@ -57,7 +58,7 @@ func UserLogin(ctx iris.Context) {
tokenstring, err := token.SignedString(jwtKey)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
cfg.Logger.Error("error signing jwt token: %s", err)
cfg.Logger.Error("error signing jwt token", "error", err.Error())
return
}
user.JwtExpiry = claims.ExpiresAt
Expand Down
1 change: 1 addition & 0 deletions handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func InitHandlers(c *gaia.Config, i *iris.Application, s *store.Store) error {
p := "/api/" + apiVersion + "/"

i.Post(p+"users/login", UserLogin)
i.Post(p+"pipelines/gitlsremote", PipelineGitLSRemote)

return nil
}
87 changes: 87 additions & 0 deletions handlers/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package handlers

import (
"strings"

"github.com/kataras/iris"
"github.com/michelvocks/gaia"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/client"
)

const (
refHead = "refs/heads"
)

// PipelineGitLSRemote checks for available git remote branches.
// This is the perfect way to check if we have access to a given repo.
func PipelineGitLSRemote(ctx iris.Context) {
repo := &gaia.GitRepo{}
if err := ctx.ReadJSON(repo); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}

// Create new endpoint
ep, err := transport.NewEndpoint(repo.URL)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}

// Attach credentials if provided
if repo.Username != "" && repo.Password != "" {
ep.User = repo.Username
ep.Password = repo.Password
}

// Create client
cl, err := client.NewClient(ep)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}

// Open new session
s, err := cl.NewUploadPackSession(ep, nil)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}
defer s.Close()

// Get advertised references (e.g. branches)
// We have to reset the username and password to
// prevent go-git setting the credentials in the URL
// which will not be URL encoded.
// https://github.com/src-d/go-git/issues/723
ep.User = ""
ep.Password = ""
repo.Password = ""
ar, err := s.AdvertisedReferences()
if err == transport.ErrAuthenticationRequired {
ctx.StatusCode(iris.StatusForbidden)
ctx.WriteString(err.Error())
return
} else if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
return
}

// Iterate all references
repo.Branches = []string{}
for ref := range ar.References {
// filter for head refs which is a branch
if strings.Contains(ref, refHead) {
repo.Branches = append(repo.Branches, ref)
}
}

// Return branches
ctx.JSON(repo)
}

0 comments on commit 5e9fc46

Please sign in to comment.