-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgithub.go
112 lines (106 loc) · 2.58 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package controllers
import (
"bytes"
"encoding/json"
"errors"
_ "io/ioutil"
"github.com/gin-gonic/gin"
"github.com/sdslabs/gasper/configs"
"github.com/sdslabs/gasper/lib/factory"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/services/master/middlewares"
"github.com/sdslabs/gasper/types"
)
// Endpoint to create repository in GitHub
func CreateRepository(c *gin.Context) {
raw, err := c.GetRawData()
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
var data *types.RepositoryRequest = &types.RepositoryRequest{}
claims := middlewares.ExtractClaims(c)
if claims == nil {
utils.SendServerErrorResponse(c, errors.New("failed to extract JWT claims"))
return
}
err = json.Unmarshal(raw, data)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
response, err := factory.CreateGithubRepository(claims.Username + data.Name)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
responseBody := new(bytes.Buffer)
json.NewEncoder(responseBody).Encode(response)
c.Data(200, "application/json", responseBody.Bytes())
}
func FetchPAT(c *gin.Context) {
raw, err := c.GetRawData()
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
var data *types.EncryptKey= &types.EncryptKey{}
err = json.Unmarshal(raw, data)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
encryptedPAT, err := factory.Encrypt(data.PublicKey)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
response := &types.AccessToken{
PAT: encryptedPAT,
Username: configs.GithubConfig.Username,
Email: configs.GithubConfig.Email,
}
responseBody := new(bytes.Buffer)
json.NewEncoder(responseBody).Encode(response)
c.Data(200, "application/json", responseBody.Bytes())
}
func DeleteRepository(c *gin.Context){
raw, err := c.GetRawData()
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
var data *types.ApplicationRemote = &types.ApplicationRemote{}
err = json.Unmarshal(raw, data)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
success, err := factory.DeleteGithubRepository(data.GitURL)
if !success {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
} else {
c.JSON(200, gin.H{
"success": true,
})
}
}