Skip to content

Commit

Permalink
fixup! maintain compatibility with deprecated Global ID format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit committed Oct 30, 2021
1 parent 50d482f commit c5a7393
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions github/util_v4_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"encoding/base64"
"errors"

"github.com/shurcooL/githubv4"
Expand All @@ -15,6 +16,12 @@ func getRepositoryID(name string, meta interface{}) (githubv4.ID, error) {
return githubv4.ID(name), nil
}

// Interpret `name` as a legacy node ID
exists, _ = repositoryLegacyNodeIDExists(name, meta)
if exists {
return githubv4.ID(name), nil
}

// Could not find repo by node ID, interpret `name` as repo name
var query struct {
Repository struct {
Expand Down Expand Up @@ -59,3 +66,31 @@ func repositoryNodeIDExists(name string, meta interface{}) (bool, error) {

return query.Node.ID.(string) == name, nil
}

// Maintain compatibility with deprecated Global ID format
// https://github.blog/2021-02-10-new-global-id-format-coming-to-graphql/
func repositoryLegacyNodeIDExists(name string, meta interface{}) (bool, error) {
// Check if the name is a base 64 encoded node ID
_, err := base64.StdEncoding.DecodeString(name)
if err != nil {
return false, nil
}

// API check if node ID exists
var query struct {
Node struct {
ID githubv4.ID
} `graphql:"node(id:$id)"`
}
variables := map[string]interface{}{
"id": githubv4.ID(name),
}
ctx := context.Background()
client := meta.(*Owner).v4client
err = client.Query(ctx, &query, variables)
if err != nil {
return false, err
}

return query.Node.ID.(string) == name, nil
}

0 comments on commit c5a7393

Please sign in to comment.