@@ -5,6 +5,7 @@ package actions
55
66import (
77 "bytes"
8+ "context"
89 "io"
910 "slices"
1011 "strings"
@@ -44,12 +45,12 @@ func IsWorkflow(path string) bool {
4445 return strings .HasPrefix (path , ".gitea/workflows" ) || strings .HasPrefix (path , ".github/workflows" )
4546}
4647
47- func ListWorkflows (commit * git.Commit ) (string , git.Entries , error ) {
48+ func ListWorkflows (ctx context. Context , commit * git.Commit ) (string , git.Entries , error ) {
4849 rpath := ".gitea/workflows"
49- tree , err := commit .SubTree (rpath )
50+ tree , err := commit .SubTree (ctx , rpath )
5051 if _ , ok := err .(git.ErrNotExist ); ok {
5152 rpath = ".github/workflows"
52- tree , err = commit .SubTree (rpath )
53+ tree , err = commit .SubTree (ctx , rpath )
5354 }
5455 if _ , ok := err .(git.ErrNotExist ); ok {
5556 return "" , nil , nil
@@ -58,7 +59,7 @@ func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
5859 return "" , nil , err
5960 }
6061
61- entries , err := tree .ListEntriesRecursiveFast ()
62+ entries , err := tree .ListEntriesRecursiveFast (ctx )
6263 if err != nil {
6364 return "" , nil , err
6465 }
@@ -72,8 +73,8 @@ func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
7273 return rpath , ret , nil
7374}
7475
75- func GetContentFromEntry (entry * git.TreeEntry ) ([]byte , error ) {
76- f , err := entry .Blob ().DataAsync ()
76+ func GetContentFromEntry (ctx context. Context , entry * git.TreeEntry ) ([]byte , error ) {
77+ f , err := entry .Blob ().DataAsync (ctx )
7778 if err != nil {
7879 return nil , err
7980 }
@@ -99,21 +100,22 @@ func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
99100}
100101
101102func DetectWorkflows (
103+ ctx context.Context ,
102104 gitRepo * git.Repository ,
103105 commit * git.Commit ,
104106 triggedEvent webhook_module.HookEventType ,
105107 payload api.Payloader ,
106108 detectSchedule bool ,
107109) ([]* DetectedWorkflow , []* DetectedWorkflow , error ) {
108- _ , entries , err := ListWorkflows (commit )
110+ _ , entries , err := ListWorkflows (ctx , commit )
109111 if err != nil {
110112 return nil , nil , err
111113 }
112114
113115 workflows := make ([]* DetectedWorkflow , 0 , len (entries ))
114116 schedules := make ([]* DetectedWorkflow , 0 , len (entries ))
115117 for _ , entry := range entries {
116- content , err := GetContentFromEntry (entry )
118+ content , err := GetContentFromEntry (ctx , entry )
117119 if err != nil {
118120 return nil , nil , err
119121 }
@@ -135,7 +137,7 @@ func DetectWorkflows(
135137 }
136138 schedules = append (schedules , dwf )
137139 }
138- } else if detectMatched (gitRepo , commit , triggedEvent , payload , evt ) {
140+ } else if detectMatched (ctx , gitRepo , commit , triggedEvent , payload , evt ) {
139141 dwf := & DetectedWorkflow {
140142 EntryName : entry .Name (),
141143 TriggerEvent : evt ,
@@ -149,15 +151,15 @@ func DetectWorkflows(
149151 return workflows , schedules , nil
150152}
151153
152- func DetectScheduledWorkflows (gitRepo * git.Repository , commit * git.Commit ) ([]* DetectedWorkflow , error ) {
153- _ , entries , err := ListWorkflows (commit )
154+ func DetectScheduledWorkflows (ctx context. Context , gitRepo * git.Repository , commit * git.Commit ) ([]* DetectedWorkflow , error ) {
155+ _ , entries , err := ListWorkflows (ctx , commit )
154156 if err != nil {
155157 return nil , err
156158 }
157159
158160 wfs := make ([]* DetectedWorkflow , 0 , len (entries ))
159161 for _ , entry := range entries {
160- content , err := GetContentFromEntry (entry )
162+ content , err := GetContentFromEntry (ctx , entry )
161163 if err != nil {
162164 return nil , err
163165 }
@@ -184,7 +186,7 @@ func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*D
184186 return wfs , nil
185187}
186188
187- func detectMatched (gitRepo * git.Repository , commit * git.Commit , triggedEvent webhook_module.HookEventType , payload api.Payloader , evt * jobparser.Event ) bool {
189+ func detectMatched (ctx context. Context , gitRepo * git.Repository , commit * git.Commit , triggedEvent webhook_module.HookEventType , payload api.Payloader , evt * jobparser.Event ) bool {
188190 if ! canGithubEventMatch (evt .Name , triggedEvent ) {
189191 return false
190192 }
@@ -204,7 +206,7 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
204206
205207 case // push
206208 webhook_module .HookEventPush :
207- return matchPushEvent (commit , payload .(* api.PushPayload ), evt )
209+ return matchPushEvent (ctx , commit , payload .(* api.PushPayload ), evt )
208210
209211 case // issues
210212 webhook_module .HookEventIssues ,
@@ -227,7 +229,7 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
227229 webhook_module .HookEventPullRequestLabel ,
228230 webhook_module .HookEventPullRequestReviewRequest ,
229231 webhook_module .HookEventPullRequestMilestone :
230- return matchPullRequestEvent (gitRepo , commit , payload .(* api.PullRequestPayload ), evt )
232+ return matchPullRequestEvent (ctx , gitRepo , commit , payload .(* api.PullRequestPayload ), evt )
231233
232234 case // pull_request_review
233235 webhook_module .HookEventPullRequestReviewApproved ,
@@ -256,7 +258,7 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
256258 }
257259}
258260
259- func matchPushEvent (commit * git.Commit , pushPayload * api.PushPayload , evt * jobparser.Event ) bool {
261+ func matchPushEvent (ctx context. Context , commit * git.Commit , pushPayload * api.PushPayload , evt * jobparser.Event ) bool {
260262 // with no special filter parameters
261263 if len (evt .Acts ()) == 0 {
262264 return true
@@ -322,7 +324,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa
322324 matchTimes ++
323325 break
324326 }
325- filesChanged , err := commit .GetFilesChangedSinceCommit (pushPayload .Before )
327+ filesChanged , err := commit .GetFilesChangedSinceCommit (ctx , pushPayload .Before )
326328 if err != nil {
327329 log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
328330 } else {
@@ -339,7 +341,7 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa
339341 matchTimes ++
340342 break
341343 }
342- filesChanged , err := commit .GetFilesChangedSinceCommit (pushPayload .Before )
344+ filesChanged , err := commit .GetFilesChangedSinceCommit (ctx , pushPayload .Before )
343345 if err != nil {
344346 log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , commit .ID .String (), err )
345347 } else {
@@ -410,7 +412,7 @@ func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool
410412 return matchTimes == len (evt .Acts ())
411413}
412414
413- func matchPullRequestEvent (gitRepo * git.Repository , commit * git.Commit , prPayload * api.PullRequestPayload , evt * jobparser.Event ) bool {
415+ func matchPullRequestEvent (ctx context. Context , gitRepo * git.Repository , commit * git.Commit , prPayload * api.PullRequestPayload , evt * jobparser.Event ) bool {
414416 acts := evt .Acts ()
415417 activityTypeMatched := false
416418 matchTimes := 0
@@ -454,7 +456,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa
454456 err error
455457 )
456458 if evt .Name == GithubEventPullRequestTarget && (len (acts ["paths" ]) > 0 || len (acts ["paths-ignore" ]) > 0 ) {
457- headCommit , err = gitRepo .GetCommit (prPayload .PullRequest .Head .Sha )
459+ headCommit , err = gitRepo .GetCommit (ctx , prPayload .PullRequest .Head .Sha )
458460 if err != nil {
459461 log .Error ("GetCommit [ref: %s]: %v" , prPayload .PullRequest .Head .Sha , err )
460462 return false
@@ -486,7 +488,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa
486488 matchTimes ++
487489 }
488490 case "paths" :
489- filesChanged , err := headCommit .GetFilesChangedSinceCommit (prPayload .PullRequest .MergeBase )
491+ filesChanged , err := headCommit .GetFilesChangedSinceCommit (ctx , prPayload .PullRequest .MergeBase )
490492 if err != nil {
491493 log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , headCommit .ID .String (), err )
492494 } else {
@@ -499,7 +501,7 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa
499501 }
500502 }
501503 case "paths-ignore" :
502- filesChanged , err := headCommit .GetFilesChangedSinceCommit (prPayload .PullRequest .MergeBase )
504+ filesChanged , err := headCommit .GetFilesChangedSinceCommit (ctx , prPayload .PullRequest .MergeBase )
503505 if err != nil {
504506 log .Error ("GetFilesChangedSinceCommit [commit_sha1: %s]: %v" , headCommit .ID .String (), err )
505507 } else {
0 commit comments