Skip to content

Commit

Permalink
feat(project ci): allow auto configure of auth.json for Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Feb 21, 2024
1 parent 3de95b9 commit 4f204f9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/project/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,20 @@ var projectCI = &cobra.Command{
composerFlags = append(composerFlags, "--no-dev")
}

token, err := prepareComposerAuth(cmd.Context())

if err != nil {
return err
}

composer := phpexec.ComposerCommand(cmd.Context(), composerFlags...)
composer.Dir = args[0]
composer.Stdin = os.Stdin
composer.Stdout = os.Stdout
composer.Stderr = os.Stderr
composer.Env = append(os.Environ(),
"COMPOSER_AUTH="+token,
)

if err := composer.Run(); err != nil {
return err
Expand Down Expand Up @@ -172,6 +181,50 @@ var projectCI = &cobra.Command{
},
}

type ComposerAuth struct {
HTTPBasicAuth *interface{} `json:"http-basic,omitempty"`
BearerAuth map[string]string `json:"bearer,omitempty"`
GitlabAuth *interface{} `json:"gitlab-token,omitempty"`
GithubOAuth *interface{} `json:"github-oauth,omitempty"`
BitbucketOauth *interface{} `json:"bitbucket-oauth,omitempty"`
}

func prepareComposerAuth(ctx context.Context) (string, error) {
composerToken := os.Getenv("SHOPWARE_PACKAGES_TOKEN")

if composerToken == "" {
return "", nil
}

logging.FromContext(ctx).Infof("Setting up composer auth for packages.shopware.com")

composerAuth := os.Getenv("COMPOSER_AUTH")

var auth ComposerAuth

if composerAuth == "" {
auth = ComposerAuth{}
} else {
if err := json.Unmarshal([]byte(composerAuth), &auth); err != nil {
return "", err
}
}

if auth.BearerAuth == nil {
auth.BearerAuth = make(map[string]string)
}

auth.BearerAuth["packages.shopware.com"] = composerToken

data, err := json.Marshal(auth)

if err != nil {
return "", err
}

return string(data), nil
}

func init() {
projectRootCmd.AddCommand(projectCI)
projectCI.PersistentFlags().Bool("with-dev-dependencies", false, "Install dev dependencies")
Expand Down
3 changes: 3 additions & 0 deletions wiki/docs/commands/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ Flags:

- `--with-dev-dependencies` - Install dev dependencies

You can set `SHOPWARE_PACKAGES_TOKEN` as environment variable with the Shopware Composer Registry token,
to pass it to the composer command.

What that command does:

- Installs all composer dependencies
Expand Down

0 comments on commit 4f204f9

Please sign in to comment.