From 8ef0581a6f0f7cb0f805f332a17bf743d94ff516 Mon Sep 17 00:00:00 2001 From: Joshua May Date: Sat, 6 Apr 2019 19:19:59 +0200 Subject: [PATCH] Generally the pulling/reading works, to the point of commenting --- source/dsl/GitLabDSL.ts | 28 +++++++++++++++++++++++++++- source/platforms/GitLab.ts | 23 +++++++++++++++++++---- source/platforms/gitlab/GitLabAPI.ts | 26 +++++++++++++++++++++----- source/runner/jsonToDSL.ts | 3 --- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/source/dsl/GitLabDSL.ts b/source/dsl/GitLabDSL.ts index f863abdc2..70521b1dd 100644 --- a/source/dsl/GitLabDSL.ts +++ b/source/dsl/GitLabDSL.ts @@ -7,8 +7,9 @@ export interface GitLabDSL { metadata: RepoMetaData // issues: any[] mr: GitLabMR - // commits: GitLabMRCommit[] + commits: GitLabMRCommit[] // comments: any[] + utils: {} } // --- @@ -23,6 +24,31 @@ export interface GitLabUser { web_url: string } +export interface GitLabUserProfile extends GitLabUser { + created_at: string + bio: string | null + location: string | null + public_email: string + skype: string + linkedin: string + twitter: string + website_url: string + organization: string + last_sign_in_at: string + confirmed_at: string + theme_id: number + last_activity_on: string + color_scheme_id: number + projects_limit: number + current_sign_in_at: string + identities: [{ provider: string; extern_uid: string }] + can_create_group: boolean + can_create_project: boolean + two_factor_enabled: boolean + external: boolean + private_profile: boolean +} + export interface GitLabMRBase { /** */ id: number diff --git a/source/platforms/GitLab.ts b/source/platforms/GitLab.ts index d6033ad16..804ec42b3 100644 --- a/source/platforms/GitLab.ts +++ b/source/platforms/GitLab.ts @@ -5,6 +5,9 @@ import { GitDSL, GitJSONDSL } from "../dsl/GitDSL" import { GitCommit } from "../dsl/Commit" import { GitLabDSL } from "../dsl/GitLabDSL" +import { debug } from "../debug" +const d = debug("GitLab") + class GitLab implements Platform { public readonly name: string @@ -19,7 +22,7 @@ class GitLab implements Platform { // returns the `danger.gitlab` object getPlatformReviewDSLRepresentation = async (): Promise => { const mr = await this.getReviewInfo() - // const commits = await this.api.getMergeRequestCommits() + const commits = await this.api.getMergeRequestCommits() // const comments: any[] = [] //await this.api.getMergeRequestComments() // const activities = {} //await this.api.getPullRequestActivities() // const issues: any[] = [] //await this.api.getIssues() @@ -28,8 +31,9 @@ class GitLab implements Platform { metadata: this.api.repoMetadata, // issues, mr, - // commits, + commits, // comments, + utils: {}, } } @@ -80,15 +84,19 @@ class GitLab implements Platform { } } - getInlineComments = async (_: string): Promise => { + getInlineComments = async (dangerID: string): Promise => { + const dangerUserID = (await this.api.getUser()).id + const comments = (await this.api.getMergeRequestInlineComments()).map(comment => { return { id: `${comment.id}`, body: comment.body, - ownedByDanger: comment.author.id === 1, + ownedByDanger: comment.author.id === dangerUserID && comment.body.includes(dangerID), } }) + console.log({ comments }) + return comments } @@ -101,30 +109,37 @@ class GitLab implements Platform { } updateOrCreateComment = async (_dangerID: string, _newComment: string): Promise => { + d("updateOrCreateComment", { _dangerID, _newComment }) return "https://gitlab.com/group/project/merge_requests/154#note_132143425" } createComment = async (_comment: string): Promise => { + d("createComment", { _comment }) return true } createInlineComment = async (_git: GitDSL, _comment: string, _path: string, _line: number): Promise => { + d("createInlineComment", { _comment, _path, _line }) return true } updateInlineComment = async (_comment: string, _commentId: string): Promise => { + d("updateInlineComment", { _comment, _commentId }) return true } deleteInlineComment = async (_id: string): Promise => { + d("deleteInlineComment", { _id }) return true } deleteMainComment = async (): Promise => { + d("deleteMainComment", {}) return true } updateStatus = async (): Promise => { + d("updateStatus", {}) return true } diff --git a/source/platforms/gitlab/GitLabAPI.ts b/source/platforms/gitlab/GitLabAPI.ts index da0535c44..7e2523fd1 100644 --- a/source/platforms/gitlab/GitLabAPI.ts +++ b/source/platforms/gitlab/GitLabAPI.ts @@ -7,6 +7,7 @@ import { GitLabMRCommit, GitLabInlineComment, GitLabComment, + GitLabUserProfile, } from "../../dsl/GitLabDSL" import { Gitlab } from "gitlab" @@ -18,6 +19,7 @@ class GitLabAPI { fetch: typeof fetch // private mr: GitLabMR | undefined + // private user: GitLabUserProfile | undefined // https://github.com/jdalrymple/node-gitlab/issues/257 private api: any //typeof Gitlab @@ -52,6 +54,18 @@ class GitLabAPI { return `${this.projectURL}/merge_requests/${this.repoMetadata.pullRequestID}` } + getUser = async (): Promise => { + // if (this.user) { + // return this.user + // } + + const user = (await this.api.Users.current()) as GitLabUserProfile + + // this.user = user + + return user + } + getMergeRequestInfo = async (): Promise => { // if (this.mr) { // return this.mr @@ -93,15 +107,17 @@ class GitLabAPI { } getMergeRequestComments = async (): Promise => { - const api = this.api.MergeRequestNotes() + const api = this.api.MergeRequestNotes return (await api.all(this.repoMetadata.repoSlug, this.repoMetadata.pullRequestID)) as GitLabComment[] } getMergeRequestInlineComments = async (): Promise => { - const api = this.api.MergeRequestNotes() - return (await api - .all(this.repoMetadata.repoSlug, this.repoMetadata.pullRequestID) - .filter((comment: GitLabComment) => comment.type == "DiffNote")) as GitLabInlineComment[] + const api = this.api.MergeRequestNotes + const res = await api.all(this.repoMetadata.repoSlug, this.repoMetadata.pullRequestID) + + const returns = res.filter((comment: GitLabComment) => comment.type == "DiffNote") as GitLabInlineComment[] + + return Promise.resolve(returns) } } diff --git a/source/runner/jsonToDSL.ts b/source/runner/jsonToDSL.ts index 60feb3bee..15ea855b0 100644 --- a/source/runner/jsonToDSL.ts +++ b/source/runner/jsonToDSL.ts @@ -64,9 +64,6 @@ const apiForDSL = (dsl: DangerDSLJSONType): OctoKit | BitBucketServerAPI | GitLa } const gitlab = dsl.gitlab - console.log("??????") - d("???????????") - d({ dsl }) if (gitlab != null && process.env["DANGER_GITLAB_API_TOKEN"] != null) { // d({ gitlab }) return new GitLabAPI(gitlab.metadata, process.env["DANGER_GITLAB_API_TOKEN"]!)