1
0
mirror of https://codeberg.org/crowci/crow.git synced 2025-08-12 06:02:53 +03:00

Add release event trigger (#3226)

Supersedes #764 

Bitbucket does not support release webhooks.

---------

Co-authored-by: Patrick Schratz <patrick.schratz@gmail.com>
This commit is contained in:
qwerty287
2024-01-30 17:39:00 +01:00
committed by GitHub
parent da4bd8b97d
commit 9df572ef31
35 changed files with 628 additions and 18 deletions

View File

@@ -582,6 +582,15 @@ func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model
return nil, nil, err
}
if pipeline != nil && pipeline.Event == model.EventRelease && pipeline.Commit == "" {
tagName := strings.Split(pipeline.Ref, "/")[2]
sha, err := c.getTagCommitSHA(ctx, repo, tagName)
if err != nil {
return nil, nil, err
}
pipeline.Commit = sha
}
if pull != nil && len(pipeline.ChangedFiles) == 0 {
pipeline, err = c.loadChangedFilesFromPullRequest(ctx, pull, repo, pipeline)
if err != nil {
@@ -629,3 +638,49 @@ func (c *client) loadChangedFilesFromPullRequest(ctx context.Context, pull *gith
return pipeline, err
}
func (c *client) getTagCommitSHA(ctx context.Context, repo *model.Repo, tagName string) (string, error) {
_store, ok := store.TryFromContext(ctx)
if !ok {
log.Error().Msg("could not get store from context")
return "", nil
}
repo, err := _store.GetRepoNameFallback(repo.ForgeRemoteID, repo.FullName)
if err != nil {
return "", err
}
user, err := _store.GetUser(repo.UserID)
if err != nil {
return "", err
}
gh := c.newClientToken(ctx, user.Token)
if err != nil {
return "", err
}
page := 1
var tag *github.RepositoryTag
for {
tags, _, err := gh.Repositories.ListTags(ctx, repo.Owner, repo.Name, &github.ListOptions{Page: page})
if err != nil {
return "", err
}
for _, t := range tags {
if t.GetName() == tagName {
tag = t
break
}
}
if tag != nil {
break
}
}
if tag == nil {
return "", fmt.Errorf("could not find tag %s", tagName)
}
return tag.GetCommit().GetSHA(), nil
}