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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. if err := actions_model.CleanRepoScheduleTasks(ctx, input.Repo); err != nil {
  101. log.Error("CleanRepoScheduleTasks: %v", err)
  102. }
  103. return nil
  104. }
  105. if err := input.Repo.LoadUnits(ctx); err != nil {
  106. return fmt.Errorf("repo.LoadUnits: %w", err)
  107. } else if !input.Repo.UnitEnabled(ctx, unit_model.TypeActions) {
  108. return nil
  109. }
  110. gitRepo, err := git.OpenRepository(context.Background(), input.Repo.RepoPath())
  111. if err != nil {
  112. return fmt.Errorf("git.OpenRepository: %w", err)
  113. }
  114. defer gitRepo.Close()
  115. ref := input.Ref
  116. if input.Event == webhook_module.HookEventDelete {
  117. // The event is deleting a reference, so it will fail to get the commit for a deleted reference.
  118. // Set ref to empty string to fall back to the default branch.
  119. ref = ""
  120. }
  121. if ref == "" {
  122. ref = input.Repo.DefaultBranch
  123. }
  124. // Get the commit object for the ref
  125. commit, err := gitRepo.GetCommit(ref)
  126. if err != nil {
  127. return fmt.Errorf("gitRepo.GetCommit: %w", err)
  128. }
  129. var detectedWorkflows []*actions_module.DetectedWorkflow
  130. actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig()
  131. workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit,
  132. input.Event,
  133. input.Payload,
  134. input.Event == webhook_module.HookEventPush && input.Ref == input.Repo.DefaultBranch,
  135. )
  136. if err != nil {
  137. return fmt.Errorf("DetectWorkflows: %w", err)
  138. }
  139. if len(workflows) == 0 {
  140. log.Trace("repo %s with commit %s couldn't find workflows", input.Repo.RepoPath(), commit.ID)
  141. } else {
  142. for _, wf := range workflows {
  143. if actionsConfig.IsWorkflowDisabled(wf.EntryName) {
  144. log.Trace("repo %s has disable workflows %s", input.Repo.RepoPath(), wf.EntryName)
  145. continue
  146. }
  147. if wf.TriggerEvent.Name != actions_module.GithubEventPullRequestTarget {
  148. detectedWorkflows = append(detectedWorkflows, wf)
  149. }
  150. }
  151. }
  152. if input.PullRequest != nil {
  153. // detect pull_request_target workflows
  154. baseRef := git.BranchPrefix + input.PullRequest.BaseBranch
  155. baseCommit, err := gitRepo.GetCommit(baseRef)
  156. if err != nil {
  157. return fmt.Errorf("gitRepo.GetCommit: %w", err)
  158. }
  159. baseWorkflows, _, err := actions_module.DetectWorkflows(gitRepo, baseCommit, input.Event, input.Payload, false)
  160. if err != nil {
  161. return fmt.Errorf("DetectWorkflows: %w", err)
  162. }
  163. if len(baseWorkflows) == 0 {
  164. log.Trace("repo %s with commit %s couldn't find pull_request_target workflows", input.Repo.RepoPath(), baseCommit.ID)
  165. } else {
  166. for _, wf := range baseWorkflows {
  167. if wf.TriggerEvent.Name == actions_module.GithubEventPullRequestTarget {
  168. detectedWorkflows = append(detectedWorkflows, wf)
  169. }
  170. }
  171. }
  172. }
  173. if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil {
  174. return err
  175. }
  176. return handleWorkflows(ctx, detectedWorkflows, commit, input, ref)
  177. }
  178. func handleWorkflows(
  179. ctx context.Context,
  180. detectedWorkflows []*actions_module.DetectedWorkflow,
  181. commit *git.Commit,
  182. input *notifyInput,
  183. ref string,
  184. ) error {
  185. if len(detectedWorkflows) == 0 {
  186. log.Trace("repo %s with commit %s couldn't find workflows", input.Repo.RepoPath(), commit.ID)
  187. return nil
  188. }
  189. p, err := json.Marshal(input.Payload)
  190. if err != nil {
  191. return fmt.Errorf("json.Marshal: %w", err)
  192. }
  193. isForkPullRequest := false
  194. if pr := input.PullRequest; pr != nil {
  195. switch pr.Flow {
  196. case issues_model.PullRequestFlowGithub:
  197. isForkPullRequest = pr.IsFromFork()
  198. case issues_model.PullRequestFlowAGit:
  199. // There is no fork concept in agit flow, anyone with read permission can push refs/for/<target-branch>/<topic-branch> to the repo.
  200. // So we can treat it as a fork pull request because it may be from an untrusted user
  201. isForkPullRequest = true
  202. default:
  203. // unknown flow, assume it's a fork pull request to be safe
  204. isForkPullRequest = true
  205. }
  206. }
  207. for _, dwf := range detectedWorkflows {
  208. run := &actions_model.ActionRun{
  209. Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
  210. RepoID: input.Repo.ID,
  211. OwnerID: input.Repo.OwnerID,
  212. WorkflowID: dwf.EntryName,
  213. TriggerUserID: input.Doer.ID,
  214. Ref: ref,
  215. CommitSHA: commit.ID.String(),
  216. IsForkPullRequest: isForkPullRequest,
  217. Event: input.Event,
  218. EventPayload: string(p),
  219. TriggerEvent: dwf.TriggerEvent.Name,
  220. Status: actions_model.StatusWaiting,
  221. }
  222. if need, err := ifNeedApproval(ctx, run, input.Repo, input.Doer); err != nil {
  223. log.Error("check if need approval for repo %d with user %d: %v", input.Repo.ID, input.Doer.ID, err)
  224. continue
  225. } else {
  226. run.NeedApproval = need
  227. }
  228. jobs, err := jobparser.Parse(dwf.Content)
  229. if err != nil {
  230. log.Error("jobparser.Parse: %v", err)
  231. continue
  232. }
  233. // cancel running jobs if the event is push
  234. if run.Event == webhook_module.HookEventPush {
  235. // cancel running jobs of the same workflow
  236. if err := actions_model.CancelRunningJobs(
  237. ctx,
  238. run.RepoID,
  239. run.Ref,
  240. run.WorkflowID,
  241. run.Event,
  242. ); err != nil {
  243. log.Error("CancelRunningJobs: %v", err)
  244. }
  245. }
  246. if err := actions_model.InsertRun(ctx, run, jobs); err != nil {
  247. log.Error("InsertRun: %v", err)
  248. continue
  249. }
  250. alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID})
  251. if err != nil {
  252. log.Error("FindRunJobs: %v", err)
  253. continue
  254. }
  255. CreateCommitStatus(ctx, alljobs...)
  256. }
  257. return nil
  258. }
  259. func newNotifyInputFromIssue(issue *issues_model.Issue, event webhook_module.HookEventType) *notifyInput {
  260. return newNotifyInput(issue.Repo, issue.Poster, event)
  261. }
  262. func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) {
  263. if err := rel.LoadAttributes(ctx); err != nil {
  264. log.Error("LoadAttributes: %v", err)
  265. return
  266. }
  267. permission, _ := access_model.GetUserRepoPermission(ctx, rel.Repo, doer)
  268. newNotifyInput(rel.Repo, doer, webhook_module.HookEventRelease).
  269. WithRef(git.RefNameFromTag(rel.TagName).String()).
  270. WithPayload(&api.ReleasePayload{
  271. Action: action,
  272. Release: convert.ToAPIRelease(ctx, rel.Repo, rel),
  273. Repository: convert.ToRepo(ctx, rel.Repo, permission),
  274. Sender: convert.ToUser(ctx, doer, nil),
  275. }).
  276. Notify(ctx)
  277. }
  278. func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
  279. if pd.Repository == nil {
  280. // When a package is uploaded to an organization, it could trigger an event to notify.
  281. // So the repository could be nil, however, actions can't support that yet.
  282. // See https://github.com/go-gitea/gitea/pull/17940
  283. return
  284. }
  285. apiPackage, err := convert.ToPackage(ctx, pd, sender)
  286. if err != nil {
  287. log.Error("Error converting package: %v", err)
  288. return
  289. }
  290. newNotifyInput(pd.Repository, sender, webhook_module.HookEventPackage).
  291. WithPayload(&api.PackagePayload{
  292. Action: action,
  293. Package: apiPackage,
  294. Sender: convert.ToUser(ctx, sender, nil),
  295. }).
  296. Notify(ctx)
  297. }
  298. func ifNeedApproval(ctx context.Context, run *actions_model.ActionRun, repo *repo_model.Repository, user *user_model.User) (bool, error) {
  299. // 1. don't need approval if it's not a fork PR
  300. // 2. don't need approval if the event is `pull_request_target` since the workflow will run in the context of base branch
  301. // see https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks#about-workflow-runs-from-public-forks
  302. if !run.IsForkPullRequest || run.TriggerEvent == actions_module.GithubEventPullRequestTarget {
  303. return false, nil
  304. }
  305. // always need approval if the user is restricted
  306. if user.IsRestricted {
  307. log.Trace("need approval because user %d is restricted", user.ID)
  308. return true, nil
  309. }
  310. // don't need approval if the user can write
  311. if perm, err := access_model.GetUserRepoPermission(ctx, repo, user); err != nil {
  312. return false, fmt.Errorf("GetUserRepoPermission: %w", err)
  313. } else if perm.CanWrite(unit_model.TypeActions) {
  314. log.Trace("do not need approval because user %d can write", user.ID)
  315. return false, nil
  316. }
  317. // don't need approval if the user has been approved before
  318. if count, err := actions_model.CountRuns(ctx, actions_model.FindRunOptions{
  319. RepoID: repo.ID,
  320. TriggerUserID: user.ID,
  321. Approved: true,
  322. }); err != nil {
  323. return false, fmt.Errorf("CountRuns: %w", err)
  324. } else if count > 0 {
  325. log.Trace("do not need approval because user %d has been approved before", user.ID)
  326. return false, nil
  327. }
  328. // otherwise, need approval
  329. log.Trace("need approval because it's the first time user %d triggered actions", user.ID)
  330. return true, nil
  331. }
  332. func handleSchedules(
  333. ctx context.Context,
  334. detectedWorkflows []*actions_module.DetectedWorkflow,
  335. commit *git.Commit,
  336. input *notifyInput,
  337. ref string,
  338. ) error {
  339. branch, err := commit.GetBranchName()
  340. if err != nil {
  341. return err
  342. }
  343. if branch != input.Repo.DefaultBranch {
  344. log.Trace("commit branch is not default branch in repo")
  345. return nil
  346. }
  347. if count, err := actions_model.CountSchedules(ctx, actions_model.FindScheduleOptions{RepoID: input.Repo.ID}); err != nil {
  348. log.Error("CountSchedules: %v", err)
  349. return err
  350. } else if count > 0 {
  351. if err := actions_model.CleanRepoScheduleTasks(ctx, input.Repo); err != nil {
  352. log.Error("CleanRepoScheduleTasks: %v", err)
  353. }
  354. }
  355. if len(detectedWorkflows) == 0 {
  356. log.Trace("repo %s with commit %s couldn't find schedules", input.Repo.RepoPath(), commit.ID)
  357. return nil
  358. }
  359. p, err := json.Marshal(input.Payload)
  360. if err != nil {
  361. return fmt.Errorf("json.Marshal: %w", err)
  362. }
  363. crons := make([]*actions_model.ActionSchedule, 0, len(detectedWorkflows))
  364. for _, dwf := range detectedWorkflows {
  365. // Check cron job condition. Only working in default branch
  366. workflow, err := model.ReadWorkflow(bytes.NewReader(dwf.Content))
  367. if err != nil {
  368. log.Error("ReadWorkflow: %v", err)
  369. continue
  370. }
  371. schedules := workflow.OnSchedule()
  372. if len(schedules) == 0 {
  373. log.Warn("no schedule event")
  374. continue
  375. }
  376. run := &actions_model.ActionSchedule{
  377. Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
  378. RepoID: input.Repo.ID,
  379. OwnerID: input.Repo.OwnerID,
  380. WorkflowID: dwf.EntryName,
  381. TriggerUserID: input.Doer.ID,
  382. Ref: ref,
  383. CommitSHA: commit.ID.String(),
  384. Event: input.Event,
  385. EventPayload: string(p),
  386. Specs: schedules,
  387. Content: dwf.Content,
  388. }
  389. crons = append(crons, run)
  390. }
  391. return actions_model.CreateScheduleTask(ctx, crons)
  392. }