github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/gits/interface.go (about) 1 package gits 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "time" 8 9 "github.com/google/go-github/v32/github" 10 "github.com/olli-ai/jx/v2/pkg/auth" 11 gitcfg "gopkg.in/src-d/go-git.v4/config" 12 ) 13 14 // OrganisationLister returns a slice of GitOrganisation 15 //go:generate pegomock generate github.com/olli-ai/jx/v2/pkg/gits OrganisationLister -o mocks/organisation_lister.go 16 type OrganisationLister interface { 17 ListOrganisations() ([]GitOrganisation, error) 18 } 19 20 // OrganisationChecker verifies if an user is member of an organization 21 //go:generate pegomock generate github.com/olli-ai/jx/v2/pkg/gits OrganisationChecker -o mocks/organisation_checker.go 22 type OrganisationChecker interface { 23 IsUserInOrganisation(user string, organisation string) (bool, error) 24 } 25 26 // GitProvider is the interface for abstracting use of different git provider APIs 27 //go:generate pegomock generate github.com/olli-ai/jx/v2/pkg/gits GitProvider -o mocks/git_provider.go 28 type GitProvider interface { 29 OrganisationLister 30 31 ListRepositories(org string) ([]*GitRepository, error) 32 33 CreateRepository(org string, name string, private bool) (*GitRepository, error) 34 35 GetRepository(org string, name string) (*GitRepository, error) 36 37 DeleteRepository(org string, name string) error 38 39 ForkRepository(originalOrg string, name string, destinationOrg string) (*GitRepository, error) 40 41 RenameRepository(org string, name string, newName string) (*GitRepository, error) 42 43 ValidateRepositoryName(org string, name string) error 44 45 CreatePullRequest(data *GitPullRequestArguments) (*GitPullRequest, error) 46 47 UpdatePullRequest(data *GitPullRequestArguments, number int) (*GitPullRequest, error) 48 49 UpdatePullRequestStatus(pr *GitPullRequest) error 50 51 AddLabelsToIssue(owner, repo string, number int, labels []string) error 52 53 GetPullRequest(owner string, repo *GitRepository, number int) (*GitPullRequest, error) 54 55 ListOpenPullRequests(owner string, repo string) ([]*GitPullRequest, error) 56 57 GetPullRequestCommits(owner string, repo *GitRepository, number int) ([]*GitCommit, error) 58 59 PullRequestLastCommitStatus(pr *GitPullRequest) (string, error) 60 61 ListCommitStatus(org string, repo string, sha string) ([]*GitRepoStatus, error) 62 63 ListCommits(owner string, repo string, opt *ListCommitsArguments) ([]*GitCommit, error) 64 65 UpdateCommitStatus(org string, repo string, sha string, status *GitRepoStatus) (*GitRepoStatus, error) 66 67 MergePullRequest(pr *GitPullRequest, message string) error 68 69 CreateWebHook(data *GitWebHookArguments) error 70 71 ListWebHooks(org string, repo string) ([]*GitWebHookArguments, error) 72 73 UpdateWebHook(data *GitWebHookArguments) error 74 75 IsGitHub() bool 76 77 IsGitea() bool 78 79 IsBitbucketCloud() bool 80 81 IsBitbucketServer() bool 82 83 IsGerrit() bool 84 85 Kind() string 86 87 GetIssue(org string, name string, number int) (*GitIssue, error) 88 89 IssueURL(org string, name string, number int, isPull bool) string 90 91 SearchIssues(org string, name string, state string) ([]*GitIssue, error) 92 93 SearchIssuesClosedSince(org string, name string, t time.Time) ([]*GitIssue, error) 94 95 CreateIssue(owner string, repo string, issue *GitIssue) (*GitIssue, error) 96 97 HasIssues() bool 98 99 AddPRComment(pr *GitPullRequest, comment string) error 100 101 CreateIssueComment(owner string, repo string, number int, comment string) error 102 103 UpdateRelease(owner string, repo string, tag string, releaseInfo *GitRelease) error 104 105 UpdateReleaseStatus(owner string, repo string, tag string, releaseInfo *GitRelease) error 106 107 ListReleases(org string, name string) ([]*GitRelease, error) 108 109 GetRelease(org string, name string, tag string) (*GitRelease, error) 110 111 UploadReleaseAsset(org string, repo string, id int64, name string, asset *os.File) (*GitReleaseAsset, error) 112 113 GetLatestRelease(org string, name string) (*GitRelease, error) 114 115 GetContent(org string, name string, path string, ref string) (*GitFileContent, error) 116 117 // returns the path relative to the Jenkins URL to trigger webhooks on this kind of repository 118 // 119 120 // e.g. for GitHub its /github-webhook/ 121 // other examples include: 122 // 123 // * gitlab: /gitlab/notify_commit 124 // https://github.com/elvanja/jenkins-gitlab-hook-plugin#notify-commit-hook 125 // 126 // * git plugin 127 // /git/notifyCommit?url= 128 // http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/ 129 // 130 // * gitea 131 // /gitea-webhook/post 132 // 133 // * generic webhook 134 // /generic-webhook-trigger/invoke?token=abc123 135 // https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin 136 137 JenkinsWebHookPath(gitURL string, secret string) string 138 139 // Label returns the Git service label or name 140 Label() string 141 142 // ServerURL returns the Git server URL 143 ServerURL() string 144 145 // BranchArchiveURL returns a URL to the ZIP archive for the git branch 146 BranchArchiveURL(org string, name string, branch string) string 147 148 // Returns the current username 149 CurrentUsername() string 150 151 // Returns the current user auth 152 UserAuth() auth.UserAuth 153 154 // Returns user info, if possible 155 UserInfo(username string) *GitUser 156 157 AddCollaborator(string, string, string) error 158 // TODO Refactor to remove bespoke types when we implement another provider 159 ListInvitations() ([]*github.RepositoryInvitation, *github.Response, error) 160 // TODO Refactor to remove bespoke types when we implement another provider 161 AcceptInvitation(int64) (*github.Response, error) 162 163 // ShouldForkForPullRequest returns true if we should create a personal fork of this repository 164 // before creating a pull request 165 ShouldForkForPullRequest(originalOwner string, repoName string, username string) bool 166 167 GetBranch(owner string, repo string, branch string) (*GitBranch, error) 168 169 GetProjects(owner string, repo string) ([]GitProject, error) 170 171 IsWikiEnabled(owner string, repo string) (bool, error) 172 173 ConfigureFeatures(owner string, repo string, issues *bool, projects *bool, wikis *bool) (*GitRepository, error) 174 } 175 176 // Gitter defines common git actions used by Jenkins X via git cli 177 //go:generate pegomock generate github.com/olli-ai/jx/v2/pkg/gits Gitter -o mocks/gitter.go 178 type Gitter interface { 179 // IsVersionControlled returns true if the specified directory is under Git version control, otherwise false. 180 // An error is returned for unexpected IO errors. 181 IsVersionControlled(dir string) (bool, error) 182 183 Config(dir string, args ...string) error 184 FindGitConfigDir(dir string) (string, string, error) 185 PrintCreateRepositoryGenerateAccessToken(server *auth.AuthServer, username string, o io.Writer) 186 187 Status(dir string) error 188 Server(dir string) (string, error) 189 Info(dir string) (*GitRepository, error) 190 IsFork(dir string) (bool, error) 191 Version() (string, error) 192 RepoName(org, repoName string) string 193 194 Username(dir string) (string, error) 195 SetUsername(dir string, username string) error 196 Email(dir string) (string, error) 197 SetEmail(dir string, email string) error 198 GetAuthorEmailForCommit(dir string, sha string) (string, error) 199 200 Init(dir string) error 201 Clone(url string, directory string) error 202 CloneBare(dir string, url string) error 203 PushMirror(dir string, url string) error 204 205 // ShallowCloneBranch TODO not sure if this method works any more - consider using ShallowClone(dir, url, branch, "") 206 ShallowCloneBranch(url string, branch string, directory string) error 207 ShallowClone(dir string, url string, commitish string, pullRequest string) error 208 FetchUnshallow(dir string) error 209 IsShallow(dir string) (bool, error) 210 Push(dir string, remote string, force bool, refspec ...string) error 211 PushMaster(dir string) error 212 PushTag(dir string, tag string) error 213 // CreateAuthenticatedURL adds username and password into the specified git URL. 214 CreateAuthenticatedURL(url string, userAuth *auth.UserAuth) (string, error) 215 ForcePushBranch(dir string, localBranch string, remoteBranch string) error 216 CloneOrPull(url string, directory string) error 217 Pull(dir string) error 218 PullRemoteBranches(dir string) error 219 PullUpstream(dir string) error 220 221 // ResetToUpstream resets the given branch to the upstream version 222 ResetToUpstream(dir string, branch string) error 223 224 AddRemote(dir string, name string, url string) error 225 SetRemoteURL(dir string, name string, gitURL string) error 226 UpdateRemote(dir, url string) error 227 DiscoverRemoteGitURL(gitConf string) (string, error) 228 DiscoverUpstreamGitURL(gitConf string) (string, error) 229 RemoteBranches(dir string) ([]string, error) 230 RemoteBranchNames(dir string, prefix string) ([]string, error) 231 RemoteMergedBranchNames(dir string, prefix string) ([]string, error) 232 GetRemoteUrl(config *gitcfg.Config, name string) string 233 RemoteUpdate(dir string) error 234 LocalBranches(dir string) ([]string, error) 235 Remotes(dir string) ([]string, error) 236 237 Branch(dir string) (string, error) 238 CreateBranchFrom(dir string, branchName string, startPoint string) error 239 CreateBranch(dir string, branch string) error 240 CheckoutRemoteBranch(dir string, branch string) error 241 Checkout(dir string, branch string) error 242 CheckoutCommitFiles(dir string, commit string, files []string) error 243 CheckoutOrphan(dir string, branch string) error 244 ConvertToValidBranchName(name string) string 245 FetchBranch(dir string, repo string, refspec ...string) error 246 FetchBranchShallow(dir string, repo string, refspec ...string) error 247 FetchBranchUnshallow(dir string, repo string, refspec ...string) error 248 Merge(dir string, commitish string) error 249 MergeTheirs(dir string, commitish string) error 250 Reset(dir string, commitish string, hard bool) error 251 RebaseTheirs(dir string, upstream string, branch string, skipEmpty bool) error 252 CherryPick(dir string, commitish string) error 253 CherryPickTheirs(dir string, commitish string) error 254 CherryPickTheirsKeepRedundantCommits(dir string, commitish string) error 255 256 StashPush(dir string) error 257 StashPop(dir string) error 258 259 Remove(dir, fileName string) error 260 RemoveForce(dir, fileName string) error 261 CleanForce(dir, fileName string) error 262 Add(dir string, args ...string) error 263 264 CommitIfChanges(dir string, message string) error 265 CommitDir(dir string, message string) error 266 AddCommit(dir string, msg string) error 267 AddCommitFiles(dir string, msg string, files []string) error 268 HasChanges(dir string) (bool, error) 269 HasFileChanged(dir string, fileName string) (bool, error) 270 Diff(dir string) (string, error) 271 ListChangedFilesFromBranch(dir string, branch string) (string, error) 272 LoadFileFromBranch(dir string, branch string, file string) (string, error) 273 274 GetLatestCommitMessage(dir string) (string, error) 275 GetCommitPointedToByPreviousTag(dir string) (string, string, error) 276 GetCommitPointedToByLatestTag(dir string) (string, string, error) 277 GetCommitPointedToByTag(dir string, tag string) (string, error) 278 FetchTags(dir string) error 279 FetchRemoteTags(dir string, repo string) error 280 Tags(dir string) ([]string, error) 281 FilterTags(dir string, filter string) ([]string, error) 282 CreateTag(dir string, tag string, msg string) error 283 GetLatestCommitSha(dir string) (string, error) 284 GetFirstCommitSha(dir string) (string, error) 285 GetCommits(dir string, start string, end string) ([]GitCommit, error) 286 RevParse(dir string, rev string) (string, error) 287 GetCommitsNotOnAnyRemote(dir string, branch string) ([]GitCommit, error) 288 Describe(dir string, contains bool, commitish string, abbrev string, fallback bool) (string, string, error) 289 IsAncestor(dir string, possibleAncestor string, commitish string) (bool, error) 290 291 GetRevisionBeforeDate(dir string, t time.Time) (string, error) 292 GetRevisionBeforeDateText(dir string, dateText string) (string, error) 293 DeleteRemoteBranch(dir string, remoteName string, branch string) error 294 DeleteLocalBranch(dir string, branch string) error 295 296 SetUpstreamTo(dir string, branch string) error 297 298 WriteRepoAttributes(dir string, contents string) error 299 ReadRepoAttributes(dir string) (string, error) 300 } 301 302 // PullRequestDetails is the details for creating a pull request 303 type PullRequestDetails struct { 304 Message string 305 BranchName string 306 Title string 307 Labels []string 308 } 309 310 func (p *PullRequestDetails) String() string { 311 return fmt.Sprintf("Branch Name: %s; Title: %s; Message: %s", p.BranchName, p.Title, p.Message) 312 }