gitlab.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package gitlab
  13. import (
  14. "log"
  15. "os"
  16. gitlab "github.com/xanzy/go-gitlab"
  17. )
  18. // Requires a token to be set in environment variable
  19. var GITLAB_TOKEN = os.Getenv("GITLAB_TOKEN")
  20. var GITLAB_PROJECT_ID = os.Getenv("GITLAB_PROJECT_ID")
  21. type GitlabCredentials struct {
  22. Token string `json:"token"`
  23. }
  24. // Gitlab struct with reference to a github client and a projectID
  25. type Gitlab struct {
  26. client *gitlab.Client
  27. projectID interface{}
  28. }
  29. // Function newGitlabProvider returns a reference to a new instance of a 'Gitlab' struct
  30. func NewGitlabProvider() *Gitlab {
  31. return &Gitlab{}
  32. }
  33. // Method on Gitlab to set up client with credentials and populate projectID
  34. func (g *Gitlab) NewGitlabClient(cred GitlabCredentials, projectID string) {
  35. var err error
  36. // Create a new Gitlab client with credentials
  37. g.client, err = gitlab.NewClient(cred.Token, nil)
  38. g.projectID = projectID
  39. if err != nil {
  40. log.Fatalf("Failed to create client: %v", err)
  41. }
  42. }