gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. //nolint:forbidigo // use of print functions is allowed in cli
  4. package main
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "log"
  10. "net/http"
  11. "os"
  12. "os/exec"
  13. "path"
  14. "strconv"
  15. "strings"
  16. "github.com/google/go-github/v74/github"
  17. "github.com/urfave/cli/v3"
  18. "gopkg.in/yaml.v3"
  19. )
  20. const defaultVersion = "v1.18" // to backport to
  21. func main() {
  22. app := &cli.Command{}
  23. app.Name = "backport"
  24. app.Usage = "Backport provided PR-number on to the current or previous released version"
  25. app.Description = `Backport will look-up the PR in Gitea's git log and attempt to cherry-pick it on the current version`
  26. app.ArgsUsage = "<PR-to-backport>"
  27. app.Flags = []cli.Flag{
  28. &cli.StringFlag{
  29. Name: "version",
  30. Usage: "Version branch to backport on to",
  31. },
  32. &cli.StringFlag{
  33. Name: "upstream",
  34. Value: "origin",
  35. Usage: "Upstream remote for the Gitea upstream",
  36. },
  37. &cli.StringFlag{
  38. Name: "release-branch",
  39. Value: "",
  40. Usage: "Release branch to backport on. Will default to release/<version>",
  41. },
  42. &cli.StringFlag{
  43. Name: "cherry-pick",
  44. Usage: "SHA to cherry-pick as backport",
  45. },
  46. &cli.StringFlag{
  47. Name: "backport-branch",
  48. Usage: "Backport branch to backport on to (default: backport-<pr>-<version>",
  49. },
  50. &cli.StringFlag{
  51. Name: "remote",
  52. Value: "",
  53. Usage: "Remote for your fork of the Gitea upstream",
  54. },
  55. &cli.StringFlag{
  56. Name: "fork-user",
  57. Value: "",
  58. Usage: "Forked user name on Github",
  59. },
  60. &cli.StringFlag{
  61. Name: "gh-access-token",
  62. Value: "",
  63. Usage: "Access token for GitHub api request",
  64. },
  65. &cli.BoolFlag{
  66. Name: "no-fetch",
  67. Usage: "Set this flag to prevent fetch of remote branches",
  68. },
  69. &cli.BoolFlag{
  70. Name: "no-amend-message",
  71. Usage: "Set this flag to prevent automatic amendment of the commit message",
  72. },
  73. &cli.BoolFlag{
  74. Name: "no-push",
  75. Usage: "Set this flag to prevent pushing the backport up to your fork",
  76. },
  77. &cli.BoolFlag{
  78. Name: "no-xdg-open",
  79. Usage: "Set this flag to not use xdg-open to open the PR URL",
  80. },
  81. &cli.BoolFlag{
  82. Name: "continue",
  83. Usage: "Set this flag to continue from a git cherry-pick that has broken",
  84. },
  85. }
  86. cli.RootCommandHelpTemplate = `NAME:
  87. {{.Name}} - {{.Usage}}
  88. USAGE:
  89. {{.HelpName}} {{if .VisibleFlags}}[options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
  90. {{if len .Authors}}
  91. AUTHOR:
  92. {{range .Authors}}{{ . }}{{end}}
  93. {{end}}{{if .Commands}}
  94. OPTIONS:
  95. {{range .VisibleFlags}}{{.}}
  96. {{end}}{{end}}
  97. `
  98. app.Action = runBackport
  99. if err := app.Run(context.Background(), os.Args); err != nil {
  100. fmt.Fprintf(os.Stderr, "Unable to backport: %v\n", err)
  101. }
  102. }
  103. func runBackport(ctx context.Context, c *cli.Command) error {
  104. continuing := c.Bool("continue")
  105. var pr string
  106. version := c.String("version")
  107. if version == "" && continuing {
  108. // determine version from current branch name
  109. var err error
  110. pr, version, err = readCurrentBranch(ctx)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. if version == "" {
  116. version = readVersion()
  117. }
  118. if version == "" {
  119. version = defaultVersion
  120. }
  121. upstream := c.String("upstream")
  122. if upstream == "" {
  123. upstream = "origin"
  124. }
  125. forkUser := c.String("fork-user")
  126. remote := c.String("remote")
  127. if remote == "" && !c.Bool("--no-push") {
  128. var err error
  129. remote, forkUser, err = determineRemote(ctx, forkUser)
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. upstreamReleaseBranch := c.String("release-branch")
  135. if upstreamReleaseBranch == "" {
  136. upstreamReleaseBranch = path.Join("release", version)
  137. }
  138. localReleaseBranch := path.Join(upstream, upstreamReleaseBranch)
  139. args := c.Args().Slice()
  140. if len(args) == 0 && pr == "" {
  141. return errors.New("no PR number provided\nProvide a PR number to backport")
  142. } else if len(args) != 1 && pr == "" {
  143. return fmt.Errorf("multiple PRs provided %v\nOnly a single PR can be backported at a time", args)
  144. }
  145. if pr == "" {
  146. pr = args[0]
  147. }
  148. backportBranch := c.String("backport-branch")
  149. if backportBranch == "" {
  150. backportBranch = "backport-" + pr + "-" + version
  151. }
  152. fmt.Printf("* Backporting %s to %s as %s\n", pr, localReleaseBranch, backportBranch)
  153. sha := c.String("cherry-pick")
  154. accessToken := c.String("gh-access-token")
  155. if sha == "" {
  156. var err error
  157. sha, err = determineSHAforPR(ctx, pr, accessToken)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. if sha == "" {
  163. return fmt.Errorf("unable to determine sha for cherry-pick of %s", pr)
  164. }
  165. if !c.Bool("no-fetch") {
  166. if err := fetchRemoteAndMain(ctx, upstream, upstreamReleaseBranch); err != nil {
  167. return err
  168. }
  169. }
  170. if !continuing {
  171. if err := checkoutBackportBranch(ctx, backportBranch, localReleaseBranch); err != nil {
  172. return err
  173. }
  174. }
  175. if err := cherrypick(ctx, sha); err != nil {
  176. return err
  177. }
  178. if !c.Bool("no-amend-message") {
  179. if err := amendCommit(ctx, pr); err != nil {
  180. return err
  181. }
  182. }
  183. if !c.Bool("no-push") {
  184. url := "https://github.com/go-gitea/gitea/compare/" + upstreamReleaseBranch + "..." + forkUser + ":" + backportBranch
  185. if err := gitPushUp(ctx, remote, backportBranch); err != nil {
  186. return err
  187. }
  188. if !c.Bool("no-xdg-open") {
  189. if err := xdgOpen(ctx, url); err != nil {
  190. return err
  191. }
  192. } else {
  193. fmt.Printf("* Navigate to %s to open PR\n", url)
  194. }
  195. }
  196. return nil
  197. }
  198. func xdgOpen(ctx context.Context, url string) error {
  199. fmt.Printf("* `xdg-open %s`\n", url)
  200. out, err := exec.CommandContext(ctx, "xdg-open", url).Output()
  201. if err != nil {
  202. fmt.Fprintf(os.Stderr, "%s", string(out))
  203. return fmt.Errorf("unable to xdg-open to %s: %w", url, err)
  204. }
  205. return nil
  206. }
  207. func gitPushUp(ctx context.Context, remote, backportBranch string) error {
  208. fmt.Printf("* `git push -u %s %s`\n", remote, backportBranch)
  209. out, err := exec.CommandContext(ctx, "git", "push", "-u", remote, backportBranch).Output()
  210. if err != nil {
  211. fmt.Fprintf(os.Stderr, "%s", string(out))
  212. return fmt.Errorf("unable to push up to %s: %w", remote, err)
  213. }
  214. return nil
  215. }
  216. func amendCommit(ctx context.Context, pr string) error {
  217. fmt.Printf("* Amending commit to prepend `Backport #%s` to body\n", pr)
  218. out, err := exec.CommandContext(ctx, "git", "log", "-1", "--pretty=format:%B").Output()
  219. if err != nil {
  220. fmt.Fprintf(os.Stderr, "%s", string(out))
  221. return fmt.Errorf("unable to get last log message: %w", err)
  222. }
  223. parts := strings.SplitN(string(out), "\n", 2)
  224. if len(parts) != 2 {
  225. return fmt.Errorf("unable to interpret log message:\n%s", string(out))
  226. }
  227. subject, body := parts[0], parts[1]
  228. if !strings.HasSuffix(subject, " (#"+pr+")") {
  229. subject = subject + " (#" + pr + ")"
  230. }
  231. out, err = exec.CommandContext(ctx, "git", "commit", "--amend", "-m", subject+"\n\nBackport #"+pr+"\n"+body).Output()
  232. if err != nil {
  233. fmt.Fprintf(os.Stderr, "%s", string(out))
  234. return fmt.Errorf("unable to amend last log message: %w", err)
  235. }
  236. return nil
  237. }
  238. func cherrypick(ctx context.Context, sha string) error {
  239. // Check if a CHERRY_PICK_HEAD exists
  240. if _, err := os.Stat(".git/CHERRY_PICK_HEAD"); err == nil {
  241. // Assume that we are in the middle of cherry-pick - continue it
  242. fmt.Println("* Attempting git cherry-pick --continue")
  243. out, err := exec.CommandContext(ctx, "git", "cherry-pick", "--continue").Output()
  244. if err != nil {
  245. fmt.Fprintf(os.Stderr, "git cherry-pick --continue failed:\n%s\n", string(out))
  246. return fmt.Errorf("unable to continue cherry-pick: %w", err)
  247. }
  248. return nil
  249. }
  250. fmt.Printf("* Attempting git cherry-pick %s\n", sha)
  251. out, err := exec.CommandContext(ctx, "git", "cherry-pick", sha).Output()
  252. if err != nil {
  253. fmt.Fprintf(os.Stderr, "git cherry-pick %s failed:\n%s\n", sha, string(out))
  254. return fmt.Errorf("git cherry-pick %s failed: %w", sha, err)
  255. }
  256. return nil
  257. }
  258. func checkoutBackportBranch(ctx context.Context, backportBranch, releaseBranch string) error {
  259. out, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
  260. if err != nil {
  261. return fmt.Errorf("unable to check current branch %w", err)
  262. }
  263. currentBranch := strings.TrimSpace(string(out))
  264. fmt.Printf("* Current branch is %s\n", currentBranch)
  265. if currentBranch == backportBranch {
  266. fmt.Printf("* Current branch is %s - not checking out\n", currentBranch)
  267. return nil
  268. }
  269. if _, err := exec.CommandContext(ctx, "git", "rev-list", "-1", backportBranch).Output(); err == nil {
  270. fmt.Printf("* Branch %s already exists. Checking it out...\n", backportBranch)
  271. return exec.CommandContext(ctx, "git", "checkout", "-f", backportBranch).Run()
  272. }
  273. fmt.Printf("* `git checkout -b %s %s`\n", backportBranch, releaseBranch)
  274. return exec.CommandContext(ctx, "git", "checkout", "-b", backportBranch, releaseBranch).Run()
  275. }
  276. func fetchRemoteAndMain(ctx context.Context, remote, releaseBranch string) error {
  277. fmt.Printf("* `git fetch %s main`\n", remote)
  278. out, err := exec.CommandContext(ctx, "git", "fetch", remote, "main").Output()
  279. if err != nil {
  280. fmt.Println(string(out))
  281. return fmt.Errorf("unable to fetch %s from %s: %w", "main", remote, err)
  282. }
  283. fmt.Println(string(out))
  284. fmt.Printf("* `git fetch %s %s`\n", remote, releaseBranch)
  285. out, err = exec.CommandContext(ctx, "git", "fetch", remote, releaseBranch).Output()
  286. if err != nil {
  287. fmt.Println(string(out))
  288. return fmt.Errorf("unable to fetch %s from %s: %w", releaseBranch, remote, err)
  289. }
  290. fmt.Println(string(out))
  291. return nil
  292. }
  293. func determineRemote(ctx context.Context, forkUser string) (string, string, error) {
  294. out, err := exec.CommandContext(ctx, "git", "remote", "-v").Output()
  295. if err != nil {
  296. fmt.Fprintf(os.Stderr, "Unable to list git remotes:\n%s\n", string(out))
  297. return "", "", fmt.Errorf("unable to determine forked remote: %w", err)
  298. }
  299. lines := strings.SplitSeq(string(out), "\n")
  300. for line := range lines {
  301. fields := strings.Split(line, "\t")
  302. name, remote := fields[0], fields[1]
  303. // only look at pushers
  304. if !strings.HasSuffix(remote, " (push)") {
  305. continue
  306. }
  307. // only look at github.com pushes
  308. if !strings.Contains(remote, "github.com") {
  309. continue
  310. }
  311. // ignore go-gitea/gitea
  312. if strings.Contains(remote, "go-gitea/gitea") {
  313. continue
  314. }
  315. if !strings.Contains(remote, forkUser) {
  316. continue
  317. }
  318. if after, ok := strings.CutPrefix(remote, "git@github.com:"); ok {
  319. forkUser = after
  320. } else if after, ok := strings.CutPrefix(remote, "https://github.com/"); ok {
  321. forkUser = after
  322. } else if after, ok := strings.CutPrefix(remote, "https://www.github.com/"); ok {
  323. forkUser = after
  324. } else if forkUser == "" {
  325. return "", "", fmt.Errorf("unable to extract forkUser from remote %s: %s", name, remote)
  326. }
  327. idx := strings.Index(forkUser, "/")
  328. if idx >= 0 {
  329. forkUser = forkUser[:idx]
  330. }
  331. return name, forkUser, nil
  332. }
  333. return "", "", fmt.Errorf("unable to find appropriate remote in:\n%s", string(out))
  334. }
  335. func readCurrentBranch(ctx context.Context) (pr, version string, err error) {
  336. out, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
  337. if err != nil {
  338. fmt.Fprintf(os.Stderr, "Unable to read current git branch:\n%s\n", string(out))
  339. return "", "", fmt.Errorf("unable to read current git branch: %w", err)
  340. }
  341. parts := strings.Split(strings.TrimSpace(string(out)), "-")
  342. if len(parts) != 3 || parts[0] != "backport" {
  343. fmt.Fprintf(os.Stderr, "Unable to continue from git branch:\n%s\n", string(out))
  344. return "", "", fmt.Errorf("unable to continue from git branch:\n%s", string(out))
  345. }
  346. return parts[1], parts[2], nil
  347. }
  348. func readVersion() string {
  349. bs, err := os.ReadFile("docs/config.yaml")
  350. if err != nil {
  351. if err == os.ErrNotExist {
  352. log.Println("`docs/config.yaml` not present")
  353. return ""
  354. }
  355. fmt.Fprintf(os.Stderr, "Unable to read `docs/config.yaml`: %v\n", err)
  356. return ""
  357. }
  358. type params struct {
  359. Version string
  360. }
  361. type docConfig struct {
  362. Params params
  363. }
  364. dc := &docConfig{}
  365. if err := yaml.Unmarshal(bs, dc); err != nil {
  366. fmt.Fprintf(os.Stderr, "Unable to read `docs/config.yaml`: %v\n", err)
  367. return ""
  368. }
  369. if dc.Params.Version == "" {
  370. fmt.Fprintf(os.Stderr, "No version in `docs/config.yaml`")
  371. return ""
  372. }
  373. version := dc.Params.Version
  374. if version[0] != 'v' {
  375. version = "v" + version
  376. }
  377. split := strings.SplitN(version, ".", 3)
  378. return strings.Join(split[:2], ".")
  379. }
  380. func determineSHAforPR(ctx context.Context, prStr, accessToken string) (string, error) {
  381. prNum, err := strconv.Atoi(prStr)
  382. if err != nil {
  383. return "", err
  384. }
  385. client := github.NewClient(http.DefaultClient)
  386. if accessToken != "" {
  387. client = client.WithAuthToken(accessToken)
  388. }
  389. pr, _, err := client.PullRequests.Get(ctx, "go-gitea", "gitea", prNum)
  390. if err != nil {
  391. return "", err
  392. }
  393. if pr.Merged == nil || !*pr.Merged {
  394. return "", fmt.Errorf("PR #%d is not yet merged - cannot determine sha to backport", prNum)
  395. }
  396. if pr.MergeCommitSHA != nil {
  397. return *pr.MergeCommitSHA, nil
  398. }
  399. return "", nil
  400. }