From 826d6a03727715ab4c0136ac658d91283dcf8f6e Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Tue, 1 Aug 2023 16:46:43 -0500 Subject: [PATCH 01/13] Add table formatting to status update and convert run URL line to table item Signed-off-by: Alec Hinh --- pkg/comment_formatter/tfc_status_update.go | 4 ++++ pkg/vcs/gitlab/client.go | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/comment_formatter/tfc_status_update.go b/pkg/comment_formatter/tfc_status_update.go index 73bdaf7..b991243 100644 --- a/pkg/comment_formatter/tfc_status_update.go +++ b/pkg/comment_formatter/tfc_status_update.go @@ -173,4 +173,8 @@ var needToApplyFullWorkSpace = ` const OLD_RUN_BLOCK = ` ### Previous TFC URLS + +| Run ID | Status | +| ------ | ------ | + %s` diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index bb5f1f7..1e1f2ce 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -129,9 +129,12 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) for _, note := range notes { if note.Author.Username == currentUser.Username { runUrl := utils.CaptureSubstring(note.Body, utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX) + // We need to get the Run ID which is the last portion of the run URL + runUrlSplit := strings.Split(runUrl, "/") + runID := runUrlSplit[len(runUrlSplit)-1] runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { - oldRunUrls = append(oldRunUrls, fmt.Sprintf("%s - %s", runUrl, utils.FormatStatus(runStatus))) + oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrl, utils.FormatStatus(runStatus))) } // Gitlab default sort is order by created by, so take the last match on this From 3eaa07a2ac024945b96384409d26a3d058c4197b Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Tue, 1 Aug 2023 16:47:00 -0500 Subject: [PATCH 02/13] Add markdown run table skeleton to formatter Signed-off-by: Alec Hinh --- pkg/utils/formatter.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/utils/formatter.go b/pkg/utils/formatter.go index 6963470..7ccfb0c 100644 --- a/pkg/utils/formatter.go +++ b/pkg/utils/formatter.go @@ -14,7 +14,10 @@ const ( ### Previous TFC Urls: -` + + +| Run ID | Status | +| ------ | ------ |` URL_RUN_GROUP_SUFFIX = "" URL_RUN_STATUS_PREFIX = "**Status**: " ) From a81d2a25d2a8db176bf0e4cef6f4b48b4e2fb283 Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Tue, 1 Aug 2023 17:41:03 -0500 Subject: [PATCH 03/13] Handle gitlab url parsing and tabling Signed-off-by: Alec Hinh --- pkg/vcs/gitlab/client.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 1e1f2ce..1e7bee1 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -129,12 +129,14 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) for _, note := range notes { if note.Author.Username == currentUser.Username { runUrl := utils.CaptureSubstring(note.Body, utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX) - // We need to get the Run ID which is the last portion of the run URL + runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]") runUrlSplit := strings.Split(runUrl, "/") - runID := runUrlSplit[len(runUrlSplit)-1] + runID := strings.Replace(runUrlSplit[len(runUrlSplit)-1], ") ", "", 1) runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { - oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrl, utils.FormatStatus(runStatus))) + log.Debug().Msgf("Run ID: %s", runID) + log.Debug().Msgf("Run URL Raw: %s", runUrlRaw) + oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus))) } // Gitlab default sort is order by created by, so take the last match on this From 225c4395ba9e39b496b9057950e06937347681cb Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:15:23 -0500 Subject: [PATCH 04/13] Correct line breaking and formatting for gitlab MRs Signed-off-by: Alec Hinh --- pkg/comment_formatter/tfc_status_update.go | 1 - pkg/vcs/gitlab/client.go | 14 +++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/comment_formatter/tfc_status_update.go b/pkg/comment_formatter/tfc_status_update.go index b991243..213c667 100644 --- a/pkg/comment_formatter/tfc_status_update.go +++ b/pkg/comment_formatter/tfc_status_update.go @@ -176,5 +176,4 @@ const OLD_RUN_BLOCK = ` | Run ID | Status | | ------ | ------ | - %s` diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 1e7bee1..89f69ad 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -129,13 +129,15 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) for _, note := range notes { if note.Author.Username == currentUser.Username { runUrl := utils.CaptureSubstring(note.Body, utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX) + // We scrape the run URLs from the previous MR comments. + // Since they are hyperlinked in markdown format, we need to extract the URL + // without the markdown artifacts. runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]") - runUrlSplit := strings.Split(runUrl, "/") - runID := strings.Replace(runUrlSplit[len(runUrlSplit)-1], ") ", "", 1) + runUrlSplit := strings.Split(runUrlRaw, "/") + // The run ID is the last part of the run URL, and it looks like run-abcd12345... + runID := runUrlSplit[len(runUrlSplit)-1] runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { - log.Debug().Msgf("Run ID: %s", runID) - log.Debug().Msgf("Run URL Raw: %s", runUrlRaw) oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus))) } @@ -143,6 +145,8 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) oldRunBlockTest := utils.CaptureSubstring(note.Body, utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX) if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest + } else { + oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && note.ID != rootNoteID { log.Debug().Str("projectID", project).Int("mrIID", mrIID).Msgf("deleting note %d", note.ID) @@ -156,7 +160,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) // Add new urls into block if len(oldRunUrls) > 0 { - return fmt.Sprintf("%s\n%s\n%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil + return fmt.Sprintf("%s%s%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil } return oldRunBlock, nil } From bb940222d3fe2dfafaeba7f7332ed0ebe8eca3af Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:16:33 -0500 Subject: [PATCH 05/13] Replace airplane arriving emoji with inbox tray for imports icon Signed-off-by: Alec Hinh --- pkg/terraform_plan/templates/plan_output.tpl | 2 +- .../testdata/TestPresentPlanChangesAsMarkdown/basic.md | 2 +- .../testdata/TestPresentPlanChangesAsMarkdown/destroy-create.md | 2 +- .../testdata/TestPresentPlanChangesAsMarkdown/import.md | 2 +- .../testdata/TestPresentPlanChangesAsMarkdown/replace.md | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/terraform_plan/templates/plan_output.tpl b/pkg/terraform_plan/templates/plan_output.tpl index 6fbd5fb..9561038 100644 --- a/pkg/terraform_plan/templates/plan_output.tpl +++ b/pkg/terraform_plan/templates/plan_output.tpl @@ -5,7 +5,7 @@ {{- end}} {{ end }} -:airplane_arriving: Imports: {{.ImportCount}} +:inbox_tray: Imports: {{.ImportCount}}
    {{- range .Imports}}
  • {{ . }}
  • diff --git a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/basic.md b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/basic.md index d7c7ffb..134b56a 100644 --- a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/basic.md +++ b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/basic.md @@ -1,6 +1,6 @@ -:airplane_arriving: Imports: 0 +:inbox_tray: Imports: 0
    diff --git a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/destroy-create.md b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/destroy-create.md index 6783fcd..5039e93 100644 --- a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/destroy-create.md +++ b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/destroy-create.md @@ -1,6 +1,6 @@ -:airplane_arriving: Imports: 0 +:inbox_tray: Imports: 0
    diff --git a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/import.md b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/import.md index 8614612..0d19555 100644 --- a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/import.md +++ b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/import.md @@ -1,6 +1,6 @@ -:airplane_arriving: Imports: 1 +:inbox_tray: Imports: 1
    • random_integer.import
    diff --git a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/replace.md b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/replace.md index 2faee2d..b8ee7a2 100644 --- a/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/replace.md +++ b/pkg/terraform_plan/testdata/TestPresentPlanChangesAsMarkdown/replace.md @@ -1,6 +1,6 @@ -:airplane_arriving: Imports: 0 +:inbox_tray: Imports: 0
    From 3f33e502e6aa3cb8f5ac35df1474ab5830197bee Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:24:35 -0500 Subject: [PATCH 06/13] Port over markdown tabling to github client Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 5b47293..1c420d9 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -84,16 +84,25 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st // If this token user made the comment, and we're making a new comment, pick the TFC url out of the body and delete the comment if comment.GetUser().GetLogin() == currentUser.GetLogin() { runUrl := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_PREFIX, utils.URL_RUN_SUFFIX) + // We scrape the run URLs from the previous MR comments. + // Since they are hyperlinked in markdown format, we need to extract the URL + // without the markdown artifacts. + runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]") + runUrlSplit := strings.Split(runUrlRaw, "/") + // The run ID is the last part of the run URL, and it looks like run-abcd12345... + runID := runUrlSplit[len(runUrlSplit)-1] runStatus := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { // Example: - ✅ Applied - oldRunUrls = append(oldRunUrls, fmt.Sprintf("%s - %s", runUrl, runStatus)) + oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrlRaw, runStatus)) } // Github orders comments from earliest -> latest via ID, so we check each comment and take the last match on an "old url" block oldRunBlockTest := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX) if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest + } else { + oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && comment.GetID() != int64(rootCommentID) { @@ -109,7 +118,7 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st // If we found any old run urls, return them formatted if len(oldRunUrls) > 0 { // Try and find any exisitng groupings of old urls, else make a new one - return fmt.Sprintf("%s\n%s\n%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil + return fmt.Sprintf("%s%s%s\n%s", utils.URL_RUN_GROUP_PREFIX, oldRunBlock, strings.Join(oldRunUrls, "\n"), utils.URL_RUN_GROUP_SUFFIX), nil } return oldRunBlock, nil } From 2cbf9d12226010ec64250f9b7f563a9d74624004 Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:43:10 -0500 Subject: [PATCH 07/13] Add field for created at timestamp Signed-off-by: Alec Hinh --- pkg/comment_formatter/tfc_status_update.go | 4 ++-- pkg/utils/formatter.go | 4 ++-- pkg/vcs/gitlab/client.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/comment_formatter/tfc_status_update.go b/pkg/comment_formatter/tfc_status_update.go index 213c667..3e70cc5 100644 --- a/pkg/comment_formatter/tfc_status_update.go +++ b/pkg/comment_formatter/tfc_status_update.go @@ -174,6 +174,6 @@ var needToApplyFullWorkSpace = ` const OLD_RUN_BLOCK = ` ### Previous TFC URLS -| Run ID | Status | -| ------ | ------ | +| Run ID | Status | Created at | +| ------ | ------ | ---------- | %s` diff --git a/pkg/utils/formatter.go b/pkg/utils/formatter.go index 7ccfb0c..47759f0 100644 --- a/pkg/utils/formatter.go +++ b/pkg/utils/formatter.go @@ -16,8 +16,8 @@ const ( -| Run ID | Status | -| ------ | ------ |` +| Run ID | Status | Created at | +| ------ | ------ | ---------- |` URL_RUN_GROUP_SUFFIX = "" URL_RUN_STATUS_PREFIX = "**Status**: " ) diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 89f69ad..1d3bb1d 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -138,7 +138,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) runID := runUrlSplit[len(runUrlSplit)-1] runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { - oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus))) + oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus), note.CreatedAt)) } // Gitlab default sort is order by created by, so take the last match on this From c48bc6347eef069d14160436733b70ed62001cf9 Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:46:53 -0500 Subject: [PATCH 08/13] Extend github comment to also include timestamp Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 1c420d9..9e7b685 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -93,8 +93,8 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st runID := runUrlSplit[len(runUrlSplit)-1] runStatus := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { - // Example: - ✅ Applied - oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|", runID, runUrlRaw, runStatus)) + // Example: |[]()|✅ Applied|2023-08-02 15:41:48.82 +0000 UTC| + oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|%s|", runID, runUrlRaw, runStatus, comment.CreatedAt)) } // Github orders comments from earliest -> latest via ID, so we check each comment and take the last match on an "old url" block From a592d7887d9db854ae334e108fa79f3bcef7e10c Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:57:12 -0500 Subject: [PATCH 09/13] Comment about the oldrunblock newline Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 1 + pkg/vcs/gitlab/client.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 9e7b685..788de4f 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -102,6 +102,7 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest } else { + // Add a new line for the first table entry so that markdown tabling can properly begin oldRunBlock = "\n" } diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 1d3bb1d..8813d8d 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -146,6 +146,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest } else { + // Add a new line for the first table entry so that markdown tabling can properly begin. oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && note.ID != rootNoteID { From 5488319ed28e7d8b9458ea707b4203bf438f84df Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Wed, 2 Aug 2023 10:59:09 -0500 Subject: [PATCH 10/13] Comment about the oldrunblock newline (for gitlabb) Signed-off-by: Alec Hinh --- pkg/vcs/gitlab/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 8813d8d..7447bf4 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -146,7 +146,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest } else { - // Add a new line for the first table entry so that markdown tabling can properly begin. + // Add a new line for the first table entry so that markdown tabling can properly begin oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && note.ID != rootNoteID { From 4aee5a43a25a9884b32f20fe27f8c2487fa0dc76 Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Mon, 7 Aug 2023 10:10:55 -0500 Subject: [PATCH 11/13] Add fallback behavior in the event runURLSplit is 0 in length Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 12 +++++++++++- pkg/vcs/gitlab/client.go | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 788de4f..568506b 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -90,7 +90,17 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]") runUrlSplit := strings.Split(runUrlRaw, "/") // The run ID is the last part of the run URL, and it looks like run-abcd12345... - runID := runUrlSplit[len(runUrlSplit)-1] + runID := "" + if len(runUrlSplit) > 0 { + runID = runUrlSplit[len(runUrlSplit)-1] + } else { + // If the URL split slice doesn't contain anything for any reason + // We set the ID and URL to the run URL as a fallback (as it was originally scraped) + // It'll appear like this in markdown + // [https://app.terraform.io/...](https://app.terraform.io/...) + runID = runUrl + runUrlRaw = runUrl + } runStatus := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { // Example: |[]()|✅ Applied|2023-08-02 15:41:48.82 +0000 UTC| diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 7447bf4..cbc9ff5 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -135,7 +135,17 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) runUrlRaw := utils.CaptureSubstring(runUrl, "[", "]") runUrlSplit := strings.Split(runUrlRaw, "/") // The run ID is the last part of the run URL, and it looks like run-abcd12345... - runID := runUrlSplit[len(runUrlSplit)-1] + runID := "" + if len(runUrlSplit) > 0 { + runID = runUrlSplit[len(runUrlSplit)-1] + } else { + // If the URL split slice doesn't contain anything for any reason + // We set the ID and URL to the run URL as a fallback (as it was originally scraped) + // It'll appear like this in markdown + // [https://app.terraform.io/...](https://app.terraform.io/...) + runID = runUrl + runUrlRaw = runUrl + } runStatus := utils.CaptureSubstring(note.Body, utils.URL_RUN_STATUS_PREFIX, utils.URL_RUN_SUFFIX) if runUrl != "" && runStatus != "" { oldRunUrls = append(oldRunUrls, fmt.Sprintf("|[%s](%s)|%s|%s|", runID, runUrlRaw, utils.FormatStatus(runStatus), note.CreatedAt)) From f09701df7c5a22e9a9be3fb311671a9470cd2442 Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Mon, 7 Aug 2023 10:12:18 -0500 Subject: [PATCH 12/13] Move oldrunblock logic to not execute as an else condition Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 5 ++--- pkg/vcs/gitlab/client.go | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 568506b..14721cf 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -109,11 +109,10 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st // Github orders comments from earliest -> latest via ID, so we check each comment and take the last match on an "old url" block oldRunBlockTest := utils.CaptureSubstring(comment.GetBody(), utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX) + // Add a new line for the first table entry so that markdown tabling can properly begin + oldRunBlock = "\n" if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest - } else { - // Add a new line for the first table entry so that markdown tabling can properly begin - oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && comment.GetID() != int64(rootCommentID) { diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index cbc9ff5..96c9a97 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -153,11 +153,10 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) // Gitlab default sort is order by created by, so take the last match on this oldRunBlockTest := utils.CaptureSubstring(note.Body, utils.URL_RUN_GROUP_PREFIX, utils.URL_RUN_GROUP_SUFFIX) + // Add a new line for the first table entry so that markdown tabling can properly begin + oldRunBlock = "\n" if oldRunBlockTest != "" { oldRunBlock = oldRunBlockTest - } else { - // Add a new line for the first table entry so that markdown tabling can properly begin - oldRunBlock = "\n" } if os.Getenv("TFBUDDY_DELETE_OLD_COMMENTS") != "" && note.ID != rootNoteID { log.Debug().Str("projectID", project).Int("mrIID", mrIID).Msgf("deleting note %d", note.ID) From 9e80812970bd10d23108bce470cc5984bb754c9f Mon Sep 17 00:00:00 2001 From: Alec Hinh Date: Mon, 7 Aug 2023 10:16:59 -0500 Subject: [PATCH 13/13] Add log warn message about possibly malformed run URLs Signed-off-by: Alec Hinh --- pkg/vcs/github/client.go | 1 + pkg/vcs/gitlab/client.go | 1 + 2 files changed, 2 insertions(+) diff --git a/pkg/vcs/github/client.go b/pkg/vcs/github/client.go index 14721cf..e1a4b69 100644 --- a/pkg/vcs/github/client.go +++ b/pkg/vcs/github/client.go @@ -98,6 +98,7 @@ func (c *Client) GetOldRunUrls(prID int, fullName string, rootCommentID int) (st // We set the ID and URL to the run URL as a fallback (as it was originally scraped) // It'll appear like this in markdown // [https://app.terraform.io/...](https://app.terraform.io/...) + log.Warn().Msg("Unable to obtain Terraform cloud run ID. The run URL(s) on the previous comments may be malformed.") runID = runUrl runUrlRaw = runUrl } diff --git a/pkg/vcs/gitlab/client.go b/pkg/vcs/gitlab/client.go index 96c9a97..eeafc8f 100644 --- a/pkg/vcs/gitlab/client.go +++ b/pkg/vcs/gitlab/client.go @@ -143,6 +143,7 @@ func (c *GitlabClient) GetOldRunUrls(mrIID int, project string, rootNoteID int) // We set the ID and URL to the run URL as a fallback (as it was originally scraped) // It'll appear like this in markdown // [https://app.terraform.io/...](https://app.terraform.io/...) + log.Warn().Msg("Unable to obtain Terraform cloud run ID. The run URL(s) on the previous comments may be malformed.") runID = runUrl runUrlRaw = runUrl }