From f35a9ffa469bae754caba4ff126ac01837a18cc9 Mon Sep 17 00:00:00 2001 From: Hampus Lidin Date: Mon, 5 Apr 2021 13:30:13 +0200 Subject: [PATCH 1/3] Fix incorrect owner and repo name for organization --- main.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index e04660d..b09756c 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,24 @@ func main() { parentResource, parentName, err := projectParentName(url) errCheck(err) + + var pjID int64 + var pjName string + var pjOwner string + if pjType == "repository" { + pjID, err = projectIDByRepo(ctx, client, url, parentResource, parentName) + pjName = parentName + pjOwner = parentResource + errCheck(err) + } else if pjType == "organization" { + pjID, pjName, err = projectIDAndNameByOrg(ctx, client, url, parentName) + pjOwner = parentName + errCheck(err) + } else if pjType == "user" { + errorLog(errors.New("User project is not supported yet")) + os.Exit(1) + } + infoLog("Project type:%s\n", pjType) // eventID stores issue ID or pull-request ID var eventID int64 @@ -48,7 +66,7 @@ func main() { payload := issueEventPayload() eventID, err = extractIssueID(payload) errCheck(err) - projectCards, err = getProjectCardsFromIssue(ctx, client, payload.Issue, parentResource, parentName) + projectCards, err = getProjectCardsFromIssue(ctx, client, payload.Issue, pjOwner, pjName) errCheck(err) } else if eventName == "pull_request" { payload := pullRequestEventPayload() @@ -59,19 +77,6 @@ func main() { infoLog("Payload for %s extract correctly", eventName) debugLog("Target event ID: %d\n", eventID) - var pjID int64 - if pjType == "repository" { - pjID, err = projectIDByRepo(ctx, client, url, parentResource, parentName) - errCheck(err) - } else if pjType == "organization" { - pjID, err = projectIDByOrg(ctx, client, url, parentName) - errCheck(err) - } else if pjType == "user" { - errorLog(errors.New("User project is not supported yet")) - os.Exit(1) - } - infoLog("Project type:%s\n", pjType) - pjColumn := os.Getenv("GITHUB_PROJECT_COLUMN_NAME") if pjColumn == "" { errorLog(errors.New("Environment variable PROJECT_COLUMN_NAME is not defined in your workflow file")) @@ -188,8 +193,9 @@ func projectIDByRepo(ctx context.Context, client *github.Client, url, owner, rep return projectID, nil } -func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) (int64, error) { +func projectIDAndNameByOrg(ctx context.Context, client *github.Client, url, org string) (int64, string, error) { var projectID int64 + var projectName string opt := &github.ProjectListOptions{ ListOptions: github.ListOptions{ PerPage: 200, @@ -205,7 +211,8 @@ func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) for _, project := range projects { if project.GetHTMLURL() == url { projectID = project.GetID() - infoLog("Project Name: " + project.GetName()) + projectName = project.GetName() + infoLog("Project Name: " + projectName) infoLog("Project ID: %d\n", projectID) break } @@ -214,7 +221,7 @@ func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) if projectID == 0 { return 0, errors.New("No such a project url: " + url) } - return projectID, nil + return projectID, projectName, nil } func projectColumnID(ctx context.Context, client *github.Client, pjID int64, pjColumn string) (int64, error) { From 9f03a9651928e7c576338aef2f82280f477860dc Mon Sep 17 00:00:00 2001 From: Hampus Lidin Date: Mon, 5 Apr 2021 14:05:10 +0200 Subject: [PATCH 2/3] Retrieve repo owner and name from issue payload --- main.go | 59 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index b09756c..a0d3540 100644 --- a/main.go +++ b/main.go @@ -41,32 +41,19 @@ func main() { parentResource, parentName, err := projectParentName(url) errCheck(err) - var pjID int64 - var pjName string - var pjOwner string - if pjType == "repository" { - pjID, err = projectIDByRepo(ctx, client, url, parentResource, parentName) - pjName = parentName - pjOwner = parentResource - errCheck(err) - } else if pjType == "organization" { - pjID, pjName, err = projectIDAndNameByOrg(ctx, client, url, parentName) - pjOwner = parentName - errCheck(err) - } else if pjType == "user" { - errorLog(errors.New("User project is not supported yet")) - os.Exit(1) - } - infoLog("Project type:%s\n", pjType) - // eventID stores issue ID or pull-request ID var eventID int64 var projectCards []*github.ProjectCard if eventName == "issues" { payload := issueEventPayload() + eventID, err = extractIssueID(payload) errCheck(err) - projectCards, err = getProjectCardsFromIssue(ctx, client, payload.Issue, pjOwner, pjName) + + repoOwner, repoName, err := repoOwnerAndName(payload.Issue.RepositoryUrl) + errCheck(err) + + projectCards, err = getProjectCardsFromIssue(ctx, client, payload.Issue, repoOwner, repoName) errCheck(err) } else if eventName == "pull_request" { payload := pullRequestEventPayload() @@ -76,6 +63,20 @@ func main() { infoLog("Payload for %s extract correctly", eventName) debugLog("Target event ID: %d\n", eventID) + + var pjID int64 + if pjType == "repository" { + pjID, err = projectIDByRepo(ctx, client, url, parentResource, parentName) + errCheck(err) + } else if pjType == "organization" { + pjID, err = projectIDByOrg(ctx, client, url, parentName) + errCheck(err) + } else if pjType == "user" { + errorLog(errors.New("User project is not supported yet")) + os.Exit(1) + } + infoLog("Project type:%s\n", pjType) + pjColumn := os.Getenv("GITHUB_PROJECT_COLUMN_NAME") if pjColumn == "" { @@ -165,6 +166,21 @@ func projectParentName(rawURL string) (string, string, error) { return path[1], path[2], nil } +func repoOwnerAndName(rawApiURL string) (string, string, error) { + u, err := url.Parse(rawApiURL) + if err != nil { + return "", "", errors.New("Failed to parse URL") + } + // A "Get Repository API" URL must be formed with https://api.github.com/repos/REPO_OWNER/REPO_NAME style. + // (e.g) + // - (repo) https://github.com/username/reponame/projects/1 + // - (org) https://github.com/orgname/reponame/1 + // Thus, organization and user repository owner and name can be extracted from given project URL as REPO_OWNER + // and REPO_NAME, respectively. + path := strings.Split(u.Path, "/") + return path[2], path[3], nil +} + func projectIDByRepo(ctx context.Context, client *github.Client, url, owner, repo string) (int64, error) { var projectID int64 projects, res, err := client.Repositories.ListProjects(ctx, owner, repo, nil) @@ -193,7 +209,7 @@ func projectIDByRepo(ctx context.Context, client *github.Client, url, owner, rep return projectID, nil } -func projectIDAndNameByOrg(ctx context.Context, client *github.Client, url, org string) (int64, string, error) { +func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) (int64, error) { var projectID int64 var projectName string opt := &github.ProjectListOptions{ @@ -211,7 +227,6 @@ func projectIDAndNameByOrg(ctx context.Context, client *github.Client, url, org for _, project := range projects { if project.GetHTMLURL() == url { projectID = project.GetID() - projectName = project.GetName() infoLog("Project Name: " + projectName) infoLog("Project ID: %d\n", projectID) break @@ -221,7 +236,7 @@ func projectIDAndNameByOrg(ctx context.Context, client *github.Client, url, org if projectID == 0 { return 0, errors.New("No such a project url: " + url) } - return projectID, projectName, nil + return projectID, nil } func projectColumnID(ctx context.Context, client *github.Client, pjID int64, pjColumn string) (int64, error) { From 1a46f1f0ae6156714076e89191caf72736a7b004 Mon Sep 17 00:00:00 2001 From: Hampus Lidin Date: Mon, 5 Apr 2021 14:06:38 +0200 Subject: [PATCH 3/3] Remove residue code from recent refactor --- main.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a0d3540..bc474e4 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { parentResource, parentName, err := projectParentName(url) errCheck(err) - + // eventID stores issue ID or pull-request ID var eventID int64 var projectCards []*github.ProjectCard @@ -63,7 +63,7 @@ func main() { infoLog("Payload for %s extract correctly", eventName) debugLog("Target event ID: %d\n", eventID) - + var pjID int64 if pjType == "repository" { pjID, err = projectIDByRepo(ctx, client, url, parentResource, parentName) @@ -77,7 +77,6 @@ func main() { } infoLog("Project type:%s\n", pjType) - pjColumn := os.Getenv("GITHUB_PROJECT_COLUMN_NAME") if pjColumn == "" { errorLog(errors.New("Environment variable PROJECT_COLUMN_NAME is not defined in your workflow file")) @@ -211,7 +210,6 @@ func projectIDByRepo(ctx context.Context, client *github.Client, url, owner, rep func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) (int64, error) { var projectID int64 - var projectName string opt := &github.ProjectListOptions{ ListOptions: github.ListOptions{ PerPage: 200, @@ -227,7 +225,6 @@ func projectIDByOrg(ctx context.Context, client *github.Client, url, org string) for _, project := range projects { if project.GetHTMLURL() == url { projectID = project.GetID() - infoLog("Project Name: " + projectName) infoLog("Project ID: %d\n", projectID) break }