@@ -73,17 +73,17 @@ func (c *Command) HandleEvent(payload interface{}) error {
7373 return err
7474 }
7575 target := & Target {
76- Owner : e .Repo .Owner .GetLogin (),
77- Repo : e .Repo .GetName (),
76+ Owner : e .Repo .Owner .GetLogin (),
77+ Repo : e .Repo .GetName (),
7878 PullRequest : pull ,
7979 }
8080 return c .EnsureCheckRun (target )
8181 case * github.PullRequestEvent :
8282 owner := e .Repo .Owner .GetLogin ()
8383 repo := e .Repo .GetName ()
8484 target := & Target {
85- Owner : owner ,
86- Repo : repo ,
85+ Owner : owner ,
86+ Repo : repo ,
8787 PullRequest : e .PullRequest ,
8888 }
8989 return c .EnsureCheckRun (target )
@@ -119,19 +119,62 @@ func (c *Command) createCheckRun(suite *github.CheckSuite, cr Run) (*github.Chec
119119 return created , err
120120}
121121
122+ func (c * Command ) CreateAndLogStatus (client * github.Client , owner , repo , sha string , status * github.RepoStatus ) error {
123+ desc := status .GetDescription ()
124+ if len (desc ) > 140 {
125+ // Otherwise you get errors like:
126+ // 2019/10/17 18:25:08 Failed creating status: POST https://api.github.com/repos/variantdev/go-actions/statuses/ceb4320db3c54081d55daa6d7a50ed8dc7fafc86: 422 Validation Failed [{Resource:Status Field:description Code:custom Message:description is too long (maximum is 140 characters)}]
127+ desc = desc [0 :140 ]
128+
129+ status .Description = & desc
130+ }
131+
132+ repoStatus , _ , err := client .Repositories .CreateStatus (context .Background (), owner , repo , sha , status )
133+ if err != nil {
134+ log .Printf ("Failed creating status: %v" , err )
135+ } else {
136+ buf := bytes.Buffer {}
137+ enc := json .NewEncoder (& buf )
138+ enc .SetIndent ("" , " " )
139+ if err := enc .Encode (repoStatus ); err != nil {
140+ return err
141+ }
142+ log .Printf ("Created repo status:\n %s" , buf .String ())
143+ }
144+
145+ return nil
146+ }
147+
122148func (c * Command ) EnsureCheckRun (pre * Target ) error {
123149 client , err := c .instTokenClient ()
124150 if err != nil {
125151 return err
126152 }
127153
154+ owner := pre .Owner
155+ repo := pre .Repo
156+ sha := pre .PullRequest .Head .GetSHA ()
157+
158+ if c .statusContext != "" {
159+ status := & github.RepoStatus {
160+ State : github .String ("pending" ),
161+ Context : github .String (c .statusContext ),
162+ Description : github .String (c .statusDescription ),
163+ }
164+
165+ if c .statusTargetURL != "" {
166+ status .TargetURL = github .String (c .statusTargetURL )
167+ }
168+
169+ if err := c .CreateAndLogStatus (client , owner , repo , sha , status ); err != nil {
170+ return err
171+ }
172+ }
173+
128174 log .Printf ("Running command: %q" , c .cmd )
129175
130176 summary , text , runErr := c .runIt ()
131177
132- owner := pre .Owner
133- repo := pre .Repo
134-
135178 if c .checkRunName != "" {
136179 suite , err := c .EnsureCheckSuite (pre )
137180 if err != nil {
@@ -176,7 +219,6 @@ func (c *Command) EnsureCheckRun(pre *Target) error {
176219 }
177220
178221 if c .statusContext != "" {
179- sha := pre .PullRequest .Head .GetSHA ()
180222 var state string
181223 if runErr != nil {
182224 state = "failure"
@@ -192,12 +234,6 @@ func (c *Command) EnsureCheckRun(pre *Target) error {
192234 desc = summary
193235 }
194236
195- if len (desc ) > 140 {
196- // Otherwise you get errors like:
197- // 2019/10/17 18:25:08 Failed creating status: POST https://api.github.com/repos/variantdev/go-actions/statuses/ceb4320db3c54081d55daa6d7a50ed8dc7fafc86: 422 Validation Failed [{Resource:Status Field:description Code:custom Message:description is too long (maximum is 140 characters)}]
198- desc = desc [0 :140 ]
199- }
200-
201237 status := & github.RepoStatus {
202238 State : github .String (state ),
203239 Context : github .String (c .statusContext ),
@@ -208,17 +244,8 @@ func (c *Command) EnsureCheckRun(pre *Target) error {
208244 status .TargetURL = github .String (c .statusTargetURL )
209245 }
210246
211- repoStatus , _ , err := client .Repositories .CreateStatus (context .Background (), owner , repo , sha , status )
212- if err != nil {
213- log .Printf ("Failed creating status: %v" , err )
214- } else {
215- buf := bytes.Buffer {}
216- enc := json .NewEncoder (& buf )
217- enc .SetIndent ("" , " " )
218- if err := enc .Encode (repoStatus ); err != nil {
219- return err
220- }
221- log .Printf ("Created repo status:\n %s" , buf .String ())
247+ if err := c .CreateAndLogStatus (client , owner , repo , sha , status ); err != nil {
248+ return err
222249 }
223250 }
224251
0 commit comments