github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/scm_provider/gitlab_test.go (about)

     1  package scm_provider
     2  
     3  import (
     4  	"crypto/x509"
     5  	"encoding/pem"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
    16  )
    17  
    18  func gitlabMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
    19  	t.Helper()
    20  	return func(w http.ResponseWriter, r *http.Request) {
    21  		w.Header().Set("Content-Type", "application/json")
    22  		fmt.Println(r.RequestURI)
    23  		switch r.RequestURI {
    24  		case "/api/v4":
    25  			fmt.Println("here1")
    26  		case "/api/v4/groups/test-argocd-proton/projects?include_subgroups=false&per_page=100", "/api/v4/groups/test-argocd-proton/projects?include_subgroups=false&per_page=100&topic=&with_shared=false":
    27  			fmt.Println("here")
    28  			_, err := io.WriteString(w, `[{
    29  				"id": 27084533,
    30  				"description": "",
    31  				"name": "argocd",
    32  				"name_with_namespace": "test argocd proton / argocd",
    33  				"path": "argocd",
    34  				"path_with_namespace": "test-argocd-proton/argocd",
    35  				"created_at": "2021-06-01T17:30:44.724Z",
    36  				"default_branch": "master",
    37  				"tag_list": [
    38  					"test-topic"
    39  				],
    40  				"topics": [
    41  					"test-topic"
    42  				],
    43  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git",
    44  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git",
    45  				"web_url": "https://gitlab.com/test-argocd-proton/argocd",
    46  				"readme_url": null,
    47  				"avatar_url": null,
    48  				"forks_count": 0,
    49  				"star_count": 0,
    50  				"last_activity_at": "2021-06-04T08:19:51.656Z",
    51  				"namespace": {
    52  					"id": 12258515,
    53  					"name": "test argocd proton",
    54  					"path": "test-argocd-proton",
    55  					"kind": "gro* Connection #0 to host gitlab.com left intact up ",
    56  					"full_path ": "test - argocd - proton ",
    57  					"parent_id ": null,
    58  					"avatar_url ": null,
    59  					"web_url ": "https: //gitlab.com/groups/test-argocd-proton"
    60  				},
    61  				"container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/argocd",
    62  				"_links": {
    63  					"self": "https://gitlab.com/api/v4/projects/27084533",
    64  					"issues": "https://gitlab.com/api/v4/projects/27084533/issues",
    65  					"merge_requests": "https://gitlab.com/api/v4/projects/27084533/merge_requests",
    66  					"repo_branches": "https://gitlab.com/api/v4/projects/27084533/repository/branches",
    67  					"labels": "https://gitlab.com/api/v4/projects/27084533/labels",
    68  					"events": "https://gitlab.com/api/v4/projects/27084533/events",
    69  					"members": "https://gitlab.com/api/v4/projects/27084533/members",
    70  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084533/cluster_agents"
    71  				},
    72  				"packages_enabled": true,
    73  				"empty_repo": false,
    74  				"archived": false,
    75  				"visibility": "public",
    76  				"resolve_outdated_diff_discussions": false,
    77  				"container_expiration_policy": {
    78  					"cadence": "1d",
    79  					"enabled": false,
    80  					"keep_n": 10,
    81  					"older_than": "90d",
    82  					"name_regex": ".*",
    83  					"name_regex_keep": null,
    84  					"next_run_at": "2021-06-02T17:30:44.740Z"
    85  				},
    86  				"issues_enabled": true,
    87  				"merge_requests_enabled": true,
    88  				"wiki_enabled": true,
    89  				"jobs_enabled": true,
    90  				"snippets_enabled": true,
    91  				"container_registry_enabled": true,
    92  				"service_desk_enabled": true,
    93  				"can_create_merge_request_in": false,
    94  				"issues_access_level": "enabled",
    95  				"repository_access_level": "enabled",
    96  				"merge_requests_access_level": "enabled",
    97  				"forking_access_level": "enabled",
    98  				"wiki_access_level": "enabled",
    99  				"builds_access_level": "enabled",
   100  				"snippets_access_level": "enabled",
   101  				"pages_access_level": "enabled",
   102  				"operations_access_level": "enabled",
   103  				"analytics_access_level": "enabled",
   104  				"container_registry_access_level": "enabled",
   105  				"security_and_compliance_access_level": "private",
   106  				"emails_disabled": null,
   107  				"shared_runners_enabled": true,
   108  				"lfs_enabled": true,
   109  				"creator_id": 2378866,
   110  				"import_status": "none",
   111  				"open_issues_count": 0,
   112  				"ci_default_git_depth": 50,
   113  				"ci_forward_deployment_enabled": true,
   114  				"ci_job_token_scope_enabled": false,
   115  				"public_jobs": true,
   116  				"build_timeout": 3600,
   117  				"auto_cancel_pending_pipelines": "enabled",
   118  				"ci_config_path": "",
   119  				"shared_with_groups": [],
   120  				"only_allow_merge_if_pipeline_succeeds": false,
   121  				"allow_merge_on_skipped_pipeline": null,
   122  				"restrict_user_defined_variables": false,
   123  				"request_access_enabled": true,
   124  				"only_allow_merge_if_all_discussions_are_resolved": false,
   125  				"remove_source_branch_after_merge": true,
   126  				"printing_merge_request_link_enabled": true,
   127  				"merge_method": "merge",
   128  				"squash_option": "default_off",
   129  				"suggestion_commit_message": null,
   130  				"merge_commit_template": null,
   131  				"squash_commit_template": null,
   132  				"auto_devops_enabled": false,
   133  				"auto_devops_deploy_strategy": "continuous",
   134  				"autoclose_referenced_issues": true,
   135  				"keep_latest_artifact": true,
   136  				"runner_token_expiration_interval": null,
   137  				"approvals_before_merge": 0,
   138  				"mirror": false,
   139  				"external_authorization_classification_label": "",
   140  				"marked_for_deletion_at": null,
   141  				"marked_for_deletion_on": null,
   142  				"requirements_enabled": true,
   143  				"requirements_access_level": "enabled",
   144  				"security_and_compliance_enabled": false,
   145  				"compliance_frameworks": [],
   146  				"issues_template": null,
   147  				"merge_requests_template": null,
   148  				"merge_pipelines_enabled": false,
   149  				"merge_trains_enabled": false
   150  			}]`)
   151  			if err != nil {
   152  				t.Fail()
   153  			}
   154  		case "/api/v4/groups/test-argocd-proton/projects?include_subgroups=true&per_page=100&topic=&with_shared=false":
   155  			fmt.Println("here")
   156  			_, err := io.WriteString(w, `[{
   157  				"id": 27084533,
   158  				"description": "",
   159  				"name": "argocd",
   160  				"name_with_namespace": "test argocd proton / argocd",
   161  				"path": "argocd",
   162  				"path_with_namespace": "test-argocd-proton/argocd",
   163  				"created_at": "2021-06-01T17:30:44.724Z",
   164  				"default_branch": "master",
   165  				"tag_list": [
   166  					"test-topic",
   167  					"specific-topic"
   168  				],
   169  				"topics": [
   170  					"test-topic",
   171  					"specific-topic"
   172  				],
   173  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git",
   174  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git",
   175  				"web_url": "https://gitlab.com/test-argocd-proton/argocd",
   176  				"readme_url": null,
   177  				"avatar_url": null,
   178  				"forks_count": 0,
   179  				"star_count": 0,
   180  				"last_activity_at": "2021-06-04T08:19:51.656Z",
   181  				"namespace": {
   182  					"id": 12258515,
   183  					"name": "test argocd proton",
   184  					"path": "test-argocd-proton",
   185  					"kind": "gro* Connection #0 to host gitlab.com left intact up ",
   186  					"full_path ": "test - argocd - proton ",
   187  					"parent_id ": null,
   188  					"avatar_url ": null,
   189  					"web_url ": "https: //gitlab.com/groups/test-argocd-proton"
   190  				},
   191  				"container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/argocd",
   192  				"_links": {
   193  					"self": "https://gitlab.com/api/v4/projects/27084533",
   194  					"issues": "https://gitlab.com/api/v4/projects/27084533/issues",
   195  					"merge_requests": "https://gitlab.com/api/v4/projects/27084533/merge_requests",
   196  					"repo_branches": "https://gitlab.com/api/v4/projects/27084533/repository/branches",
   197  					"labels": "https://gitlab.com/api/v4/projects/27084533/labels",
   198  					"events": "https://gitlab.com/api/v4/projects/27084533/events",
   199  					"members": "https://gitlab.com/api/v4/projects/27084533/members",
   200  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084533/cluster_agents"
   201  				},
   202  				"packages_enabled": true,
   203  				"empty_repo": false,
   204  				"archived": false,
   205  				"visibility": "public",
   206  				"resolve_outdated_diff_discussions": false,
   207  				"container_expiration_policy": {
   208  					"cadence": "1d",
   209  					"enabled": false,
   210  					"keep_n": 10,
   211  					"older_than": "90d",
   212  					"name_regex": ".*",
   213  					"name_regex_keep": null,
   214  					"next_run_at": "2021-06-02T17:30:44.740Z"
   215  				},
   216  				"issues_enabled": true,
   217  				"merge_requests_enabled": true,
   218  				"wiki_enabled": true,
   219  				"jobs_enabled": true,
   220  				"snippets_enabled": true,
   221  				"container_registry_enabled": true,
   222  				"service_desk_enabled": true,
   223  				"can_create_merge_request_in": false,
   224  				"issues_access_level": "enabled",
   225  				"repository_access_level": "enabled",
   226  				"merge_requests_access_level": "enabled",
   227  				"forking_access_level": "enabled",
   228  				"wiki_access_level": "enabled",
   229  				"builds_access_level": "enabled",
   230  				"snippets_access_level": "enabled",
   231  				"pages_access_level": "enabled",
   232  				"operations_access_level": "enabled",
   233  				"analytics_access_level": "enabled",
   234  				"container_registry_access_level": "enabled",
   235  				"security_and_compliance_access_level": "private",
   236  				"emails_disabled": null,
   237  				"shared_runners_enabled": true,
   238  				"lfs_enabled": true,
   239  				"creator_id": 2378866,
   240  				"import_status": "none",
   241  				"open_issues_count": 0,
   242  				"ci_default_git_depth": 50,
   243  				"ci_forward_deployment_enabled": true,
   244  				"ci_job_token_scope_enabled": false,
   245  				"public_jobs": true,
   246  				"build_timeout": 3600,
   247  				"auto_cancel_pending_pipelines": "enabled",
   248  				"ci_config_path": "",
   249  				"shared_with_groups": [],
   250  				"only_allow_merge_if_pipeline_succeeds": false,
   251  				"allow_merge_on_skipped_pipeline": null,
   252  				"restrict_user_defined_variables": false,
   253  				"request_access_enabled": true,
   254  				"only_allow_merge_if_all_discussions_are_resolved": false,
   255  				"remove_source_branch_after_merge": true,
   256  				"printing_merge_request_link_enabled": true,
   257  				"merge_method": "merge",
   258  				"squash_option": "default_off",
   259  				"suggestion_commit_message": null,
   260  				"merge_commit_template": null,
   261  				"squash_commit_template": null,
   262  				"auto_devops_enabled": false,
   263  				"auto_devops_deploy_strategy": "continuous",
   264  				"autoclose_referenced_issues": true,
   265  				"keep_latest_artifact": true,
   266  				"runner_token_expiration_interval": null,
   267  				"approvals_before_merge": 0,
   268  				"mirror": false,
   269  				"external_authorization_classification_label": "",
   270  				"marked_for_deletion_at": null,
   271  				"marked_for_deletion_on": null,
   272  				"requirements_enabled": true,
   273  				"requirements_access_level": "enabled",
   274  				"security_and_compliance_enabled": false,
   275  				"compliance_frameworks": [],
   276  				"issues_template": null,
   277  				"merge_requests_template": null,
   278  				"merge_pipelines_enabled": false,
   279  				"merge_trains_enabled": false
   280  			},
   281  			{
   282  				"id": 27084538,
   283  				"description": "This is a Project from a Subgroup",
   284  				"name": "argocd-subgroup",
   285  				"name_with_namespace": "test argocd proton / subgroup / argocd-subgroup",
   286  				"path": "argocd-subgroup",
   287  				"path_with_namespace": "test-argocd-proton/subgroup/argocd-subgroup",
   288  				"created_at": "2021-06-01T17:30:44.724Z",
   289  				"default_branch": "master",
   290  				"tag_list": [
   291  					"test-topic"
   292  				],
   293  				"topics": [
   294  					"test-topic"
   295  				],
   296  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/subgroup/argocd-subgroup.git",
   297  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup.git",
   298  				"web_url": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup",
   299  				"readme_url": null,
   300  				"avatar_url": null,
   301  				"forks_count": 0,
   302  				"star_count": 0,
   303  				"last_activity_at": "2021-06-04T08:19:51.656Z",
   304  				"namespace": {
   305  					"id": 12258542,
   306  					"name": "subgroup",
   307  					"path": "subgroup",
   308  					"kind": "group ",
   309  					"full_path ": "test-argocd-proton/subgroup",
   310  					"parent_id ": 12258515,
   311  					"avatar_url ": null,
   312  					"web_url ": "https: //gitlab.com/groups/test-argocd-proton/subgroup"
   313  				},
   314  				"container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/subgroup/argocd",
   315  				"_links": {
   316  					"self": "https://gitlab.com/api/v4/projects/27084538",
   317  					"issues": "https://gitlab.com/api/v4/projects/27084538/issues",
   318  					"merge_requests": "https://gitlab.com/api/v4/projects/27084538/merge_requests",
   319  					"repo_branches": "https://gitlab.com/api/v4/projects/27084538/repository/branches",
   320  					"labels": "https://gitlab.com/api/v4/projects/27084538/labels",
   321  					"events": "https://gitlab.com/api/v4/projects/27084538/events",
   322  					"members": "https://gitlab.com/api/v4/projects/27084538/members",
   323  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084538/cluster_agents"
   324  				},
   325  				"packages_enabled": true,
   326  				"empty_repo": false,
   327  				"archived": false,
   328  				"visibility": "public",
   329  				"resolve_outdated_diff_discussions": false,
   330  				"container_expiration_policy": {
   331  					"cadence": "1d",
   332  					"enabled": false,
   333  					"keep_n": 10,
   334  					"older_than": "90d",
   335  					"name_regex": ".*",
   336  					"name_regex_keep": null,
   337  					"next_run_at": "2021-06-02T17:30:44.740Z"
   338  				},
   339  				"issues_enabled": true,
   340  				"merge_requests_enabled": true,
   341  				"wiki_enabled": true,
   342  				"jobs_enabled": true,
   343  				"snippets_enabled": true,
   344  				"container_registry_enabled": true,
   345  				"service_desk_enabled": true,
   346  				"can_create_merge_request_in": false,
   347  				"issues_access_level": "enabled",
   348  				"repository_access_level": "enabled",
   349  				"merge_requests_access_level": "enabled",
   350  				"forking_access_level": "enabled",
   351  				"wiki_access_level": "enabled",
   352  				"builds_access_level": "enabled",
   353  				"snippets_access_level": "enabled",
   354  				"pages_access_level": "enabled",
   355  				"operations_access_level": "enabled",
   356  				"analytics_access_level": "enabled",
   357  				"container_registry_access_level": "enabled",
   358  				"security_and_compliance_access_level": "private",
   359  				"emails_disabled": null,
   360  				"shared_runners_enabled": true,
   361  				"lfs_enabled": true,
   362  				"creator_id": 2378866,
   363  				"import_status": "none",
   364  				"open_issues_count": 0,
   365  				"ci_default_git_depth": 50,
   366  				"ci_forward_deployment_enabled": true,
   367  				"ci_job_token_scope_enabled": false,
   368  				"public_jobs": true,
   369  				"build_timeout": 3600,
   370  				"auto_cancel_pending_pipelines": "enabled",
   371  				"ci_config_path": "",
   372  				"shared_with_groups": [],
   373  				"only_allow_merge_if_pipeline_succeeds": false,
   374  				"allow_merge_on_skipped_pipeline": null,
   375  				"restrict_user_defined_variables": false,
   376  				"request_access_enabled": true,
   377  				"only_allow_merge_if_all_discussions_are_resolved": false,
   378  				"remove_source_branch_after_merge": true,
   379  				"printing_merge_request_link_enabled": true,
   380  				"merge_method": "merge",
   381  				"squash_option": "default_off",
   382  				"suggestion_commit_message": null,
   383  				"merge_commit_template": null,
   384  				"squash_commit_template": null,
   385  				"auto_devops_enabled": false,
   386  				"auto_devops_deploy_strategy": "continuous",
   387  				"autoclose_referenced_issues": true,
   388  				"keep_latest_artifact": true,
   389  				"runner_token_expiration_interval": null,
   390  				"approvals_before_merge": 0,
   391  				"mirror": false,
   392  				"external_authorization_classification_label": "",
   393  				"marked_for_deletion_at": null,
   394  				"marked_for_deletion_on": null,
   395  				"requirements_enabled": true,
   396  				"requirements_access_level": "enabled",
   397  				"security_and_compliance_enabled": false,
   398  				"compliance_frameworks": [],
   399  				"issues_template": null,
   400  				"merge_requests_template": null,
   401  				"merge_pipelines_enabled": false,
   402  				"merge_trains_enabled": false
   403  			}
   404  			]`)
   405  			if err != nil {
   406  				t.Fail()
   407  			}
   408  		case "/api/v4/groups/test-argocd-proton/projects?include_subgroups=false&per_page=100&topic=specific-topic&with_shared=false":
   409  			fmt.Println("here")
   410  			_, err := io.WriteString(w, `[{
   411  				"id": 27084533,
   412  				"description": "",
   413  				"name": "argocd",
   414  				"name_with_namespace": "test argocd proton / argocd",
   415  				"path": "argocd",
   416  				"path_with_namespace": "test-argocd-proton/argocd",
   417  				"created_at": "2021-06-01T17:30:44.724Z",
   418  				"default_branch": "master",
   419  				"tag_list": [
   420  					"test-topic",
   421  					"specific-topic"
   422  				],
   423  				"topics": [
   424  					"test-topic",
   425  					"specific-topic"
   426  				],
   427  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git",
   428  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git",
   429  				"web_url": "https://gitlab.com/test-argocd-proton/argocd",
   430  				"readme_url": null,
   431  				"avatar_url": null,
   432  				"forks_count": 0,
   433  				"star_count": 0,
   434  				"last_activity_at": "2021-06-04T08:19:51.656Z",
   435  				"namespace": {
   436  					"id": 12258515,
   437  					"name": "test argocd proton",
   438  					"path": "test-argocd-proton",
   439  					"kind": "gro* Connection #0 to host gitlab.com left intact up ",
   440  					"full_path ": "test - argocd - proton ",
   441  					"parent_id ": null,
   442  					"avatar_url ": null,
   443  					"web_url ": "https: //gitlab.com/groups/test-argocd-proton"
   444  				},
   445  				"container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/argocd",
   446  				"_links": {
   447  					"self": "https://gitlab.com/api/v4/projects/27084533",
   448  					"issues": "https://gitlab.com/api/v4/projects/27084533/issues",
   449  					"merge_requests": "https://gitlab.com/api/v4/projects/27084533/merge_requests",
   450  					"repo_branches": "https://gitlab.com/api/v4/projects/27084533/repository/branches",
   451  					"labels": "https://gitlab.com/api/v4/projects/27084533/labels",
   452  					"events": "https://gitlab.com/api/v4/projects/27084533/events",
   453  					"members": "https://gitlab.com/api/v4/projects/27084533/members",
   454  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084533/cluster_agents"
   455  				},
   456  				"packages_enabled": true,
   457  				"empty_repo": false,
   458  				"archived": false,
   459  				"visibility": "public",
   460  				"resolve_outdated_diff_discussions": false,
   461  				"container_expiration_policy": {
   462  					"cadence": "1d",
   463  					"enabled": false,
   464  					"keep_n": 10,
   465  					"older_than": "90d",
   466  					"name_regex": ".*",
   467  					"name_regex_keep": null,
   468  					"next_run_at": "2021-06-02T17:30:44.740Z"
   469  				},
   470  				"issues_enabled": true,
   471  				"merge_requests_enabled": true,
   472  				"wiki_enabled": true,
   473  				"jobs_enabled": true,
   474  				"snippets_enabled": true,
   475  				"container_registry_enabled": true,
   476  				"service_desk_enabled": true,
   477  				"can_create_merge_request_in": false,
   478  				"issues_access_level": "enabled",
   479  				"repository_access_level": "enabled",
   480  				"merge_requests_access_level": "enabled",
   481  				"forking_access_level": "enabled",
   482  				"wiki_access_level": "enabled",
   483  				"builds_access_level": "enabled",
   484  				"snippets_access_level": "enabled",
   485  				"pages_access_level": "enabled",
   486  				"operations_access_level": "enabled",
   487  				"analytics_access_level": "enabled",
   488  				"container_registry_access_level": "enabled",
   489  				"security_and_compliance_access_level": "private",
   490  				"emails_disabled": null,
   491  				"shared_runners_enabled": true,
   492  				"lfs_enabled": true,
   493  				"creator_id": 2378866,
   494  				"import_status": "none",
   495  				"open_issues_count": 0,
   496  				"ci_default_git_depth": 50,
   497  				"ci_forward_deployment_enabled": true,
   498  				"ci_job_token_scope_enabled": false,
   499  				"public_jobs": true,
   500  				"build_timeout": 3600,
   501  				"auto_cancel_pending_pipelines": "enabled",
   502  				"ci_config_path": "",
   503  				"shared_with_groups": [],
   504  				"only_allow_merge_if_pipeline_succeeds": false,
   505  				"allow_merge_on_skipped_pipeline": null,
   506  				"restrict_user_defined_variables": false,
   507  				"request_access_enabled": true,
   508  				"only_allow_merge_if_all_discussions_are_resolved": false,
   509  				"remove_source_branch_after_merge": true,
   510  				"printing_merge_request_link_enabled": true,
   511  				"merge_method": "merge",
   512  				"squash_option": "default_off",
   513  				"suggestion_commit_message": null,
   514  				"merge_commit_template": null,
   515  				"squash_commit_template": null,
   516  				"auto_devops_enabled": false,
   517  				"auto_devops_deploy_strategy": "continuous",
   518  				"autoclose_referenced_issues": true,
   519  				"keep_latest_artifact": true,
   520  				"runner_token_expiration_interval": null,
   521  				"approvals_before_merge": 0,
   522  				"mirror": false,
   523  				"external_authorization_classification_label": "",
   524  				"marked_for_deletion_at": null,
   525  				"marked_for_deletion_on": null,
   526  				"requirements_enabled": true,
   527  				"requirements_access_level": "enabled",
   528  				"security_and_compliance_enabled": false,
   529  				"compliance_frameworks": [],
   530  				"issues_template": null,
   531  				"merge_requests_template": null,
   532  				"merge_pipelines_enabled": false,
   533  				"merge_trains_enabled": false
   534  			}
   535  			]`)
   536  			if err != nil {
   537  				t.Fail()
   538  			}
   539  		case "/api/v4/groups/test-argocd-proton/projects?include_subgroups=true&per_page=100&topic=&with_shared=true":
   540  			fmt.Println("here")
   541  			_, err := io.WriteString(w, `[{
   542  				"id": 27084533,
   543  				"description": "",
   544  				"name": "argocd",
   545  				"name_with_namespace": "test argocd proton / argocd",
   546  				"path": "argocd",
   547  				"path_with_namespace": "test-argocd-proton/argocd",
   548  				"created_at": "2021-06-01T17:30:44.724Z",
   549  				"default_branch": "master",
   550  				"tag_list": [
   551  					"test-topic"
   552  				],
   553  				"topics": [
   554  					"test-topic"
   555  				],
   556  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git",
   557  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git",
   558  				"web_url": "https://gitlab.com/test-argocd-proton/argocd",
   559  				"readme_url": null,
   560  				"avatar_url": null,
   561  				"forks_count": 0,
   562  				"star_count": 0,
   563  				"last_activity_at": "2021-06-04T08:19:51.656Z",
   564  				"namespace": {
   565  					"id": 12258515,
   566  					"name": "test argocd proton",
   567  					"path": "test-argocd-proton",
   568  					"kind": "gro* Connection #0 to host gitlab.com left intact up ",
   569  					"full_path ": "test - argocd - proton ",
   570  					"parent_id ": null,
   571  					"avatar_url ": null,
   572  					"web_url ": "https: //gitlab.com/groups/test-argocd-proton"
   573  				},
   574  				"container_registry_image_prefix": "registry.gitlab.com/test-argocd-proton/argocd",
   575  				"_links": {
   576  					"self": "https://gitlab.com/api/v4/projects/27084533",
   577  					"issues": "https://gitlab.com/api/v4/projects/27084533/issues",
   578  					"merge_requests": "https://gitlab.com/api/v4/projects/27084533/merge_requests",
   579  					"repo_branches": "https://gitlab.com/api/v4/projects/27084533/repository/branches",
   580  					"labels": "https://gitlab.com/api/v4/projects/27084533/labels",
   581  					"events": "https://gitlab.com/api/v4/projects/27084533/events",
   582  					"members": "https://gitlab.com/api/v4/projects/27084533/members",
   583  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084533/cluster_agents"
   584  				},
   585  				"packages_enabled": true,
   586  				"empty_repo": false,
   587  				"archived": false,
   588  				"visibility": "public",
   589  				"resolve_outdated_diff_discussions": false,
   590  				"container_expiration_policy": {
   591  					"cadence": "1d",
   592  					"enabled": false,
   593  					"keep_n": 10,
   594  					"older_than": "90d",
   595  					"name_regex": ".*",
   596  					"name_regex_keep": null,
   597  					"next_run_at": "2021-06-02T17:30:44.740Z"
   598  				},
   599  				"issues_enabled": true,
   600  				"merge_requests_enabled": true,
   601  				"wiki_enabled": true,
   602  				"jobs_enabled": true,
   603  				"snippets_enabled": true,
   604  				"container_registry_enabled": true,
   605  				"service_desk_enabled": true,
   606  				"can_create_merge_request_in": false,
   607  				"issues_access_level": "enabled",
   608  				"repository_access_level": "enabled",
   609  				"merge_requests_access_level": "enabled",
   610  				"forking_access_level": "enabled",
   611  				"wiki_access_level": "enabled",
   612  				"builds_access_level": "enabled",
   613  				"snippets_access_level": "enabled",
   614  				"pages_access_level": "enabled",
   615  				"operations_access_level": "enabled",
   616  				"analytics_access_level": "enabled",
   617  				"container_registry_access_level": "enabled",
   618  				"security_and_compliance_access_level": "private",
   619  				"emails_disabled": null,
   620  				"shared_runners_enabled": true,
   621  				"lfs_enabled": true,
   622  				"creator_id": 2378866,
   623  				"import_status": "none",
   624  				"open_issues_count": 0,
   625  				"ci_default_git_depth": 50,
   626  				"ci_forward_deployment_enabled": true,
   627  				"ci_job_token_scope_enabled": false,
   628  				"public_jobs": true,
   629  				"build_timeout": 3600,
   630  				"auto_cancel_pending_pipelines": "enabled",
   631  				"ci_config_path": "",
   632  				"shared_with_groups": [],
   633  				"only_allow_merge_if_pipeline_succeeds": false,
   634  				"allow_merge_on_skipped_pipeline": null,
   635  				"restrict_user_defined_variables": false,
   636  				"request_access_enabled": true,
   637  				"only_allow_merge_if_all_discussions_are_resolved": false,
   638  				"remove_source_branch_after_merge": true,
   639  				"printing_merge_request_link_enabled": true,
   640  				"merge_method": "merge",
   641  				"squash_option": "default_off",
   642  				"suggestion_commit_message": null,
   643  				"merge_commit_template": null,
   644  				"squash_commit_template": null,
   645  				"auto_devops_enabled": false,
   646  				"auto_devops_deploy_strategy": "continuous",
   647  				"autoclose_referenced_issues": true,
   648  				"keep_latest_artifact": true,
   649  				"runner_token_expiration_interval": null,
   650  				"approvals_before_merge": 0,
   651  				"mirror": false,
   652  				"external_authorization_classification_label": "",
   653  				"marked_for_deletion_at": null,
   654  				"marked_for_deletion_on": null,
   655  				"requirements_enabled": true,
   656  				"requirements_access_level": "enabled",
   657  				"security_and_compliance_enabled": false,
   658  				"compliance_frameworks": [],
   659  				"issues_template": null,
   660  				"merge_requests_template": null,
   661  				"merge_pipelines_enabled": false,
   662  				"merge_trains_enabled": false
   663  			},
   664  			{
   665  				"id": 27084534,
   666  				"description": "This is a Shared Project",
   667  				"name": "shared-argocd",
   668  				"name_with_namespace": "shared project to test argocd proton / argocd",
   669  				"path": "shared-argocd",
   670  				"path_with_namespace": "test-shared-argocd-proton/shared-argocd",
   671  				"created_at": "2021-06-11T17:30:44.724Z",
   672  				"default_branch": "master",
   673  				"tag_list": [
   674  					"test-topic"
   675  				],
   676  				"topics": [
   677  					"test-topic"
   678  				],
   679  				"ssh_url_to_repo": "git@gitlab.com:test-shared-argocd-proton/shared-argocd.git",
   680  				"http_url_to_repo": "https://gitlab.com/test-shared-argocd-proton/shared-argocd.git",
   681  				"web_url": "https://gitlab.com/test-shared-argocd-proton/shared-argocd",
   682  				"readme_url": null,
   683  				"avatar_url": null,
   684  				"forks_count": 0,
   685  				"star_count": 0,
   686  				"last_activity_at": "2021-06-04T08:19:51.656Z",
   687  				"namespace": {
   688  					"id": 12258518,
   689  					"name": "test shared argocd proton",
   690  					"path": "test-shared-argocd-proton",
   691  					"kind": "group",
   692  					"full_path ": "test-shared-argocd-proton",
   693  					"parent_id ": null,
   694  					"avatar_url ": null,
   695  					"web_url ": "https: //gitlab.com/groups/test-shared-argocd-proton"
   696  				},
   697  				"container_registry_image_prefix": "registry.gitlab.com/test-shared-argocd-proton/shared-argocd",
   698  				"_links": {
   699  					"self": "https://gitlab.com/api/v4/projects/27084534",
   700  					"issues": "https://gitlab.com/api/v4/projects/27084534/issues",
   701  					"merge_requests": "https://gitlab.com/api/v4/projects/27084534/merge_requests",
   702  					"repo_branches": "https://gitlab.com/api/v4/projects/27084534/repository/branches",
   703  					"labels": "https://gitlab.com/api/v4/projects/27084534/labels",
   704  					"events": "https://gitlab.com/api/v4/projects/27084534/events",
   705  					"members": "https://gitlab.com/api/v4/projects/27084534/members",
   706  					"cluster_agents": "https://gitlab.com/api/v4/projects/27084534/cluster_agents"
   707  				},
   708  				"packages_enabled": true,
   709  				"empty_repo": false,
   710  				"archived": false,
   711  				"visibility": "public",
   712  				"resolve_outdated_diff_discussions": false,
   713  				"container_expiration_policy": {
   714  					"cadence": "1d",
   715  					"enabled": false,
   716  					"keep_n": 10,
   717  					"older_than": "90d",
   718  					"name_regex": ".*",
   719  					"name_regex_keep": null,
   720  					"next_run_at": "2021-06-12T17:30:44.740Z"
   721  				},
   722  				"issues_enabled": true,
   723  				"merge_requests_enabled": true,
   724  				"wiki_enabled": true,
   725  				"jobs_enabled": true,
   726  				"snippets_enabled": true,
   727  				"container_registry_enabled": true,
   728  				"service_desk_enabled": true,
   729  				"can_create_merge_request_in": false,
   730  				"issues_access_level": "enabled",
   731  				"repository_access_level": "enabled",
   732  				"merge_requests_access_level": "enabled",
   733  				"forking_access_level": "enabled",
   734  				"wiki_access_level": "enabled",
   735  				"builds_access_level": "enabled",
   736  				"snippets_access_level": "enabled",
   737  				"pages_access_level": "enabled",
   738  				"operations_access_level": "enabled",
   739  				"analytics_access_level": "enabled",
   740  				"container_registry_access_level": "enabled",
   741  				"security_and_compliance_access_level": "private",
   742  				"emails_disabled": null,
   743  				"shared_runners_enabled": true,
   744  				"lfs_enabled": true,
   745  				"creator_id": 2378866,
   746  				"import_status": "none",
   747  				"open_issues_count": 0,
   748  				"ci_default_git_depth": 50,
   749  				"ci_forward_deployment_enabled": true,
   750  				"ci_job_token_scope_enabled": false,
   751  				"public_jobs": true,
   752  				"build_timeout": 3600,
   753  				"auto_cancel_pending_pipelines": "enabled",
   754  				"ci_config_path": "",
   755  				"shared_with_groups": [
   756  					{
   757  						"group_id": 12258515,
   758  						"group_name": "test-argocd-proton",
   759  						"group_full_path": "test-shared-argocd-proton",
   760  						"group_access_level": 30,
   761  						"expires_at": null
   762  					}
   763  				],
   764  				"only_allow_merge_if_pipeline_succeeds": false,
   765  				"allow_merge_on_skipped_pipeline": null,
   766  				"restrict_user_defined_variables": false,
   767  				"request_access_enabled": true,
   768  				"only_allow_merge_if_all_discussions_are_resolved": false,
   769  				"remove_source_branch_after_merge": true,
   770  				"printing_merge_request_link_enabled": true,
   771  				"merge_method": "merge",
   772  				"squash_option": "default_off",
   773  				"suggestion_commit_message": null,
   774  				"merge_commit_template": null,
   775  				"squash_commit_template": null,
   776  				"auto_devops_enabled": false,
   777  				"auto_devops_deploy_strategy": "continuous",
   778  				"autoclose_referenced_issues": true,
   779  				"keep_latest_artifact": true,
   780  				"runner_token_expiration_interval": null,
   781  				"approvals_before_merge": 0,
   782  				"mirror": false,
   783  				"external_authorization_classification_label": "",
   784  				"marked_for_deletion_at": null,
   785  				"marked_for_deletion_on": null,
   786  				"requirements_enabled": true,
   787  				"requirements_access_level": "enabled",
   788  				"security_and_compliance_enabled": false,
   789  				"compliance_frameworks": [],
   790  				"issues_template": null,
   791  				"merge_requests_template": null,
   792  				"merge_pipelines_enabled": false,
   793  				"merge_trains_enabled": false
   794  			}]`)
   795  			if err != nil {
   796  				t.Fail()
   797  			}
   798  		case "/api/v4/projects/27084533/repository/branches/master":
   799  			fmt.Println("returning")
   800  			_, err := io.WriteString(w, `{
   801  				"name": "master",
   802  				"commit": {
   803  					"id": "8898d7999fc99dd0fd578650b58b244fc63f6b53",
   804  					"short_id": "8898d799",
   805  					"created_at": "2021-06-04T08:24:44.000+00:00",
   806  					"parent_ids": ["3c9d50be1ef949ad28674e238c7e12a17b1e9706", "56482e001731640b4123cf177e51c696f08a3005"],
   807  					"title": "Merge branch 'pipeline-1317911429' into 'master'",
   808  					"message": "Merge branch 'pipeline-1317911429' into 'master'\n\n[testapp-ci] manifests/demo/test-app.yaml: release v1.1.0\n\nSee merge request test-argocd-proton/argocd!3",
   809  					"author_name": "Martin Vozník",
   810  					"author_email": "martin@voznik.cz",
   811  					"authored_date": "2021-06-04T08:24:44.000+00:00",
   812  					"committer_name": "Martin Vozník",
   813  					"committer_email": "martin@voznik.cz",
   814  					"committed_date": "2021-06-04T08:24:44.000+00:00",
   815  					"trailers": {},
   816  					"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53"
   817  				},
   818  				"merged": false,
   819  				"protected": true,
   820  				"developers_can_push": false,
   821  				"developers_can_merge": false,
   822  				"can_push": false,
   823  				"default": true,
   824  				"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/master"
   825  			}`)
   826  			if err != nil {
   827  				t.Fail()
   828  			}
   829  		case "/api/v4/projects/27084533/repository/branches?per_page=100":
   830  			_, err := io.WriteString(w, `[{
   831  				"name": "master",
   832  				"commit": {
   833  					"id": "8898d7999fc99dd0fd578650b58b244fc63f6b53",
   834  					"short_id": "8898d799",
   835  					"created_at": "2021-06-04T08:24:44.000+00:00",
   836  					"parent_ids": null,
   837  					"title": "Merge branch 'pipeline-1317911429' into 'master'",
   838  					"message": "Merge branch 'pipeline-1317911429' into 'master'",
   839  					"author_name": "Martin Vozník",
   840  					"author_email": "martin@voznik.cz",
   841  					"authored_date": "2021-06-04T08:24:44.000+00:00",
   842  					"committer_name": "Martin Vozník",
   843  					"committer_email": "martin@voznik.cz",
   844  					"committed_date": "2021-06-04T08:24:44.000+00:00",
   845  					"trailers": null,
   846  					"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53"
   847  				},
   848  				"merged": false,
   849  				"protected": true,
   850  				"developers_can_push": false,
   851  				"developers_can_merge": false,
   852  				"can_push": false,
   853  				"default": true,
   854  				"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/master"
   855  			}, {
   856  				"name": "pipeline-1310077506",
   857  				"commit": {
   858  					"id": "0f92540e5f396ba960adea4ed0aa905baf3f73d1",
   859  					"short_id": "0f92540e",
   860  					"created_at": "2021-06-01T18:39:59.000+00:00",
   861  					"parent_ids": null,
   862  					"title": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   863  					"message": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   864  					"author_name": "ci-test-app",
   865  					"author_email": "mvoznik+cicd@protonmail.com",
   866  					"authored_date": "2021-06-01T18:39:59.000+00:00",
   867  					"committer_name": "ci-test-app",
   868  					"committer_email": "mvoznik+cicd@protonmail.com",
   869  					"committed_date": "2021-06-01T18:39:59.000+00:00",
   870  					"trailers": null,
   871  					"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/commit/0f92540e5f396ba960adea4ed0aa905baf3f73d1"
   872  				},
   873  				"merged": false,
   874  				"protected": false,
   875  				"developers_can_push": false,
   876  				"developers_can_merge": false,
   877  				"can_push": false,
   878  				"default": false,
   879  				"web_url": "https://gitlab.com/test-argocd-proton/argocd/-/tree/pipeline-1310077506"
   880  			}]`)
   881  			if err != nil {
   882  				t.Fail()
   883  			}
   884  		case "/api/v4/projects/27084534/repository/branches?per_page=100":
   885  			_, err := io.WriteString(w, `[{
   886  				"name": "master",
   887  				"commit": {
   888  					"id": "8898d7999fc99dd0fd578650b58b244fc63f6b53",
   889  					"short_id": "8898d799",
   890  					"created_at": "2021-06-04T08:24:44.000+00:00",
   891  					"parent_ids": null,
   892  					"title": "Merge branch 'pipeline-1317911429' into 'master'",
   893  					"message": "Merge branch 'pipeline-1317911429' into 'master'",
   894  					"author_name": "Martin Vozník",
   895  					"author_email": "martin@voznik.cz",
   896  					"authored_date": "2021-06-04T08:24:44.000+00:00",
   897  					"committer_name": "Martin Vozník",
   898  					"committer_email": "martin@voznik.cz",
   899  					"committed_date": "2021-06-04T08:24:44.000+00:00",
   900  					"trailers": null,
   901  					"web_url": "https://gitlab.com/test-shared-argocd-proton/shared-argocd/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53"
   902  				},
   903  				"merged": false,
   904  				"protected": true,
   905  				"developers_can_push": false,
   906  				"developers_can_merge": false,
   907  				"can_push": false,
   908  				"default": true,
   909  				"web_url": "https://gitlab.com/test-shared-argocd-proton/shared-argocd/-/tree/master"
   910  			}, {
   911  				"name": "pipeline-2310077506",
   912  				"commit": {
   913  					"id": "0f92540e5f396ba960adea4ed0aa905baf3f73d1",
   914  					"short_id": "0f92540e",
   915  					"created_at": "2021-06-01T18:39:59.000+00:00",
   916  					"parent_ids": null,
   917  					"title": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   918  					"message": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   919  					"author_name": "ci-test-app",
   920  					"author_email": "mvoznik+cicd@protonmail.com",
   921  					"authored_date": "2021-06-01T18:39:59.000+00:00",
   922  					"committer_name": "ci-test-app",
   923  					"committer_email": "mvoznik+cicd@protonmail.com",
   924  					"committed_date": "2021-06-01T18:39:59.000+00:00",
   925  					"trailers": null,
   926  					"web_url": "https://gitlab.com/test-shared-argocd-proton/shared-argocd/-/commit/0f92540e5f396ba960adea4ed0aa905baf3f73d1"
   927  				},
   928  				"merged": false,
   929  				"protected": false,
   930  				"developers_can_push": false,
   931  				"developers_can_merge": false,
   932  				"can_push": false,
   933  				"default": false,
   934  				"web_url": "https://gitlab.com/test-shared-argocd-proton/shared-argocd/-/tree/pipeline-1310077506"
   935  			}]`)
   936  			if err != nil {
   937  				t.Fail()
   938  			}
   939  		case "/api/v4/projects/27084538/repository/branches?per_page=100":
   940  			_, err := io.WriteString(w, `[{
   941  				"name": "master",
   942  				"commit": {
   943  					"id": "8898d7999fc99dd0fd578650b58b244fc63f6b58",
   944  					"short_id": "8898d801",
   945  					"created_at": "2021-06-04T08:24:44.000+00:00",
   946  					"parent_ids": null,
   947  					"title": "Merge branch 'pipeline-1317911429' into 'master'",
   948  					"message": "Merge branch 'pipeline-1317911429' into 'master'",
   949  					"author_name": "Martin Vozník",
   950  					"author_email": "martin@voznik.cz",
   951  					"authored_date": "2021-06-04T08:24:44.000+00:00",
   952  					"committer_name": "Martin Vozník",
   953  					"committer_email": "martin@voznik.cz",
   954  					"committed_date": "2021-06-04T08:24:44.000+00:00",
   955  					"trailers": null,
   956  					"web_url": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup/-/commit/8898d7999fc99dd0fd578650b58b244fc63f6b53"
   957  				},
   958  				"merged": false,
   959  				"protected": true,
   960  				"developers_can_push": false,
   961  				"developers_can_merge": false,
   962  				"can_push": false,
   963  				"default": true,
   964  				"web_url": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup/-/tree/master"
   965  			}, {
   966  				"name": "pipeline-2310077506",
   967  				"commit": {
   968  					"id": "0f92540e5f396ba960adea4ed0aa905baf3f73d1",
   969  					"short_id": "0f92540e",
   970  					"created_at": "2021-06-01T18:39:59.000+00:00",
   971  					"parent_ids": null,
   972  					"title": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   973  					"message": "[testapp-ci] manifests/demo/test-app.yaml: release v1.0.1",
   974  					"author_name": "ci-test-app",
   975  					"author_email": "mvoznik+cicd@protonmail.com",
   976  					"authored_date": "2021-06-01T18:39:59.000+00:00",
   977  					"committer_name": "ci-test-app",
   978  					"committer_email": "mvoznik+cicd@protonmail.com",
   979  					"committed_date": "2021-06-01T18:39:59.000+00:00",
   980  					"trailers": null,
   981  					"web_url": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup/-/commit/0f92540e5f396ba960adea4ed0aa905baf3f73d1"
   982  				},
   983  				"merged": false,
   984  				"protected": false,
   985  				"developers_can_push": false,
   986  				"developers_can_merge": false,
   987  				"can_push": false,
   988  				"default": false,
   989  				"web_url": "https://gitlab.com/test-argocd-proton/subgroup/argocd-subgroup/-/tree/pipeline-1310077506"
   990  			}]`)
   991  			if err != nil {
   992  				t.Fail()
   993  			}
   994  		case "/api/v4/projects/test-argocd-proton%2Fargocd":
   995  			fmt.Println("auct")
   996  			_, err := io.WriteString(w, `{
   997  				"id": 27084533,
   998  				"description": "",
   999  				"name": "argocd",
  1000  				"name_with_namespace": "test argocd proton / argocd",
  1001  				"path": "argocd",
  1002  				"path_with_namespace": "test-argocd-proton/argocd",
  1003  				"created_at": "2021-06-01T17:30:44.724Z",
  1004  				"default_branch": "master",
  1005  				"tag_list": [
  1006  					"test-topic"
  1007  				],
  1008  				"topics": [
  1009  					"test-topic"
  1010  				],
  1011  				"ssh_url_to_repo": "git@gitlab.com:test-argocd-proton/argocd.git",
  1012  				"http_url_to_repo": "https://gitlab.com/test-argocd-proton/argocd.git",
  1013  				"web_url": "https://gitlab.com/test-argocd-proton/argocd",
  1014  				"readme_url": null,
  1015  				"avatar_url": null,
  1016  				"forks_count": 0,
  1017  				"star_count": 0,
  1018  				"last_activity_at": "2021-06-04T08:19:51.656Z",
  1019  				"namespace": {
  1020  					"id": 12258515,
  1021  					"name": "test argocd proton",
  1022  					"path": "test-argocd-proton",
  1023  					"kind": "group",
  1024  					"full_path": "test-argocd-proton",
  1025  					"parent_id": null,
  1026  					"avatar_url": null,
  1027  					"web_url": "https://gitlab.com/groups/test-argocd-proton"
  1028  				}
  1029  			}`)
  1030  			if err != nil {
  1031  				t.Fail()
  1032  			}
  1033  		case "/api/v4/projects/27084533/repository/tree?path=argocd&ref=master":
  1034  			_, err := io.WriteString(w, `[{"id":"ca14f2a3718159c74572a5325fb4bfb0662a2d3e","name":"ingress.yaml","type":"blob","path":"argocd/ingress.yaml","mode":"100644"},{"id":"de2a53a73b1550b3e0f4d37ea0a6d878bf9c5096","name":"install.yaml","type":"blob","path":"argocd/install.yaml","mode":"100644"}]`)
  1035  			if err != nil {
  1036  				t.Fail()
  1037  			}
  1038  		case "/api/v4/projects/27084533/repository/tree?path=.&ref=master":
  1039  			_, err := io.WriteString(w, `[{"id":"f2bf99fa8f7a27df9c43d2dffc8c8cd747f3181a","name":"argocd","type":"tree","path":"argocd","mode":"040000"},{"id":"68a3125232e01c1583a6a6299534ce10c5e7dd83","name":"manifests","type":"tree","path":"manifests","mode":"040000"}]`)
  1040  			if err != nil {
  1041  				t.Fail()
  1042  			}
  1043  			// Recent versions of the Gitlab API (v17.7+) listTree return 404 not only when a file doesn't exist, but also
  1044  			// when a path is to a file instead of a directory. Code was refactored to explicitly search for file then
  1045  			// search for directory, catching 404 errors as "file not found".
  1046  		case "/api/v4/projects/27084533/repository/files/argocd?ref=master":
  1047  			w.WriteHeader(http.StatusNotFound)
  1048  		case "/api/v4/projects/27084533/repository/files/argocd%2Finstall%2Eyaml?ref=master":
  1049  			_, err := io.WriteString(w, `{"file_name":"install.yaml","file_path":"argocd/install.yaml","size":0,"encoding":"base64","content_sha256":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","ref":"main","blob_id":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391","commit_id":"6d4c0f9d34534ccc73aa3f3180b25e2aebe630eb","last_commit_id":"b50eb63f9c0e09bfdb070db26fd32c7210291f52","execute_filemode":false,"content":""}`)
  1050  			if err != nil {
  1051  				t.Fail()
  1052  			}
  1053  		case "/api/v4/projects/27084533/repository/files/notathing?ref=master":
  1054  			w.WriteHeader(http.StatusNotFound)
  1055  		case "/api/v4/projects/27084533/repository/tree?path=notathing&ref=master":
  1056  			w.WriteHeader(http.StatusNotFound)
  1057  		case "/api/v4/projects/27084533/repository/files/argocd%2Fnotathing%2Eyaml?ref=master":
  1058  			w.WriteHeader(http.StatusNotFound)
  1059  		case "/api/v4/projects/27084533/repository/tree?path=argocd%2Fnotathing.yaml&ref=master":
  1060  			w.WriteHeader(http.StatusNotFound)
  1061  		case "/api/v4/projects/27084533/repository/files/notathing%2Fnotathing%2Eyaml?ref=master":
  1062  			w.WriteHeader(http.StatusNotFound)
  1063  		case "/api/v4/projects/27084533/repository/tree?path=notathing%2Fnotathing.yaml&ref=master":
  1064  			w.WriteHeader(http.StatusNotFound)
  1065  		case "/api/v4/projects/27084533/repository/files/notathing%2Fnotathing%2Fnotathing%2Eyaml?ref=master":
  1066  			w.WriteHeader(http.StatusNotFound)
  1067  		case "/api/v4/projects/27084533/repository/tree?path=notathing%2Fnotathing%2Fnotathing.yaml&ref=master":
  1068  			w.WriteHeader(http.StatusNotFound)
  1069  		case "/api/v4/projects/27084533/repository/branches/foo":
  1070  			w.WriteHeader(http.StatusNotFound)
  1071  		default:
  1072  			_, err := io.WriteString(w, `[]`)
  1073  			if err != nil {
  1074  				t.Fail()
  1075  			}
  1076  		}
  1077  	}
  1078  }
  1079  
  1080  func TestGitlabListRepos(t *testing.T) {
  1081  	cases := []struct {
  1082  		name, proto, url, topic                                                  string
  1083  		hasError, allBranches, includeSubgroups, includeSharedProjects, insecure bool
  1084  		branches                                                                 []string
  1085  		filters                                                                  []v1alpha1.SCMProviderGeneratorFilter
  1086  	}{
  1087  		{
  1088  			name:     "blank protocol",
  1089  			url:      "git@gitlab.com:test-argocd-proton/argocd.git",
  1090  			branches: []string{"master"},
  1091  		},
  1092  		{
  1093  			name:  "ssh protocol",
  1094  			proto: "ssh",
  1095  			url:   "git@gitlab.com:test-argocd-proton/argocd.git",
  1096  		},
  1097  		{
  1098  			name:  "labelmatch",
  1099  			proto: "ssh",
  1100  			url:   "git@gitlab.com:test-argocd-proton/argocd.git",
  1101  			filters: []v1alpha1.SCMProviderGeneratorFilter{
  1102  				{
  1103  					LabelMatch: strp("test-topic"),
  1104  				},
  1105  			},
  1106  		},
  1107  		{
  1108  			name:  "https protocol",
  1109  			proto: "https",
  1110  			url:   "https://gitlab.com/test-argocd-proton/argocd.git",
  1111  		},
  1112  		{
  1113  			name:     "other protocol",
  1114  			proto:    "other",
  1115  			hasError: true,
  1116  		},
  1117  		{
  1118  			name:        "all branches",
  1119  			allBranches: true,
  1120  			url:         "git@gitlab.com:test-argocd-proton/argocd.git",
  1121  			branches:    []string{"master"},
  1122  		},
  1123  		{
  1124  			name:                  "all subgroups",
  1125  			allBranches:           true,
  1126  			url:                   "git@gitlab.com:test-argocd-proton/argocd.git",
  1127  			branches:              []string{"master"},
  1128  			includeSharedProjects: false,
  1129  			includeSubgroups:      true,
  1130  		},
  1131  		{
  1132  			name:                  "all subgroups and shared projects",
  1133  			allBranches:           true,
  1134  			url:                   "git@gitlab.com:test-argocd-proton/argocd.git",
  1135  			branches:              []string{"master"},
  1136  			includeSharedProjects: true,
  1137  			includeSubgroups:      true,
  1138  		},
  1139  		{
  1140  			name:             "specific topic",
  1141  			allBranches:      true,
  1142  			url:              "git@gitlab.com:test-argocd-proton/argocd.git",
  1143  			branches:         []string{"master"},
  1144  			includeSubgroups: false,
  1145  			topic:            "specific-topic",
  1146  		},
  1147  	}
  1148  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1149  		gitlabMockHandler(t)(w, r)
  1150  	}))
  1151  	for _, c := range cases {
  1152  		t.Run(c.name, func(t *testing.T) {
  1153  			provider, _ := NewGitlabProvider("test-argocd-proton", "", ts.URL, c.allBranches, c.includeSubgroups, c.includeSharedProjects, c.insecure, "", c.topic, nil)
  1154  			rawRepos, err := ListRepos(t.Context(), provider, c.filters, c.proto)
  1155  			if c.hasError {
  1156  				require.Error(t, err)
  1157  			} else {
  1158  				require.NoError(t, err)
  1159  				// Just check that this one project shows up. Not a great test but better than nothing?
  1160  				repos := []*Repository{}
  1161  				uniqueRepos := map[string]int{}
  1162  				branches := []string{}
  1163  				for _, r := range rawRepos {
  1164  					if r.Repository == "argocd" {
  1165  						repos = append(repos, r)
  1166  						branches = append(branches, r.Branch)
  1167  					}
  1168  					uniqueRepos[r.Repository]++
  1169  				}
  1170  				assert.NotEmpty(t, repos)
  1171  				assert.Equal(t, c.url, repos[0].URL)
  1172  				for _, b := range c.branches {
  1173  					assert.Contains(t, branches, b)
  1174  				}
  1175  				// In case of listing subgroups, validate the number of returned projects
  1176  				if c.includeSubgroups || c.includeSharedProjects {
  1177  					assert.Len(t, uniqueRepos, 2)
  1178  				}
  1179  				// In case we filter on the topic, ensure we got only one repo returned
  1180  				if c.topic != "" {
  1181  					assert.Len(t, uniqueRepos, 1)
  1182  				}
  1183  			}
  1184  		})
  1185  	}
  1186  }
  1187  
  1188  func TestGitlabHasPath(t *testing.T) {
  1189  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1190  		gitlabMockHandler(t)(w, r)
  1191  	}))
  1192  	host, _ := NewGitlabProvider("test-argocd-proton", "", ts.URL, false, true, true, false, "", "", nil)
  1193  	repo := &Repository{
  1194  		Organization: "test-argocd-proton",
  1195  		Repository:   "argocd",
  1196  		Branch:       "master",
  1197  	}
  1198  
  1199  	cases := []struct {
  1200  		name, path string
  1201  		exists     bool
  1202  	}{
  1203  		{
  1204  			name:   "directory exists",
  1205  			path:   "argocd",
  1206  			exists: true,
  1207  		},
  1208  		{
  1209  			name:   "file exists",
  1210  			path:   "argocd/install.yaml",
  1211  			exists: true,
  1212  		},
  1213  		{
  1214  			name:   "directory does not exist",
  1215  			path:   "notathing",
  1216  			exists: false,
  1217  		},
  1218  		{
  1219  			name:   "file does not exist",
  1220  			path:   "argocd/notathing.yaml",
  1221  			exists: false,
  1222  		},
  1223  		{
  1224  			name:   "noexistent file in noexistent directory",
  1225  			path:   "notathing/notathing.yaml",
  1226  			exists: false,
  1227  		},
  1228  		{
  1229  			name:   "noexistent file in nested noexistent directory",
  1230  			path:   "notathing/notathing/notathing.yaml",
  1231  			exists: false,
  1232  		},
  1233  	}
  1234  
  1235  	for _, c := range cases {
  1236  		t.Run(c.name, func(t *testing.T) {
  1237  			ok, err := host.RepoHasPath(t.Context(), repo, c.path)
  1238  			require.NoError(t, err)
  1239  			assert.Equal(t, c.exists, ok)
  1240  		})
  1241  	}
  1242  }
  1243  
  1244  func TestGitlabGetBranches(t *testing.T) {
  1245  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1246  		gitlabMockHandler(t)(w, r)
  1247  	}))
  1248  	host, _ := NewGitlabProvider("test-argocd-proton", "", ts.URL, false, true, true, false, "", "", nil)
  1249  
  1250  	repo := &Repository{
  1251  		RepositoryId: 27084533,
  1252  		Branch:       "master",
  1253  	}
  1254  	t.Run("branch exists", func(t *testing.T) {
  1255  		repos, err := host.GetBranches(t.Context(), repo)
  1256  		require.NoError(t, err)
  1257  		assert.Equal(t, "master", repos[0].Branch)
  1258  	})
  1259  
  1260  	repo2 := &Repository{
  1261  		RepositoryId: 27084533,
  1262  		Branch:       "foo",
  1263  	}
  1264  	t.Run("unknown branch", func(t *testing.T) {
  1265  		_, err := host.GetBranches(t.Context(), repo2)
  1266  		require.NoError(t, err)
  1267  	})
  1268  }
  1269  
  1270  func TestGetBranchesTLS(t *testing.T) {
  1271  	tests := []struct {
  1272  		name        string
  1273  		tlsInsecure bool
  1274  		passCerts   bool
  1275  		requireErr  bool
  1276  	}{
  1277  		{
  1278  			name:        "TLS Insecure: true, No Certs",
  1279  			tlsInsecure: true,
  1280  			passCerts:   false,
  1281  			requireErr:  false,
  1282  		},
  1283  		{
  1284  			name:        "TLS Insecure: true, With Certs",
  1285  			tlsInsecure: true,
  1286  			passCerts:   true,
  1287  			requireErr:  false,
  1288  		},
  1289  		{
  1290  			name:        "TLS Insecure: false, With Certs",
  1291  			tlsInsecure: false,
  1292  			passCerts:   true,
  1293  			requireErr:  false,
  1294  		},
  1295  		{
  1296  			name:        "TLS Insecure: false, No Certs",
  1297  			tlsInsecure: false,
  1298  			passCerts:   false,
  1299  			requireErr:  true,
  1300  		},
  1301  	}
  1302  
  1303  	for _, test := range tests {
  1304  		test := test
  1305  		t.Run(test.name, func(t *testing.T) {
  1306  			ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  1307  				gitlabMockHandler(t)(w, r)
  1308  			}))
  1309  			defer ts.Close()
  1310  
  1311  			var certs []byte
  1312  			if test.passCerts {
  1313  				for _, cert := range ts.TLS.Certificates {
  1314  					for _, c := range cert.Certificate {
  1315  						parsedCert, err := x509.ParseCertificate(c)
  1316  						require.NoError(t, err, "Failed to parse certificate")
  1317  						certs = append(certs, pem.EncodeToMemory(&pem.Block{
  1318  							Type:  "CERTIFICATE",
  1319  							Bytes: parsedCert.Raw,
  1320  						})...)
  1321  					}
  1322  				}
  1323  			}
  1324  
  1325  			host, err := NewGitlabProvider("test-argocd-proton", "", ts.URL, false, true, true, test.tlsInsecure, "", "", certs)
  1326  			require.NoError(t, err)
  1327  			repo := &Repository{
  1328  				RepositoryId: 27084533,
  1329  				Branch:       "master",
  1330  			}
  1331  			_, err = host.GetBranches(t.Context(), repo)
  1332  			if test.requireErr {
  1333  				require.Error(t, err)
  1334  			} else {
  1335  				require.NoError(t, err)
  1336  			}
  1337  		})
  1338  	}
  1339  }