Skip to content

🚀 CreatePackage

在 Packagist 上提交(注册)一个新的包,触发首次元数据抓取。

何时使用

🚀 新发布开源包后,将其提交到 Packagist 使其可被 composer require;🤖 自动化发布流水线中注册包;🔄 重新提交一个之前被删除的包。

签名

go
func (c *ComposerClient) CreatePackage(ctx context.Context, request *domain.PackageCreateRequest) (*domain.PackageCreateResponse, error)

参数

参数类型说明
ctxcontext.Context上下文,用于超时与取消
request*domain.PackageCreateRequest请求体,Repository 字段为仓库 URL

返回值

类型说明
结果*domain.PackageCreateResponseStatus 字段表示操作状态
错误error凭据缺失、HTTP 失败、非 200、JSON 解析失败时返回

对应端点(POST):https://packagist.org/api/create-package?username={u}&apiToken={t}

示例

go
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/scagogogo/composer-skills/pkg/client"
	"github.com/scagogogo/composer-skills/pkg/domain"
)

func main() {
	c := client.NewComposerClient(
		30*time.Second,
		client.WithAPICredentials("your-username", "your-api-token"),
	)

	req := &domain.PackageCreateRequest{
		Repository: "https://github.com/your-org/your-package",
	}
	resp, err := c.CreatePackage(context.Background(), req)
	if err != nil {
		log.Fatalf("创建失败: %v", err)
	}
	fmt.Printf("状态: %s\n", resp.Status)
}

进阶

凭据必需

本方法在调用前会检查 username / apiToken,若为空直接返回错误。请通过 WithAPICredentials 选项配置。凭据会以查询参数形式出现在 URL 中,注意日志脱敏。

基于 MIT 许可证发布