-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/defenseunicorns/uds-releaser/src/types" | ||
"github.com/defenseunicorns/uds-releaser/src/utils" | ||
github "github.com/google/go-github/v66/github" | ||
) | ||
|
||
func TagAndRelease(flavor types.Flavor, tokenVarName string) error { | ||
repo, err := utils.OpenRepo() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
remote, err := repo.Remote("origin") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the default branch of the current repository | ||
ref, err := repo.Head() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create a new GitHub client | ||
githubClient := github.NewClient(nil) | ||
|
||
// Set the authentication token | ||
githubClient = githubClient.WithAuthToken(os.Getenv(tokenVarName)) | ||
|
||
// Get the repository owner and name | ||
|
||
remoteURL := remote.Config().URLs[0] | ||
|
||
owner, repoName, err := getGithubOwnerAndRepo(remoteURL) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create the tag | ||
zarfPackageName, err := utils.GetPackageName() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tagName := fmt.Sprintf("%s-%s", flavor.Version, flavor.Name) | ||
releaseName := fmt.Sprintf("%s %s", zarfPackageName, tagName) | ||
|
||
tag := createGitHubTag(tagName, releaseName, ref.Hash().String()) | ||
|
||
createdTag, _, err := githubClient.Git.CreateTag(context.Background(), owner, repoName, tag) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create a reference for the tag | ||
tagRef := &github.Reference{ | ||
Ref: github.String("refs/tags/" + tagName), | ||
Object: &github.GitObject{ | ||
SHA: createdTag.SHA, | ||
}, | ||
} | ||
|
||
_, _, err = githubClient.Git.CreateRef(context.Background(), owner, repoName, tagRef) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create the release | ||
release := &github.RepositoryRelease{ | ||
TagName: github.String(tagName), | ||
Name: github.String(releaseName), | ||
Body: github.String(releaseName), //TODO @corang release notes | ||
GenerateReleaseNotes: github.Bool(true), | ||
} | ||
|
||
_, _, err = githubClient.Repositories.CreateRelease(context.Background(), owner, repoName, release) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func createGitHubTag(tagName string, releaseName string, hash string) *github.Tag { | ||
tag := &github.Tag{ | ||
Tag: github.String(tagName), | ||
Message: github.String(releaseName), | ||
Object: &github.GitObject{ | ||
SHA: github.String(hash), | ||
Type: github.String("commit"), | ||
}, | ||
Tagger: &github.CommitAuthor{ | ||
Name: github.String(os.Getenv("GITHUB_ACTOR")), | ||
Email: github.String(os.Getenv("GITHUB_ACTOR") + "@users.noreply.github.com"), | ||
Date: &github.Timestamp{Time: time.Now()}, | ||
}, | ||
} | ||
return tag | ||
} | ||
|
||
func getGithubOwnerAndRepo(remoteURL string) (string, string, error) { | ||
// Parse the GitHub owner and repository name from the remote URL | ||
ownerRepoRegex := regexp.MustCompile(`github.com[:/](.*)/(.*).git`) | ||
Check failure Code scanning / CodeQL Incomplete regular expression for hostnames High
This regular expression has an unescaped dot before 'com', so it might match more hosts than expected when
the regular expression is used Error loading related location Loading |
||
matches := ownerRepoRegex.FindStringSubmatch(remoteURL) | ||
if len(matches) != 3 { | ||
return "", "", fmt.Errorf("could not parse GitHub owner and repository name from remote URL: %s", remoteURL) | ||
} | ||
|
||
owner := matches[1] | ||
repo := matches[2] | ||
|
||
return owner, repo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package github | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testCreateGitHubTag(t *testing.T) { | ||
// Create a new tag | ||
tagName := "v1.0.0-uds.0-unicorn" | ||
releaseName := "testing-package v1.0.0-uds.0-unicorn" | ||
hash := "1234567890" | ||
|
||
tag := createGitHubTag(tagName, releaseName, hash) | ||
|
||
assert.Equal(t, tagName, *tag.Tag) | ||
assert.Equal(t, releaseName, *tag.Message) | ||
assert.Equal(t, hash, *tag.Object.SHA) | ||
} | ||
|
||
func testgetGithubOwnerAndRepo(t *testing.T) { | ||
// Get the owner and repo from a remote URL | ||
httpsRemoteURL := "https://github.com/defenseunicorns/uds-releaser.git" | ||
sshRemoteURL := "git@github.com:defenseunicorns/uds-releaser.git" | ||
|
||
owner, repo, err := getGithubOwnerAndRepo(httpsRemoteURL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "defenseunicorns", owner) | ||
assert.Equal(t, "uds-releaser", repo) | ||
|
||
owner, repo, err = getGithubOwnerAndRepo(sshRemoteURL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "defenseunicorns", owner) | ||
assert.Equal(t, "uds-releaser", repo) | ||
|
||
gitlabRemoteURL := "https://gitlab.com/defenseunicorns/uds-releaser.git" | ||
owner, repo, err = getGithubOwnerAndRepo(gitlabRemoteURL) | ||
assert.Error(t, err) | ||
assert.Empty(t, owner) | ||
assert.Empty(t, repo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters