Repository: gogs/gogs
File: internal/database/webhook_discord.go (Go)

Code before:
				IconURL: p.Sender.AvatarUrl,
			},
			Fields: fields,
		}},
	}
}

func getDiscordReleasePayload(p *api.ReleasePayload) *DiscordPayload {
	repoLink := DiscordLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
	refLink := DiscordLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
	content := fmt.Sprintf("Published new release %s of %s", refLink, repoLink)
	return &DiscordPayload{
		Embeds: []*DiscordEmbedObject{{
			Description: content,
			URL:         conf.Server.ExternalURL + p.Sender.UserName,
			Author: &DiscordEmbedAuthorObject{
				Name:    p.Sender.UserName,
				IconURL: p.Sender.AvatarUrl,
			},
		}},
	}
}

func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
	slack := &SlackMeta{}
	if err := json.Unmarshal([]byte(meta), &slack); err != nil {
		return nil, errors.Newf("unmarshal: %v", err)
	}

	switch event {
	case HookEventTypeCreate:
		payload = getDiscordCreatePayload(p.(*api.CreatePayload))
	case HookEventTypeDelete:
		payload = getDiscordDeletePayload(p.(*api.DeletePayload))
	case HookEventTypeFork:
		payload = getDiscordForkPayload(p.(*api.ForkPayload))
	case HookEventTypePush:
		payload = getDiscordPushPayload(p.(*api.PushPayload), slack)
	case HookEventTypeIssues:
		payload = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
	case HookEventTypeIssueComment:
		payload = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
	case HookEventTypePullRequest:
		payload = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
	case HookEventTypeRelease:
		payload = getDiscordReleasePayload(p.(*api.ReleasePayload))
	default:
		return nil, errors.Errorf("unexpected event %q", event)
	}

	payload.Username = slack.Username

Review comment (suggestion):
`slack` is already a `*SlackMeta`, but Unmarshal is being called with `&slack` (a `**SlackMeta`). With `encoding/json`, unmarshalling a JSON `null` would set `slack` to `nil`, which can lead to panics later when accessing fields. Unmarshal into `slack` (or declare `var slack SlackMeta` and pass `&slack`) to keep the pointer non-nil and avoid unintended pointer reassignment.
```suggestion
	if err := json.Unmarshal([]byte(meta), slack); err != nil {
```

Code after:
				IconURL: p.Sender.AvatarUrl,
			},
			Fields: fields,
		}},
	}
}

func getDiscordReleasePayload(p *api.ReleasePayload) *DiscordPayload {
	repoLink := DiscordLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
	refLink := DiscordLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
	content := fmt.Sprintf("Published new release %s of %s", refLink, repoLink)
	return &DiscordPayload{
		Embeds: []*DiscordEmbedObject{{
			Description: content,
			URL:         conf.Server.ExternalURL + p.Sender.UserName,
			Author: &DiscordEmbedAuthorObject{
				Name:    p.Sender.UserName,
				IconURL: p.Sender.AvatarUrl,
			},
		}},
	}
}

func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
	slack := &SlackMeta{}
	if err := json.Unmarshal([]byte(meta), slack); err != nil {
		return nil, errors.Newf("unmarshal: %v", err)
	}

	switch event {
	case HookEventTypeCreate:
		payload = getDiscordCreatePayload(p.(*api.CreatePayload))
	case HookEventTypeDelete:
		payload = getDiscordDeletePayload(p.(*api.DeletePayload))
	case HookEventTypeFork:
		payload = getDiscordForkPayload(p.(*api.ForkPayload))
	case HookEventTypePush:
		payload = getDiscordPushPayload(p.(*api.PushPayload), slack)
	case HookEventTypeIssues:
		payload = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
	case HookEventTypeIssueComment:
		payload = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
	case HookEventTypePullRequest:
		payload = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
	case HookEventTypeRelease:
		payload = getDiscordReleasePayload(p.(*api.ReleasePayload))
	default:
		return nil, errors.Errorf("unexpected event %q", event)
	}

	payload.Username = slack.Username