@@ -32,92 +32,18 @@ import (
3232)
3333
3434const (
35- githubOwner = "im-goose"
36- githubRepo = "im-cloudbuild-workspace-github"
35+ githubRepo = "im-cloudbuild-workspace-github"
3736)
3837
39- type GitHubClient struct {
40- t * testing.T
41- client * github.Client
42- owner string
43- repoName string
44- repository * github.Repository
45- }
46-
47- func NewGitHubClient (t * testing.T , token , owner , repo string ) * GitHubClient {
48- t .Helper ()
49- client := github .NewClient (nil ).WithAuthToken (token )
50- return & GitHubClient {
51- t : t ,
52- client : client ,
53- owner : owner ,
54- repoName : repo ,
55- }
56- }
57-
58- // GetOpenPullRequest gets an open pull request for a given branch if it exists.
59- func (gh * GitHubClient ) GetOpenPullRequest (ctx context.Context , branch string ) * github.PullRequest {
60- opts := & github.PullRequestListOptions {
61- State : "open" ,
62- Head : branch ,
63- }
64- prs , resp , err := gh .client .PullRequests .List (ctx , gh .owner , gh .repoName , opts )
65- if resp .StatusCode != 422 && err != nil {
66- gh .t .Fatal (err .Error ())
67- }
68- if len (prs ) == 0 {
69- return nil
70- }
71- return prs [0 ]
72- }
73-
74- func (gh * GitHubClient ) CreatePullRequest (ctx context.Context , title , branch , base string ) * github.PullRequest {
75- newPR := & github.NewPullRequest {
76- Title : github .String (title ),
77- Head : github .String (branch ),
78- Base : github .String (base ),
79- }
80- pr , _ , err := gh .client .PullRequests .Create (ctx , gh .owner , gh .repoName , newPR )
81- if err != nil {
82- gh .t .Fatal (err .Error ())
83- }
84- return pr
85- }
86-
87- func (gh * GitHubClient ) MergePullRequest (ctx context.Context , pr * github.PullRequest , commitTitle , commitMessage string ) * github.PullRequestMergeResult {
88- result , _ , err := gh .client .PullRequests .Merge (ctx , gh .owner , gh .repoName , * pr .Number , commitMessage , nil )
89- if err != nil {
90- gh .t .Fatal (err .Error ())
91- }
92- return result
93- }
94-
95- func (gh * GitHubClient ) ClosePullRequest (ctx context.Context , pr * github.PullRequest ) {
96- pr .State = github .String ("closed" )
97- _ , _ , err := gh .client .PullRequests .Edit (ctx , gh .owner , gh .repoName , * pr .Number , pr )
98- if err != nil {
99- gh .t .Fatal (err .Error ())
100- }
101- }
102-
103- func (gh * GitHubClient ) GetRepository (ctx context.Context ) * github.Repository {
104- repo , resp , err := gh .client .Repositories .Get (ctx , gh .owner , gh .repoName )
105- if resp .StatusCode != 404 && err != nil {
106- gh .t .Fatal (err .Error ())
107- }
108- gh .repository = repo
109- return repo
110- }
111-
11238func TestIMCloudBuildWorkspaceGitHub (t * testing.T ) {
11339 ctx := context .Background ()
11440
11541 githubPAT := cftutils .ValFromEnv (t , "IM_GITHUB_PAT" )
116- client := NewGitHubClient (t , githubPAT , githubOwner , githubRepo )
42+ client := utils . NewGitHubClient (t , githubPAT , githubRepo )
11743 client .GetRepository (ctx )
11844
11945 // Testing the module's feature of appending the ".git" suffix if it's missing
120- repoURL := strings .TrimSuffix (client .repository .GetCloneURL (), ".git" )
46+ repoURL := strings .TrimSuffix (client .Repository .GetCloneURL (), ".git" )
12147 vars := map [string ]interface {}{
12248 "im_github_pat" : githubPAT ,
12349 "repository_url" : repoURL ,
@@ -138,7 +64,7 @@ func TestIMCloudBuildWorkspaceGitHub(t *testing.T) {
13864 projectID := bpt .GetStringOutput ("project_id" )
13965 secretID := bpt .GetStringOutput ("github_secret_id" )
14066 triggerLocation := "us-central1"
141- repoURLSplit := strings .Split (client .repository .GetCloneURL (), "/" )
67+ repoURLSplit := strings .Split (client .Repository .GetCloneURL (), "/" )
14268
14369 // CB P4SA IAM
14470 projectNum := gcloud .Runf (t , "projects describe %s --format='value(projectNumber)'" , projectID ).Get ("projectNumber" )
0 commit comments