@@ -4,132 +4,122 @@ package test
44
55import (
66 "context"
7- "fmt"
87 "net/http"
9- "path/filepath"
108 "testing"
119
1210 "github.com/google/go-github/v81/github"
13- "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1411 tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
1512 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
1613 "github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
1714 twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
1815 "github.com/tektoncd/pipeline/pkg/names"
1916 "gotest.tools/v3/assert"
20- corev1 "k8s.io/api/core/v1"
21- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2217)
2318
2419func TestGithubGitOpsCommentOnTag (t * testing.T ) {
2520 ctx := context .Background ()
2621 ctx , runcnx , opts , ghcnx , err := tgithub .Setup (ctx , false , false )
2722 assert .NilError (t , err )
28- tagName := "v1.0.0"
29- comment := "/test tag:" + tagName
30- sha := ""
31- targetBranch := "release-" + tagName
32- label := "Testing GitHub commit comment on tag with GitHub apps integration"
23+ var g * tgithub.PRTest
3324 targetNS := names .SimpleNameGenerator .RestrictLengthWithRandomSuffix ("pac-e2e-tag" )
34- numberOfPRs := 1
3525 repoinfo , resp , err := ghcnx .Client ().Repositories .Get (ctx , opts .Organization , opts .Repo )
3626 assert .NilError (t , err )
3727 if resp != nil && resp .StatusCode == http .StatusNotFound {
38- t .Errorf ("Repository %s not found in %s" , opts .Organization , opts .Repo )
28+ t .Errorf ("Repository %s not found in %s" , opts .Repo , opts .Organization )
3929 }
4030 err = tgithub .CreateCRD (ctx , t , repoinfo , runcnx , opts , targetNS )
4131 assert .NilError (t , err )
42-
4332 runcnx .Clients .Log .Infof ("Repository %s has been created successfully" , targetNS )
4433
45- ref , resp , err := ghcnx .Client ().Git .GetRef (ctx , opts .Organization , opts .Repo , "refs/tags/" + tagName )
46- if err != nil && resp .StatusCode == http .StatusNotFound {
47- runcnx .Clients .Log .Infof ("Tag %s is not found on the repository" , tagName )
48- runcnx .Clients .Log .Infof ("Creating tag %s on the repository" , tagName )
49- yamlFiles := []string {"testdata/pipelinerun-on-tag.yaml" }
50- yamlEntries := map [string ]string {}
51- for _ , v := range yamlFiles {
52- yamlEntries [filepath .Join (".tekton" , filepath .Base (v ))] = v
34+ tags := []string {"v1.0.0" , "v1.0.0-lightweight" }
35+ for _ , tag := range tags {
36+ comment := "/test tag:" + tag
37+ sha := ""
38+ targetRefName := "release-" + tag
39+ label := ""
40+ if tag == "v1.0.0" {
41+ label = "Testing GitHub commit comment on tag with GitHub apps integration"
42+ } else {
43+ label = "Testing GitHub commit comment on lightweight tag with GitHub apps integration"
5344 }
5445
55- entries , err := payload .GetEntries (yamlEntries ,
56- targetNS , "refs/tags/*" , "push" , map [string ]string {})
57- assert .NilError (t , err )
58-
59- targetRefName := targetBranch
60- cloneURL , err := scm .MakeGitCloneURL (repoinfo .GetCloneURL (), "git" , * ghcnx .Token )
61- assert .NilError (t , err )
62- scmOpts := scm.Opts {
63- GitURL : cloneURL ,
64- TargetRefName : targetRefName ,
65- BaseRefName : repoinfo .GetDefaultBranch (),
66- WebURL : repoinfo .GetHTMLURL (),
67- Log : runcnx .Clients .Log ,
68- CommitTitle : label ,
46+ numberOfPRs := 1
47+
48+ ref , resp , err := ghcnx .Client ().Git .GetRef (ctx , opts .Organization , opts .Repo , "refs/tags/" + tag )
49+ if err != nil && resp .StatusCode == http .StatusNotFound {
50+ runcnx .Clients .Log .Infof ("Tag %s is not found on the repository" , tag )
51+ runcnx .Clients .Log .Infof ("Creating tag %s on the repository" , tag )
52+ entries , err := payload .GetEntries (map [string ]string {".tekton/pipelinerun-on-tag.yaml" : "testdata/pipelinerun-on-tag.yaml" },
53+ targetNS , "refs/tags/*" , "push" , map [string ]string {})
54+ assert .NilError (t , err )
55+
56+ cloneURL , err := scm .MakeGitCloneURL (repoinfo .GetCloneURL (), "git" , * ghcnx .Token )
57+ assert .NilError (t , err )
58+ scmOpts := scm.Opts {
59+ GitURL : cloneURL ,
60+ TargetRefName : targetRefName ,
61+ BaseRefName : repoinfo .GetDefaultBranch (),
62+ WebURL : repoinfo .GetHTMLURL (),
63+ Log : runcnx .Clients .Log ,
64+ CommitTitle : label ,
65+ }
66+ scm .PushFilesToRefGit (t , & scmOpts , entries )
67+ branch , _ , err := ghcnx .Client ().Repositories .GetBranch (ctx , opts .Organization , opts .Repo , targetRefName , 1 )
68+ assert .NilError (t , err )
69+
70+ sha = branch .GetCommit ().GetSHA ()
71+
72+ // when we're testing a lightweight tag, we need to set the opts.LightweightTag to true
73+ // so that we don't create an annotated tag. on creating lightweight tag, ref contains the commit SHA directly.
74+ opts .LightweightTag = tag == "v1.0.0-lightweight"
75+ runcnx .Clients .Log .Infof ("is Lightweight tag: %t" , opts .LightweightTag )
76+ _ , err = tgithub .CreateTag (ctx , t , runcnx , ghcnx , opts , sha , tag )
77+ assert .NilError (t , err )
78+ numberOfPRs ++
79+ } else {
80+ // else if tag is already created, we need to get the tag object to get the commit SHA
81+ if tag == "v1.0.0" {
82+ // if tag is annotated tag, we need to get the tag object to get the commit SHA
83+ runcnx .Clients .Log .Infof ("Tag %s is already created on the repository" , tag )
84+ runcnx .Clients .Log .Infof ("Getting tag %s" , tag )
85+ tagRef , _ , err := ghcnx .Client ().Git .GetTag (ctx , opts .Organization , opts .Repo , ref .GetObject ().GetSHA ())
86+ assert .NilError (t , err )
87+ sha = tagRef .GetObject ().GetSHA ()
88+ } else {
89+ // else if tag is lightweight tag, we need to get the commit SHA from the ref
90+ runcnx .Clients .Log .Infof ("Lightweight tag %s is already created on the repository" , tag )
91+ sha = ref .GetObject ().GetSHA ()
92+ }
6993 }
70- scm .PushFilesToRefGit (t , & scmOpts , entries )
71- branch , _ , err := ghcnx .Client ().Repositories .GetBranch (ctx , opts .Organization , opts .Repo , targetBranch , 1 )
72- assert .NilError (t , err )
7394
74- sha = branch .GetCommit ().GetSHA ()
95+ g = & tgithub.PRTest {
96+ Label : label ,
97+ Cnx : runcnx ,
98+ Logger : runcnx .Clients .Log ,
99+ Options : opts ,
100+ Provider : ghcnx ,
101+ TargetNamespace : targetNS ,
102+ PRNumber : - 1 ,
103+ SHA : sha ,
104+ }
105+ defer g .TearDown (ctx , t )
75106
76- _ , err = tgithub .CreateTag (ctx , t , runcnx , ghcnx , opts , sha , tagName )
77- assert .NilError (t , err )
78- numberOfPRs ++
79- } else {
80- runcnx .Clients .Log .Infof ("Tag %s is already created on the repository" , tagName )
81- runcnx .Clients .Log .Infof ("Getting tag %s" , tagName )
82- tagRef , _ , err := ghcnx .Client ().Git .GetTag (ctx , opts .Organization , opts .Repo , ref .GetObject ().GetSHA ())
107+ runcnx .Clients .Log .Infof ("%s on tag commit" , comment )
108+ _ , _ , err = ghcnx .Client ().Repositories .CreateComment (ctx ,
109+ opts .Organization ,
110+ opts .Repo , sha , // this is the commit sha of the tag v1.0.0
111+ & github.RepositoryComment {Body : github .Ptr (comment )})
83112 assert .NilError (t , err )
84- sha = tagRef .GetObject ().GetSHA ()
85- }
86-
87- g := & tgithub.PRTest {
88- Label : label ,
89- Cnx : runcnx ,
90- Logger : runcnx .Clients .Log ,
91- Options : opts ,
92- Provider : ghcnx ,
93- TargetNamespace : targetNS ,
94- PRNumber : - 1 ,
95- SHA : sha ,
96- }
97- defer g .TearDown (ctx , t )
98113
99- runcnx .Clients .Log .Infof ("%s on tag commit" , comment )
100- _ , _ , err = ghcnx .Client ().Repositories .CreateComment (ctx ,
101- opts .Organization ,
102- opts .Repo , sha , // this is the commit sha of the tag v1.0.0
103- & github.RepositoryComment {Body : github .Ptr (comment )})
104- assert .NilError (t , err )
105-
106- waitOpts := twait.Opts {
107- RepoName : targetNS ,
108- Namespace : targetNS ,
109- MinNumberStatus : numberOfPRs ,
110- PollTimeout : twait .DefaultTimeout ,
111- TargetSHA : sha , // this is the commit sha of the tag v1.0.0
112- }
113- runcnx .Clients .Log .Info ("Waiting for Repository to be updated" )
114- _ , err = twait .UntilRepositoryUpdated (ctx , runcnx .Clients , waitOpts )
115- assert .NilError (t , err )
116-
117- runcnx .Clients .Log .Infof ("Check if we have the repository set as succeeded" )
118- repo , err := runcnx .Clients .PipelineAsCode .PipelinesascodeV1alpha1 ().Repositories (targetNS ).Get (ctx , targetNS , metav1.GetOptions {})
119- assert .NilError (t , err )
120- assert .Equal (t , repo .Status [len (repo .Status )- 1 ].Conditions [0 ].Status , corev1 .ConditionTrue )
121-
122- pruns , err := runcnx .Clients .Tekton .TektonV1 ().PipelineRuns (targetNS ).List (ctx , metav1.ListOptions {
123- LabelSelector : fmt .Sprintf ("%s=%s" , keys .SHA , sha ),
124- })
125- assert .NilError (t , err )
126- assert .Equal (t , len (pruns .Items ), numberOfPRs )
127-
128- for i := range pruns .Items {
129- sData , err := runcnx .Clients .Kube .CoreV1 ().Secrets (targetNS ).Get (ctx , pruns .Items [i ].GetAnnotations ()[keys .GitAuthSecret ], metav1.GetOptions {})
114+ waitOpts := twait.Opts {
115+ RepoName : targetNS ,
116+ Namespace : targetNS ,
117+ MinNumberStatus : numberOfPRs ,
118+ PollTimeout : twait .DefaultTimeout ,
119+ TargetSHA : sha , // this is the commit sha of the tag v1.0.0
120+ }
121+ runcnx .Clients .Log .Info ("Waiting for PipelineRun to be created" )
122+ err = twait .UntilPipelineRunCreated (ctx , runcnx .Clients , waitOpts )
130123 assert .NilError (t , err )
131- assert .Assert (t , string (sData .Data ["git-provider-token" ]) != "" )
132- assert .Assert (t , string (sData .Data [".git-credentials" ]) != "" )
133- assert .Assert (t , string (sData .Data [".gitconfig" ]) != "" )
134124 }
135125}
0 commit comments