From d42df7673f302202f58e8a3bbbdb39989a792d6b Mon Sep 17 00:00:00 2001 From: david may <1301201+wass3r@users.noreply.github.com> Date: Thu, 22 Feb 2024 13:13:19 -0600 Subject: [PATCH] fix: add missing approved by info and link PR commit (#769) --- cypress/fixtures/build_approved.json | 29 ++++++++++++++++++++ cypress/fixtures/build_pending_approval.json | 14 ++++++---- cypress/fixtures/builds_all.json | 24 ++++++++-------- cypress/fixtures/builds_pull.json | 20 +++++++------- cypress/integration/build.spec.js | 19 ++++++++++++- cypress/support/commands.js | 7 +++++ src/elm/Pages/Build/View.elm | 17 ++++++++++-- src/elm/Util.elm | 8 ++++++ 8 files changed, 106 insertions(+), 32 deletions(-) create mode 100644 cypress/fixtures/build_approved.json diff --git a/cypress/fixtures/build_approved.json b/cypress/fixtures/build_approved.json new file mode 100644 index 000000000..b1a72c788 --- /dev/null +++ b/cypress/fixtures/build_approved.json @@ -0,0 +1,29 @@ +{ + "id": 15, + "repo_id": 1, + "number": 9, + "parent": 1, + "event": "pull_request", + "status": "success", + "error": "", + "enqueued": 1572980376, + "created": 1572980376, + "started": 1572980375, + "finished": 1572980375, + "deploy": "", + "clone": "https://github.com/github/octocat.git", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", + "message": "fixing docker params", + "commit": "9b1d8bded6e992ab660eaee527c5e3232d0a2441", + "sender": "CookieCat", + "author": "CookieCat", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", + "host": "", + "runtime": "docker", + "distribution": "linux", + "approved_by": "gh0st", + "approved_at": 1572980375 +} diff --git a/cypress/fixtures/build_pending_approval.json b/cypress/fixtures/build_pending_approval.json index 35415d40b..63ad78c85 100644 --- a/cypress/fixtures/build_pending_approval.json +++ b/cypress/fixtures/build_pending_approval.json @@ -12,16 +12,18 @@ "finished": 1572980375, "deploy": "", "clone": "https://github.com/github/octocat.git", - "source": "https://github.com/github/octocat/commit/9b1d8bded6e992ab660eaee527c5e3232d0a2441", - "title": "push received from https://github.com/github/octocat", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", "message": "fixing docker params", "commit": "9b1d8bded6e992ab660eaee527c5e3232d0a2441", "sender": "CookieCat", "author": "CookieCat", - "branch": "infra", - "ref": "refs/heads/infra", - "base_ref": "", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", "host": "", "runtime": "docker", - "distribution": "linux" + "distribution": "linux", + "approved_by": "", + "approved_at": 0 } diff --git a/cypress/fixtures/builds_all.json b/cypress/fixtures/builds_all.json index 6ef4315ad..3e3ba57d4 100644 --- a/cypress/fixtures/builds_all.json +++ b/cypress/fixtures/builds_all.json @@ -32,7 +32,7 @@ "repo_id": 1, "number": 2, "parent": 1, - "event": "pull", + "event": "pull_request", "status": "success", "error": "unable to retrieve repo secret docker_username: unable to get secret repo/vela/ui/docker_username from native service: record not found", "enqueued": 1572980376, @@ -42,15 +42,15 @@ "deploy": "", "link": "/github/octocat/2", "clone": "https://github.com/github/octocat.git", - "source": "https://github.com/github/octocat/commit/7bd468e69a42e92153525288800692186bb0120b", - "title": "push received from https://github.com/github/octocat", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", "message": "fixing docker params", "commit": "7bd468e69a42e92153525288800692186bb0120b", "sender": "CookieCat", "author": "CookieCat", - "branch": "infra", - "ref": "refs/heads/infra", - "base_ref": "", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", "host": "", "runtime": "docker", "distribution": "linux" @@ -60,7 +60,7 @@ "repo_id": 1, "number": 3, "parent": 1, - "event": "pull", + "event": "pull_request", "status": "failure", "error": "unable to retrieve repo secret docker_username: unable to get secret repo/vela/ui/docker_username from native service: record not found", "enqueued": 1572980376, @@ -70,15 +70,15 @@ "deploy": "", "link": "/github/octocat/3", "clone": "https://github.com/github/octocat.git", - "source": "https://github.com/github/octocat/commit/7bd468e69a42e92153525288800692186bb0120b", - "title": "push received from https://github.com/github/octocat", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", "message": "fixing docker params", "commit": "7bd468e69a42e92153525288800692186bb0120b", "sender": "CookieCat", "author": "CookieCat", - "branch": "infra", - "ref": "refs/heads/infra", - "base_ref": "", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", "host": "", "runtime": "docker", "distribution": "linux" diff --git a/cypress/fixtures/builds_pull.json b/cypress/fixtures/builds_pull.json index c7069d570..fad7077a4 100644 --- a/cypress/fixtures/builds_pull.json +++ b/cypress/fixtures/builds_pull.json @@ -14,15 +14,15 @@ "deploy": "", "link": "/github/octocat/2", "clone": "https://github.com/github/octocat.git", - "source": "https://github.com/github/octocat/commit/7bd468e69a42e92153525288800692186bb0120b", - "title": "push received from https://github.com/github/octocat", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", "message": "fixing docker params", "commit": "7bd468e69a42e92153525288800692186bb0120b", "sender": "CookieCat", "author": "CookieCat", - "branch": "infra", - "ref": "refs/heads/infra", - "base_ref": "", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", "host": "", "runtime": "docker", "distribution": "linux" @@ -42,15 +42,15 @@ "deploy": "", "link": "/github/octocat/3", "clone": "https://github.com/github/octocat.git", - "source": "https://github.com/github/octocat/commit/7bd468e69a42e92153525288800692186bb0120b", - "title": "push received from https://github.com/github/octocat", + "source": "https://github.com/github/octocat/pull/42", + "title": "pull_request received from https://github.com/github/octocat", "message": "fixing docker params", "commit": "7bd468e69a42e92153525288800692186bb0120b", "sender": "CookieCat", "author": "CookieCat", - "branch": "infra", - "ref": "refs/heads/infra", - "base_ref": "", + "branch": "main", + "ref": "refs/pull/42/head", + "base_ref": "main", "host": "", "runtime": "docker", "distribution": "linux" diff --git a/cypress/integration/build.spec.js b/cypress/integration/build.spec.js index f296617c9..358b11ff7 100644 --- a/cypress/integration/build.spec.js +++ b/cypress/integration/build.spec.js @@ -212,7 +212,7 @@ context('Build', () => { method: 'POST', url: 'api/v1/repos/*/*/builds/*/approve', status: 200, - response: 'approved build github/octocat/8', + response: 'Successfully approved build github/octocat/8', }); cy.get('[data-test=approve-build]').as('approvedBuild'); }); @@ -230,6 +230,23 @@ context('Build', () => { }); }); + context('server stubbed Approved Build', () => { + beforeEach(() => { + cy.visit('/github/octocat/9'); + cy.server(); + }); + + it('should show who approved the build', () => { + cy.get('[data-test=git-info]').contains('approved by gh0st'); + }); + + it('sha should link to the commit in the PR', () => { + cy.get('[data-test=commit-link]') + .invoke('attr', 'href') + .should('contains', 'pull/42/commits/'); + }); + }); + context('visit running build', () => { beforeEach(() => { cy.visit('/github/octocat/1'); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ebf2326b3..31493b223 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -64,6 +64,7 @@ Cypress.Commands.add('stubBuild', () => { cy.fixture('build_error.json').as('errorBuild'); cy.fixture('build_canceled.json').as('cancelBuild'); cy.fixture('build_pending_approval.json').as('pendingApprovalBuild'); + cy.fixture('build_approved.json').as('approvedBuild'); cy.route({ method: 'GET', url: 'api/v1/repos/*/*/builds/1', @@ -112,6 +113,12 @@ Cypress.Commands.add('stubBuild', () => { status: 200, response: `@pendingApprovalBuild`, }); + cy.route({ + method: 'GET', + url: 'api/v1/repos/*/*/builds/9', + status: 200, + response: `@approvedBuild`, + }); }); Cypress.Commands.add('stubBuilds', () => { diff --git a/src/elm/Pages/Build/View.elm b/src/elm/Pages/Build/View.elm index 231598a04..e1109b497 100644 --- a/src/elm/Pages/Build/View.elm +++ b/src/elm/Pages/Build/View.elm @@ -302,7 +302,11 @@ viewPreview msgs openMenu showMenu now zone org repo showTimestamp build = , text (getNameFromRef build.ref) ] , text " (" - , a [ href build.source ] [ text <| Util.trimCommitHash build.commit ] + , a + [ href (Util.buildPRCommitURL build.source build.commit) + , Util.testAttribute "commit-link" + ] + [ text <| Util.trimCommitHash build.commit ] , text <| ")" ] @@ -391,6 +395,13 @@ viewPreview msgs openMenu showMenu now zone org repo showTimestamp build = statusClass = statusToClass build.status + + approvedBy = + if build.approved_at /= 0 && build.event == "pull_request" then + [ text <| " (approved by " ++ build.approved_by ++ ")" ] + + else + [] in div [ class "build-container", Util.testAttribute "build" ] [ div [ class "build", statusClass ] @@ -401,12 +412,12 @@ viewPreview msgs openMenu showMenu now zone org repo showTimestamp build = , div [ class "commit-msg" ] [ strong [] message ] ] , div [ class "row" ] - [ div [ class "git-info" ] + [ div [ class "git-info", Util.testAttribute "git-info" ] [ div [ class "commit" ] commit , text "on" , div [ class "branch" ] branch , text "by" - , div [ class "sender" ] sender + , div [ class "sender" ] (sender ++ approvedBy) ] , div [ class "time-info" ] [ div [ class "time-completed" ] diff --git a/src/elm/Util.elm b/src/elm/Util.elm index d1102c6ca..609bf148b 100644 --- a/src/elm/Util.elm +++ b/src/elm/Util.elm @@ -10,6 +10,7 @@ module Util exposing , base64Decode , boolToString , boolToYesNo + , buildPRCommitURL , buildRefURL , checkScheduleAllowlist , dispatch @@ -514,6 +515,13 @@ buildRefURL clone ref = String.dropRight 4 clone ++ "/tree/" ++ ref +{-| buildPRCommitURL : creates a direct link to a commit in a PR +-} +buildPRCommitURL : String -> String -> String +buildPRCommitURL source commit = + source ++ "/commits/" ++ commit + + {-| trimCommitHash : takes the first 7 characters of the full commit hash -} trimCommitHash : String -> String