-
Notifications
You must be signed in to change notification settings - Fork 787
/
github_app.go
112 lines (96 loc) · 3.67 KB
/
github_app.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 github
import (
"fmt"
"encoding/json"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/clients"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
//GithubApp represents a Github App install
type GithubApp struct {
Factory clients.Factory
}
type response struct {
Installed bool
AccessToRepo bool
URL string
AppName string
}
// Install - confirms that the github app is installed and if it isn't then prints out a url for the user to install
func (gh *GithubApp) Install(owner string, repo string, fileHandles util.IOFileHandles, newRepo bool) (bool, error) {
installed, accessToRepo, url, appName, err := gh.isInstalled(owner, repo)
if err != nil {
return false, errors.Wrap(err, "when querying whether the Github App is installed")
}
if appName == "" {
// if the appName is empty lets use Jenkins X as the default
appName = "Jenkins X"
}
if installed {
fmt.Println(fmt.Sprintf("'%s' Github App installed", util.ColorInfo(appName)))
if newRepo {
// if this is a new repo we can't confirm if it has access at this stage
return false, nil
}
if !accessToRepo {
fmt.Fprintf(fileHandles.Out, "Please confirm '%s' Github App has access to repository %s. Click this url \n%s\n\n", util.ColorInfo(appName), repo, util.ColorInfo(url))
}
} else {
fmt.Fprintf(fileHandles.Out, "Please install '%s' Github App into your organisation/account %s and allow access to repository %s. Click this url \n%s\n\n", util.ColorInfo(appName), owner, repo, util.ColorInfo(url))
}
if !accessToRepo {
accessToRepo, err = util.Confirm(fmt.Sprintf("Does the '%s' Github App have access to repository", util.ColorInfo(appName)), false,
fmt.Sprintf("Please install '%s' Github App into your organisation and grant access to repositories", util.ColorInfo(appName)), fileHandles)
if err != nil {
return false, err
}
}
if !accessToRepo {
return false, errors.New(fmt.Sprintf("Please install '%s' Github App", util.ColorInfo(appName)))
}
return accessToRepo, err
}
func (gh *GithubApp) isInstalled(owner string, repo string) (bool, bool, string, string, error) {
requirementConfig, err := gh.getRequirementConfig()
if err != nil {
return false, false, "", "", err
}
if requirementConfig.GithubApp != nil {
url := requirementConfig.GithubApp.URL + "/installed/" + owner + "/" + repo
if url != "" {
respBody, err := util.CallWithExponentialBackOff(url, "", "GET", []byte{}, nil)
log.Logger().Debug(string(respBody))
if err != nil {
return false, false, "", "", errors.Wrapf(err, "error getting response from github app via %s", url)
}
response := &response{}
err = json.Unmarshal(respBody, response)
if err != nil {
return false, false, "", "", errors.Wrapf(err, "error marshalling response %s", url)
}
return response.Installed, response.AccessToRepo, response.URL, response.AppName, nil
}
}
return false, false, "", "", errors.New("unable to locate github app ")
}
func (gh *GithubApp) getRequirementConfig() (*config.RequirementsConfig, error) {
jxClient, ns, err := gh.Factory.CreateJXClient()
if err != nil {
log.Logger().Errorf("error creating jx client %v", err)
return nil, err
}
teamSettings, err := kube.GetDevEnvTeamSettings(jxClient, ns)
if err != nil {
log.Logger().Errorf("error getting team settings from jx client %v", err)
return nil, err
}
requirementConfig, err := config.GetRequirementsConfigFromTeamSettings(teamSettings)
if err != nil {
log.Logger().Errorf("error getting requirement config from team settings %v", err)
return nil, err
}
return requirementConfig, nil
}