summaryrefslogtreecommitdiffstats
path: root/models/forgejo_migrations/v1_22/v8.go
blob: 2d3c0c594b959a8084f1d92f11bf0a4882ca323f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_22 //nolint

import (
	"strings"

	"xorm.io/xorm"
)

func RemoveSSHSignaturesFromReleaseNotes(x *xorm.Engine) error {
	type Release struct {
		ID   int64  `xorm:"pk autoincr"`
		Note string `xorm:"TEXT"`
	}

	if err := x.Sync(&Release{}); err != nil {
		return err
	}

	var releaseNotes []struct {
		ID   int64
		Note string
	}

	if err := x.Table("release").Where("note LIKE '%-----BEGIN SSH SIGNATURE-----%'").Find(&releaseNotes); err != nil {
		return err
	}

	sess := x.NewSession()
	defer sess.Close()

	if err := sess.Begin(); err != nil {
		return err
	}

	for _, release := range releaseNotes {
		idx := strings.LastIndex(release.Note, "-----BEGIN SSH SIGNATURE-----")
		if idx == -1 {
			continue
		}
		release.Note = release.Note[:idx]
		_, err := sess.Exec("UPDATE `release` SET note = ? WHERE id = ?", release.Note, release.ID)
		if err != nil {
			return err
		}
	}

	return sess.Commit()
}