mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-07 02:07:04 +00:00
feat(gitlab): support self-managed instances (#75)
Support self-managed gitlab instances by reading the GitLab CI environment variables.
This commit is contained in:
parent
55083f2a59
commit
89dc9e3fe8
2 changed files with 44 additions and 7 deletions
|
|
@ -73,6 +73,12 @@ include:
|
||||||
|
|
||||||
> You can set the `stage` input if you want to run `releaser-pleaser` during a different stage.
|
> You can set the `stage` input if you want to run `releaser-pleaser` during a different stage.
|
||||||
|
|
||||||
|
<div class="warning">
|
||||||
|
|
||||||
|
If you want to use `releaser-pleaser` on a self-managed GitLab instance, you need to mirror the GitLab.com component to your instance. See the official [GitLab documentation for details](https://docs.gitlab.com/ee/ci/components/#use-a-gitlabcom-component-in-a-self-managed-instance).
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
## 4. Release Merge Request
|
## 4. Release Merge Request
|
||||||
|
|
||||||
Once the `releaser-pleaser` job runs for the first time, you can check the logs to see what it did.
|
Once the `releaser-pleaser` job runs for the first time, you can check the logs to see what it did.
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,14 @@ const (
|
||||||
PRStateOpen = "opened"
|
PRStateOpen = "opened"
|
||||||
PRStateMerged = "merged"
|
PRStateMerged = "merged"
|
||||||
PRStateEventClose = "close"
|
PRStateEventClose = "close"
|
||||||
EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
|
|
||||||
EnvProjectPath = "CI_PROJECT_PATH"
|
EnvAPIToken = "GITLAB_TOKEN" // nolint:gosec // Not actually a hardcoded credential
|
||||||
|
|
||||||
|
// The following vars are from https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
|
||||||
|
|
||||||
|
EnvAPIURL = "CI_API_V4_URL"
|
||||||
|
EnvProjectURL = "CI_PROJECT_URL"
|
||||||
|
EnvProjectPath = "CI_PROJECT_PATH"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitLab struct {
|
type GitLab struct {
|
||||||
|
|
@ -36,19 +42,23 @@ type GitLab struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitLab) RepoURL() string {
|
func (g *GitLab) RepoURL() string {
|
||||||
|
if g.options.ProjectURL != "" {
|
||||||
|
return g.options.ProjectURL
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("https://gitlab.com/%s", g.options.Path)
|
return fmt.Sprintf("https://gitlab.com/%s", g.options.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitLab) CloneURL() string {
|
func (g *GitLab) CloneURL() string {
|
||||||
return fmt.Sprintf("https://gitlab.com/%s.git", g.options.Path)
|
return fmt.Sprintf("%s.git", g.RepoURL())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitLab) ReleaseURL(version string) string {
|
func (g *GitLab) ReleaseURL(version string) string {
|
||||||
return fmt.Sprintf("https://gitlab.com/%s/-/releases/%s", g.options.Path, version)
|
return fmt.Sprintf("%s/-/releases/%s", g.RepoURL(), version)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitLab) PullRequestURL(id int) string {
|
func (g *GitLab) PullRequestURL(id int) string {
|
||||||
return fmt.Sprintf("https://gitlab.com/%s/-/merge_requests/%d", g.options.Path, id)
|
return fmt.Sprintf("%s/-/merge_requests/%d", g.RepoURL(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GitLab) GitAuth() transport.AuthMethod {
|
func (g *GitLab) GitAuth() transport.AuthMethod {
|
||||||
|
|
@ -393,20 +403,41 @@ func gitlabMRToReleasePullRequest(pr *gitlab.MergeRequest) *releasepr.ReleasePul
|
||||||
|
|
||||||
func (g *Options) autodiscover() {
|
func (g *Options) autodiscover() {
|
||||||
// Read settings from GitLab-CI env vars
|
// Read settings from GitLab-CI env vars
|
||||||
|
if apiURL := os.Getenv(EnvAPIURL); apiURL != "" {
|
||||||
|
g.APIURL = apiURL
|
||||||
|
}
|
||||||
|
|
||||||
if apiToken := os.Getenv(EnvAPIToken); apiToken != "" {
|
if apiToken := os.Getenv(EnvAPIToken); apiToken != "" {
|
||||||
g.APIToken = apiToken
|
g.APIToken = apiToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if projectURL := os.Getenv(EnvProjectURL); projectURL != "" {
|
||||||
|
g.ProjectURL = projectURL
|
||||||
|
}
|
||||||
|
|
||||||
if projectPath := os.Getenv(EnvProjectPath); projectPath != "" {
|
if projectPath := os.Getenv(EnvProjectPath); projectPath != "" {
|
||||||
g.Path = projectPath
|
g.Path = projectPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Options) ClientOptions() []gitlab.ClientOptionFunc {
|
||||||
|
options := []gitlab.ClientOptionFunc{}
|
||||||
|
|
||||||
|
if g.APIURL != "" {
|
||||||
|
options = append(options, gitlab.WithBaseURL(g.APIURL))
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
forge.Options
|
forge.Options
|
||||||
|
|
||||||
Path string
|
ProjectURL string
|
||||||
|
Path string
|
||||||
|
|
||||||
|
APIURL string
|
||||||
APIToken string
|
APIToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -414,7 +445,7 @@ func New(log *slog.Logger, options *Options) (*GitLab, error) {
|
||||||
log = log.With("forge", "gitlab")
|
log = log.With("forge", "gitlab")
|
||||||
options.autodiscover()
|
options.autodiscover()
|
||||||
|
|
||||||
client, err := gitlab.NewClient(options.APIToken)
|
client, err := gitlab.NewClient(options.APIToken, options.ClientOptions()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue