You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notifier_helper.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "bytes"
  6. "context"
  7. "fmt"
  8. "strings"
  9. actions_model "code.gitea.io/gitea/models/actions"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. packages_model "code.gitea.io/gitea/models/packages"
  12. access_model "code.gitea.io/gitea/models/perm/access"
  13. repo_model "code.gitea.io/gitea/models/repo"
  14. unit_model "code.gitea.io/gitea/models/unit"
  15. user_model "code.gitea.io/gitea/models/user"
  16. actions_module "code.gitea.io/gitea/modules/actions"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/json"
  19. "code.gitea.io/gitea/modules/log"
  20. api "code.gitea.io/gitea/modules/structs"
  21. webhook_module "code.gitea.io/gitea/modules/webhook"
  22. "code.gitea.io/gitea/services/convert"
  23. "github.com/nektos/act/pkg/jobparser"
  24. "github.com/nektos/act/pkg/model"
  25. )
  26. var methodCtxKey struct{}
  27. // withMethod sets the notification method that this context currently executes.
  28. // Used for debugging/ troubleshooting purposes.
  29. func withMethod(ctx context.Context, method string) context.Context {
  30. // don't overwrite
  31. if v := ctx.Value(methodCtxKey); v != nil {
  32. if _, ok := v.(string); ok {
  33. return ctx
  34. }
  35. }
  36. return context.WithValue(ctx, methodCtxKey, method)
  37. }
  38. // getMethod gets the notification method that this context currently executes.
  39. // Default: "notify"
  40. // Used for debugging/ troubleshooting purposes.
  41. func getMethod(ctx context.Context) string {
  42. if v := ctx.Value(methodCtxKey); v != nil {
  43. if s, ok := v.(string); ok {
  44. return s
  45. }
  46. }
  47. return "notify"
  48. }
  49. type notifyInput struct {
  50. // required
  51. Repo *repo_model.Repository
  52. Doer *user_model.User
  53. Event webhook_module.HookEventType
  54. // optional
  55. Ref string
  56. Payload api.Payloader
  57. PullRequest *issues_model.PullRequest
  58. }
  59. func newNotifyInput(repo *repo_model.Repository, doer *user_model.User, event webhook_module.HookEventType) *notifyInput {
  60. return &notifyInput{
  61. Repo: repo,
  62. Doer: doer,
  63. Event: event,
  64. }
  65. }
  66. func (input *notifyInput) WithDoer(doer *user_model.User) *notifyInput {
  67. input.Doer = doer
  68. return input
  69. }
  70. func (input *notifyInput) WithRef(ref string) *notifyInput {
  71. input.Ref = ref
  72. return input
  73. }
  74. func (input *notifyInput) WithPayload(payload api.Payloader) *notifyInput {
  75. input.Payload = payload
  76. return input
  77. }
  78. func (input *notifyInput) WithPullRequest(pr *issues_model.PullRequest) *notifyInput {
  79. input.PullRequest = pr
  80. if input.Ref == "" {
  81. input.Ref = pr.GetGitRefName()
  82. }
  83. return input
  84. }
  85. func (input *notifyInput) Notify(ctx context.Context) {
  86. log.Trace("execute %v for event %v whose doer is %v", getMethod(ctx), input.Event, input.Doer.Name)
  87. if err := notify(ctx, input); err != nil {
  88. log.Error("an error occurred while executing the %s actions method: %v", getMethod(ctx), err)
  89. }
  90. }
  91. func notify(ctx context.Context, input *notifyInput) error {
  92. if input.Doer.IsActions() {
  93. // avoiding triggering cyclically, for example:
  94. // a comment of an issue will trigger the runner to add a new comment as reply,
  95. // and the new comment will trigger the runner again.
  96. log.Debug("ignore executing %v for event %v whose doer is %v", getMethod(ctx), input.Event, input.Doer.Name)
  97. return nil
  98. }
  99. if unit_model.TypeActions.UnitGlobalDisabled() {
  100. return nil
  101. }
  102. if err := input.Repo.LoadUnits(ctx); err != nil {
  103. return fmt.Errorf("repo.LoadUnits: %w", err)
  104. } else if !input.Repo.UnitEnabled(ctx, unit_model.TypeActions) {
  105. return nil
  106. }
  107. gitRepo, err := git.OpenRepository(context.Background(), input.Repo.RepoPath())
  108. if err != nil {
  109. return fmt.Errorf("git.OpenRepository: %w", err)
  110. }
  111. defer gitRepo.Close()
  112. ref := input.Ref
  113. if input.Event == webhook_module.HookEventDelete {
  114. // The event is deleting a reference, so it will fail to get the commit for a deleted reference.
  115. // Set ref to empty string to fall back to the default branch.
  116. ref = ""
  117. }
  118. if ref == "" {
  119. ref = input.Repo.DefaultBranch
  120. }
  121. // Get the commit object for the ref
  122. commit, err := gitRepo.GetCommit(ref)
  123. if err != nil {
  124. return fmt.Errorf("gitRepo.GetCommit: %w", err)
  125. }
  126. var detectedWorkflows []*actions_module.DetectedWorkflow
  127. actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
  128. workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, input.Event, input.Payload)
  129. if err != nil {
  130. return fmt.Errorf("DetectWorkflows: %w", err)
  131. }
  132. if len(workflows) == 0 {
  133. log.Trace("repo %s with commit %s couldn't find workflows", input.Repo.RepoPath(), commit.ID)
  134. } else {
  135. for _, wf := range workflows {
  136. if actionsConfig.IsWorkflowDisabled(wf.EntryName) {
  137. log.Trace("repo %s has disable workflows %s", input.Repo.RepoPath(), wf.EntryName)
  138. continue
  139. }
  140. if wf.TriggerEvent != actions_module.GithubEventPullRequestTarget {
  141. detectedWorkflows = append(detectedWorkflows, wf)
  142. }
  143. }
  144. }
  145. if input.PullRequest != nil {
  146. // detect pull_request_target workflows
  147. baseRef := git.BranchPrefix + input.PullRequest.BaseBranch
  148. baseCommit, err := gitRepo.GetCommit(baseRef)
  149. if err != nil {
  150. return fmt.Errorf("gitRepo.GetCommit: %w", err)
  151. }
  152. baseWorkflows, _, err := actions_module.DetectWorkflows(gitRepo, baseCommit, input.Event, input.Payload)
  153. if err != nil {
  154. return fmt.Errorf("DetectWorkflows: %w", err)
  155. }
  156. if len(baseWorkflows) == 0 {
  157. log.Trace("repo %s with commit %s couldn't find pull_request_target workflows", input.Repo.RepoPath(), baseCommit.ID)
  158. } else {
  159. for _, wf := range baseWorkflows {
  160. if wf.TriggerEvent == actions_module.GithubEventPullRequestTarget {
  161. detectedWorkflows = append(detectedWorkflows, wf)
  162. }
  163. }
  164. }
  165. }
  166. if err := handleSchedules(ctx, schedules, commit, input); err != nil {
  167. return err
  168. }
  169. return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
  170. }
  171. func handleWorkflows(
  172. ctx context.Context,
  173. detectedWorkflows []*actions_module.DetectedWorkflow,
  174. commit *git.Commit,
  175. input *notifyInput,
  176. ref string,
  177. ) error {
  178. if len(detectedWorkflows) == 0 {
  179. log.Trace("repo %s with commit %s couldn't find workflows", input.Repo.RepoPath(), commit.ID)
  180. return nil
  181. }
  182. p, err := json.Marshal(input.Payload)
  183. if err != nil {
  184. return fmt.Errorf("json.Marshal: %w", err)
  185. }
  186. isForkPullRequest := false
  187. if pr := input.PullRequest; pr != nil {
  188. switch pr.Flow {
  189. case issues_model.PullRequestFlowGithub:
  190. isForkPullRequest = pr.IsFromFork()
  191. case issues_model.PullRequestFlowAGit:
  192. // There is no fork concept in agit flow, anyone with read permission can push refs/for/<target-branch>/<topic-branch> to the repo.
  193. // So we can treat it as a fork pull request because it may be from an untrusted user
  194. isForkPullRequest = true
  195. default:
  196. // unknown flow, assume it's a fork pull request to be safe
  197. isForkPullRequest = true
  198. }
  199. }
  200. for _, dwf := range detectedWorkflows {
  201. run := &actions_model.ActionRun{
  202. Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
  203. RepoID: input.Repo.ID,
  204. OwnerID: input.Repo.OwnerID,
  205. WorkflowID: dwf.EntryName,
  206. TriggerUserID: input.Doer.ID,
  207. Ref: ref,
  208. CommitSHA: commit.ID.String(),
  209. IsForkPullRequest: isForkPullRequest,
  210. Event: input.Event,
  211. EventPayload: string(p),
  212. TriggerEvent: dwf.TriggerEvent,
  213. Status: actions_model.StatusWaiting,
  214. }
  215. if need, err := ifNeedApproval(ctx, run, input.Repo, input.Doer); err != nil {
  216. log.Error("check if need approval for repo %d with user %d: %v", input.Repo.ID, input.Doer.ID, err)
  217. continue
  218. } else {
  219. run.NeedApproval = need
  220. }
  221. jobs, err := jobparser.Parse(dwf.Content)
  222. if err != nil {
  223. log.Error("jobparser.Parse: %v", err)
  224. continue
  225. }
  226. // cancel running jobs if the event is push
  227. if run.Event == webhook_module.HookEventPush {
  228. // cancel running jobs of the same workflow
  229. if err := actions_model.CancelRunningJobs(
  230. ctx,
  231. run.RepoID,
  232. run.Ref,
  233. run.WorkflowID,
  234. ); err != nil {
  235. log.Error("CancelRunningJobs: %v", err)
  236. }
  237. }
  238. if err := actions_model.InsertRun(ctx, run, jobs); err != nil {
  239. log.Error("InsertRun: %v", err)
  240. continue
  241. }
  242. alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID})
  243. if err != nil {
  244. log.Error("FindRunJobs: %v", err)
  245. continue
  246. }
  247. CreateCommitStatus(ctx, alljobs...)
  248. }
  249. return nil
  250. }
  251. func newNotifyInputFromIssue(issue *issues_model.Issue, event webhook_module.HookEventType) *notifyInput {
  252. return newNotifyInput(issue.Repo, issue.Poster, event)
  253. }
  254. func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) {
  255. if err := rel.LoadAttributes(ctx); err != nil {
  256. log.Error("LoadAttributes: %v", err)
  257. return
  258. }
  259. permission, _ := access_model.GetUserRepoPermission(ctx, rel.Repo, doer)
  260. newNotifyInput(rel.Repo, doer, webhook_module.HookEventRelease).
  261. WithRef(git.RefNameFromTag(rel.TagName).String()).
  262. WithPayload(&api.ReleasePayload{
  263. Action: action,
  264. Release: convert.ToAPIRelease(ctx, rel.Repo, rel),
  265. Repository: convert.ToRepo(ctx, rel.Repo, permission),
  266. Sender: convert.ToUser(ctx, doer, nil),
  267. }).
  268. Notify(ctx)
  269. }
  270. func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
  271. if pd.Repository == nil {
  272. // When a package is uploaded to an organization, it could trigger an event to notify.
  273. // So the repository could be nil, however, actions can't support that yet.
  274. // See https://github.com/go-gitea/gitea/pull/17940
  275. return
  276. }
  277. apiPackage, err := convert.ToPackage(ctx, pd, sender)
  278. if err != nil {
  279. log.Error("Error converting package: %v", err)
  280. return
  281. }
  282. newNotifyInput(pd.Repository, sender, webhook_module.HookEventPackage).
  283. WithPayload(&api.PackagePayload{
  284. Action: action,
  285. Package: apiPackage,
  286. Sender: convert.ToUser(ctx, sender, nil),
  287. }).
  288. Notify(ctx)
  289. }
  290. func ifNeedApproval(ctx context.Context, run *actions_model.ActionRun, repo *repo_model.Repository, user *user_model.User) (bool, error) {
  291. // 1. don't need approval if it's not a fork PR
  292. // 2. don't need approval if the event is `pull_request_target` since the workflow will run in the context of base branch
  293. // see https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks#about-workflow-runs-from-public-forks
  294. if !run.IsForkPullRequest || run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
  295. return false, nil
  296. }
  297. // always need approval if the user is restricted
  298. if user.IsRestricted {
  299. log.Trace("need approval because user %d is restricted", user.ID)
  300. return true, nil
  301. }
  302. // don't need approval if the user can write
  303. if perm, err := access_model.GetUserRepoPermission(ctx, repo, user); err != nil {
  304. return false, fmt.Errorf("GetUserRepoPermission: %w", err)
  305. } else if perm.CanWrite(unit_model.TypeActions) {
  306. log.Trace("do not need approval because user %d can write", user.ID)
  307. return false, nil
  308. }
  309. // don't need approval if the user has been approved before
  310. if count, err := actions_model.CountRuns(ctx, actions_model.FindRunOptions{
  311. RepoID: repo.ID,
  312. TriggerUserID: user.ID,
  313. Approved: true,
  314. }); err != nil {
  315. return false, fmt.Errorf("CountRuns: %w", err)
  316. } else if count > 0 {
  317. log.Trace("do not need approval because user %d has been approved before", user.ID)
  318. return false, nil
  319. }
  320. // otherwise, need approval
  321. log.Trace("need approval because it's the first time user %d triggered actions", user.ID)
  322. return true, nil
  323. }
  324. func handleSchedules(
  325. ctx context.Context,
  326. detectedWorkflows []*actions_module.DetectedWorkflow,
  327. commit *git.Commit,
  328. input *notifyInput,
  329. ) error {
  330. branch, err := commit.GetBranchName()
  331. if err != nil {
  332. return err
  333. }
  334. if branch != input.Repo.DefaultBranch {
  335. log.Trace("commit branch is not default branch in repo")
  336. return nil
  337. }
  338. if count, err := actions_model.CountSchedules(ctx, actions_model.FindScheduleOptions{RepoID: input.Repo.ID}); err != nil {
  339. log.Error("CountSchedules: %v", err)
  340. return err
  341. } else if count > 0 {
  342. if err := actions_model.DeleteScheduleTaskByRepo(ctx, input.Repo.ID); err != nil {
  343. log.Error("DeleteCronTaskByRepo: %v", err)
  344. }
  345. }
  346. if len(detectedWorkflows) == 0 {
  347. log.Trace("repo %s with commit %s couldn't find schedules", input.Repo.RepoPath(), commit.ID)
  348. return nil
  349. }
  350. p, err := json.Marshal(input.Payload)
  351. if err != nil {
  352. return fmt.Errorf("json.Marshal: %w", err)
  353. }
  354. crons := make([]*actions_model.ActionSchedule, 0, len(detectedWorkflows))
  355. for _, dwf := range detectedWorkflows {
  356. // Check cron job condition. Only working in default branch
  357. workflow, err := model.ReadWorkflow(bytes.NewReader(dwf.Content))
  358. if err != nil {
  359. log.Error("ReadWorkflow: %v", err)
  360. continue
  361. }
  362. schedules := workflow.OnSchedule()
  363. if len(schedules) == 0 {
  364. log.Warn("no schedule event")
  365. continue
  366. }
  367. run := &actions_model.ActionSchedule{
  368. Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
  369. RepoID: input.Repo.ID,
  370. OwnerID: input.Repo.OwnerID,
  371. WorkflowID: dwf.EntryName,
  372. TriggerUserID: input.Doer.ID,
  373. Ref: input.Ref,
  374. CommitSHA: commit.ID.String(),
  375. Event: input.Event,
  376. EventPayload: string(p),
  377. Specs: schedules,
  378. Content: dwf.Content,
  379. }
  380. // cancel running jobs if the event is push
  381. if run.Event == webhook_module.HookEventPush {
  382. // cancel running jobs of the same workflow
  383. if err := actions_model.CancelRunningJobs(
  384. ctx,
  385. run.RepoID,
  386. run.Ref,
  387. run.WorkflowID,
  388. ); err != nil {
  389. log.Error("CancelRunningJobs: %v", err)
  390. }
  391. }
  392. crons = append(crons, run)
  393. }
  394. return actions_model.CreateScheduleTask(ctx, crons)
  395. }