@@ -113,107 +113,119 @@ func Run(o *options.Options) error {
113113 defaultBranchSHA := defaultBranch .Commit .SHA
114114
115115 // Skip if scorecard file already exists in workflows folder.
116- for _ , f := range workflowFiles {
116+ workflowExists := false
117+ for i , f := range workflowFiles {
118+ log .Printf (
119+ "checking for scorecard workflow file (%s)" ,
120+ f ,
121+ )
117122 scoreFileContent , _ , _ , err := gh .GetContents (
118123 ctx ,
119124 o .Owner ,
120125 repoName ,
121126 f ,
122127 & github.RepositoryContentGetOptions {},
123128 )
124- if scoreFileContent != nil || err == nil {
129+ if scoreFileContent != nil {
125130 log .Printf (
126- "skipped repo (%s) since scorecard workflow already exists" ,
131+ "skipping repo (%s) since scorecard workflow already exists: %s " ,
127132 repoName ,
133+ f ,
128134 )
129135
130- continue
136+ workflowExists = true
137+ break
138+ }
139+ if err != nil && i == len (workflowFiles )- 1 {
140+ return fmt .Errorf ("checking for scorecard workflow file: %w" , err )
131141 }
132142 }
133143
134- // Skip if branch scorecard already exists.
135- scorecardBranch , _ , err := gh .GetBranch (
136- ctx ,
137- o .Owner ,
138- repoName ,
139- "scorecard" ,
140- true ,
141- )
142- if scorecardBranch != nil || err == nil {
143- log .Printf (
144- "skipped repo (%s) since the scorecard branch already exists" ,
144+ if ! workflowExists {
145+ // Skip if branch scorecard already exists.
146+ scorecardBranch , _ , err := gh .GetBranch (
147+ ctx ,
148+ o .Owner ,
145149 repoName ,
150+ "scorecard" ,
151+ true ,
146152 )
153+ if scorecardBranch != nil || err == nil {
154+ log .Printf (
155+ "skipped repo (%s) since the scorecard branch already exists" ,
156+ repoName ,
157+ )
147158
148- continue
149- }
159+ continue
160+ }
150161
151- // Create new branch using a reference that stores the new commit hash.
152- // TODO: Capture ref creation errors
153- ref := & github.Reference {
154- Ref : github .String ("refs/heads/scorecard" ),
155- Object : & github.GitObject {SHA : defaultBranchSHA },
156- }
157- _ , _ , err = gh .CreateGitRef (ctx , o .Owner , repoName , ref )
158- if err != nil {
159- log .Printf (
160- "skipped repo (%s) because new branch could not be created: %+v" ,
162+ // Create new branch using a reference that stores the new commit hash.
163+ // TODO: Capture ref creation errors
164+ ref := & github.Reference {
165+ Ref : github .String ("refs/heads/scorecard" ),
166+ Object : & github.GitObject {SHA : defaultBranchSHA },
167+ }
168+ _ , _ , err = gh .CreateGitRef (ctx , o .Owner , repoName , ref )
169+ if err != nil {
170+ log .Printf (
171+ "skipped repo (%s) because new branch could not be created: %+v" ,
172+ repoName ,
173+ err ,
174+ )
175+
176+ continue
177+ }
178+
179+ // Create file in repository.
180+ // TODO: Capture file creation errors
181+ opts := & github.RepositoryContentFileOptions {
182+ Message : github .String ("Adding scorecard workflow" ),
183+ Content : workflowContent ,
184+ Branch : github .String ("scorecard" ),
185+ }
186+ _ , _ , err = gh .CreateFile (
187+ ctx ,
188+ o .Owner ,
161189 repoName ,
162- err ,
190+ workflowFile ,
191+ opts ,
163192 )
193+ if err != nil {
194+ log .Printf (
195+ "skipped repo (%s) because new file could not be created: %+v" ,
196+ repoName ,
197+ err ,
198+ )
164199
165- continue
166- }
200+ continue
201+ }
167202
168- // Create file in repository.
169- // TODO: Capture file creation errors
170- opts := & github.RepositoryContentFileOptions {
171- Message : github .String ("Adding scorecard workflow" ),
172- Content : workflowContent ,
173- Branch : github .String ("scorecard" ),
174- }
175- _ , _ , err = gh .CreateFile (
176- ctx ,
177- o .Owner ,
178- repoName ,
179- workflowFile ,
180- opts ,
181- )
182- if err != nil {
183- log .Printf (
184- "skipped repo (%s) because new file could not be created: %+v" ,
203+ // Create pull request.
204+ // TODO: Capture pull request creation errors
205+ _ , err = gh .CreatePullRequest (
206+ ctx ,
207+ o .Owner ,
185208 repoName ,
186- err ,
209+ * defaultBranch .Name ,
210+ "scorecard" ,
211+ "Added Scorecard Workflow" ,
212+ "Added the workflow for OpenSSF's Security Scorecard" ,
187213 )
214+ if err != nil {
215+ log .Printf (
216+ "skipped repo (%s) because pull request could not be created: %+v" ,
217+ repoName ,
218+ err ,
219+ )
188220
189- continue
190- }
221+ continue
222+ }
191223
192- // Create pull request.
193- // TODO: Capture pull request creation errors
194- _ , err = gh .CreatePullRequest (
195- ctx ,
196- o .Owner ,
197- repoName ,
198- * defaultBranch .Name ,
199- "scorecard" ,
200- "Added Scorecard Workflow" ,
201- "Added the workflow for OpenSSF's Security Scorecard" ,
202- )
203- if err != nil {
204224 log .Printf (
205- "skipped repo (%s) because pull request could not be created: %+v " ,
225+ "Created a pull request to add the scorecard workflow to %s " ,
206226 repoName ,
207- err ,
208227 )
209-
210- continue
211228 }
212-
213- log .Printf (
214- "Created a pull request to add the scorecard workflow to %s" ,
215- repoName ,
216- )
217229 }
218230
219231 return nil
0 commit comments