Skip to content

BUG: Regression in version 0.53 in Dependabot.CreateOrUpdateOrgSecret #2816

@frankywahl

Description

@frankywahl

Hello,

I believe there was a regression in #2794.

Documentation of organization secrets expects a string but we're now sending integers.

This in turn causes a failure when calling the API.

I think the issue lies in that "setting" repository ids is done via integers on the API (REF), while creating or updating a secrets expects repository ids to be a string (REF). So they cannot have the same underlying type in the client as long as the API expected different types

Sample Go Code
package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"log"
	"os"

	"github.com/google/go-github/v53/github"
	"golang.org/x/crypto/nacl/box"
	"golang.org/x/oauth2"
)

func main() {
	ctx := context.Background()
	if err := run(ctx); err != nil {
		log.Fatal(err)
	}
}

func run(ctx context.Context) error {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: os.Getenv("GITHUB_API_TOKEN")},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := github.NewClient(tc)
	org := os.Getenv("GITHUB_OWNER")

	pubKey, _, err := client.Dependabot.GetOrgPublicKey(ctx, org)
	if err != nil {
		return err
	}

	enc, err := encryptPlaintext("SECRET", *pubKey.Key)
	if err != nil {
		return err
	}

	_, err = client.Dependabot.CreateOrUpdateOrgSecret(ctx, org, &github.DependabotEncryptedSecret{
		Name:                  "EXAMPLE",
		KeyID:                 *pubKey.KeyID,
		EncryptedValue:        base64.StdEncoding.EncodeToString(enc),
		SelectedRepositoryIDs: []int64{123456789},
		Visibility:            "selected",
	})

	return err
}

func encryptPlaintext(plaintext, publicKeyB64 string) ([]byte, error) {
	publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKeyB64)
	if err != nil {
		return nil, err
	}

	var publicKeyBytes32 [32]byte
	copiedLen := copy(publicKeyBytes32[:], publicKeyBytes)
	if copiedLen == 0 {
		return nil, fmt.Errorf("could not convert publicKey to bytes")
	}

	plaintextBytes := []byte(plaintext)
	var encryptedBytes []byte

	cipherText, err := box.SealAnonymous(encryptedBytes, plaintextBytes, &publicKeyBytes32, nil)
	if err != nil {
		return nil, err
	}

	return cipherText, nil
}
Sample Output
2023/06/22 09:52:26 PUT https://api.github.com/orgs/<my-org>/dependabot/secrets/EXAMPLE: 422 Invalid request.

Invalid property /selected_repository_ids/0: `123456789` is not of type `string`. []
exit status 1

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions