Skip to content

Commit

Permalink
Add gitlabCompareChanges test, update fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Feb 25, 2020
1 parent a112547 commit a9eff81
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 125 deletions.
100 changes: 0 additions & 100 deletions source/platforms/_tests/fixtures/gitlab_mr.json

This file was deleted.

3 changes: 2 additions & 1 deletion source/platforms/gitlab/GitLabGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const gitLabGitDSL = (gitlab: GitLabDSL, json: GitJSONDSL, gitlabAPI: Git
return gitJSONToGitDSL(json, config)
}

const gitlabChangesToDiff = (changes: GitLabMRChange[]): string => {
export const gitlabChangesToDiff = (changes: GitLabMRChange[]): string => {
d("Converting GitLab Changes to Diff")
// Gitlab doesn't return full raw git diff, relevant issue: https://gitlab.com/gitlab-org/gitlab/issues/24913
return changes
.map(change => {
Expand Down
31 changes: 25 additions & 6 deletions source/platforms/gitlab/_tests/_gitlab_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFileSync } from "fs"
const nockBack = nock.back
nockBack.fixtures = __dirname + "/fixtures"

// We're testing https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/27117
// We're testing https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/27117
// This has been chosen because it is already merged and publicly available, it's unlikely to change

/** Returns a fixture. */
Expand All @@ -27,7 +27,7 @@ describe("GitLab API", () => {

beforeEach(() => {
api = new GitLabAPI(
{ pullRequestID: "27117", repoSlug: "gitlab-org/gitlab-ce" },
{ pullRequestID: "27117", repoSlug: "gitlab-org/gitlab-foss" },
getGitLabAPICredentialsFromEnv({
DANGER_GITLAB_HOST: "gitlab.com",
DANGER_GITLAB_API_TOKEN: "FAKE_DANGER_GITLAB_API_TOKEN",
Expand All @@ -37,22 +37,22 @@ describe("GitLab API", () => {

it("configures host from CI_API_V4_URL", () => {
api = new GitLabAPI(
{ pullRequestID: "27117", repoSlug: "gitlab-org/gitlab-ce" },
{ pullRequestID: "27117", repoSlug: "gitlab-org/gitlab-foss" },
getGitLabAPICredentialsFromEnv({
CI_API_V4_URL: "https://testciapiv4url.com/api/v4",
DANGER_GITLAB_API_TOKEN: "FAKE_DANGER_GITLAB_API_TOKEN",
})
)

expect(api.projectURL).toBe("https://testciapiv4url.com/gitlab-org/gitlab-ce")
expect(api.projectURL).toBe("https://testciapiv4url.com/gitlab-org/gitlab-foss")
})

it("projectURL is defined", () => {
expect(api.projectURL).toBe("https://gitlab.com/gitlab-org/gitlab-ce")
expect(api.projectURL).toBe("https://gitlab.com/gitlab-org/gitlab-foss")
})

it("mergeRequestURL is defined", () => {
expect(api.mergeRequestURL).toBe("https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/27117")
expect(api.mergeRequestURL).toBe("https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/27117")
})

const sanitizeUserResponse = (nocks: NockDefinition[]): NockDefinition[] => {
Expand Down Expand Up @@ -138,4 +138,23 @@ describe("GitLab API", () => {
// TODO: There are no inline notes on this MR, we should look for a public one that has inline notes to improve this test
expect(result).toEqual([])
})

it("getCompareChanges", async () => {
const { nockDone } = await nockBack("getCompareChanges.json")
const result = await api.getCompareChanges(
"50cd5d9b776848cf23f1fd1ec52789dbdf946185",
"28531ab43666b5fdf37e0a70db3bcbf7d3f92183"
)
nockDone()
const { response } = loadFixture("getCompareChanges")
expect(result).toEqual(response.diffs)
})

it("getCompareChanges without base/head", async () => {
const { nockDone } = await nockBack("getMergeRequestChanges.json")
const result = await api.getCompareChanges()
nockDone()
const { response } = loadFixture("getCompareChanges")
expect(result).toEqual(response.diffs)
})
})
113 changes: 113 additions & 0 deletions source/platforms/gitlab/_tests/fixtures/getCompareChanges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
[
{
"scope": "https://gitlab.com:443",
"method": "GET",
"path": "/api/v4/projects/gitlab-org%2Fgitlab-foss/repository/compare?from=50cd5d9b776848cf23f1fd1ec52789dbdf946185&to=28531ab43666b5fdf37e0a70db3bcbf7d3f92183",
"body": "",
"status": 200,
"response": {
"commit": {
"id": "28531ab43666b5fdf37e0a70db3bcbf7d3f92183",
"short_id": "28531ab4",
"created_at": "2019-04-08T11:56:17.000+01:00",
"parent_ids": ["575ec3af131e9c101edfc50ee563381b37c8076e"],
"title": "Pick reviewers based on branch name",
"message": "Pick reviewers based on branch name\n\nChange reviewer roulette to always pick the same reviewers for the same\nbranch name. We do this by:\n\n1. Making the branch name 'canonical' across CE and EE by stripping a\n leading 'ce-' or 'ee-' and a trailing '-ce' or '-ee'. If people are\n following our branch naming guidelines, this should give the same\n branch name in both repos.\n2. Converting the branch name to a stable integer by taking the integer\n form of its MD5.\n3. Passing that integer as a seed to Ruby's `Random` class, which 'may\n be used to ensure repeatable sequences of pseudo-random numbers\n between different runs of the program' (from the Ruby documentation).\n\nThe upshot is that the same branch name (in CE and EE) should always\npick the same reviewers, and those should be evenly distributed across\nthe set of possible reviewers due to the use of MD5.\n",
"author_name": "Sean McGivern",
"author_email": "sean@gitlab.com",
"authored_date": "2019-04-08T11:44:57.000+01:00",
"committer_name": "Sean McGivern",
"committer_email": "sean@gitlab.com",
"committed_date": "2019-04-08T11:56:17.000+01:00"
},
"commits": [
{
"id": "575ec3af131e9c101edfc50ee563381b37c8076e",
"short_id": "575ec3af",
"created_at": "2019-04-08T11:56:17.000+01:00",
"parent_ids": ["50cd5d9b776848cf23f1fd1ec52789dbdf946185"],
"title": "Add issue links to Danger roulette comments",
"message": "Add issue links to Danger roulette comments\n",
"author_name": "Sean McGivern",
"author_email": "sean@gitlab.com",
"authored_date": "2019-04-08T10:50:39.000+01:00",
"committer_name": "Sean McGivern",
"committer_email": "sean@gitlab.com",
"committed_date": "2019-04-08T11:56:17.000+01:00"
},
{
"id": "28531ab43666b5fdf37e0a70db3bcbf7d3f92183",
"short_id": "28531ab4",
"created_at": "2019-04-08T11:56:17.000+01:00",
"parent_ids": ["575ec3af131e9c101edfc50ee563381b37c8076e"],
"title": "Pick reviewers based on branch name",
"message": "Pick reviewers based on branch name\n\nChange reviewer roulette to always pick the same reviewers for the same\nbranch name. We do this by:\n\n1. Making the branch name 'canonical' across CE and EE by stripping a\n leading 'ce-' or 'ee-' and a trailing '-ce' or '-ee'. If people are\n following our branch naming guidelines, this should give the same\n branch name in both repos.\n2. Converting the branch name to a stable integer by taking the integer\n form of its MD5.\n3. Passing that integer as a seed to Ruby's `Random` class, which 'may\n be used to ensure repeatable sequences of pseudo-random numbers\n between different runs of the program' (from the Ruby documentation).\n\nThe upshot is that the same branch name (in CE and EE) should always\npick the same reviewers, and those should be evenly distributed across\nthe set of possible reviewers due to the use of MD5.\n",
"author_name": "Sean McGivern",
"author_email": "sean@gitlab.com",
"authored_date": "2019-04-08T11:44:57.000+01:00",
"committer_name": "Sean McGivern",
"committer_email": "sean@gitlab.com",
"committed_date": "2019-04-08T11:56:17.000+01:00"
}
],
"diffs": [
{
"old_path": "danger/roulette/Dangerfile",
"new_path": "danger/roulette/Dangerfile",
"a_mode": "100644",
"b_mode": "100644",
"new_file": false,
"renamed_file": false,
"deleted_file": false,
"diff": "@@ -1,5 +1,7 @@\n # frozen_string_literal: true\n \n+require 'digest/md5'\n+\n MESSAGE = <<MARKDOWN\n ## Reviewer roulette\n \n@@ -29,17 +31,22 @@ Please consider creating a merge request to\n for them.\n MARKDOWN\n \n-def spin(team, project, category)\n+def spin(team, project, category, branch_name)\n+ rng = Random.new(Digest::MD5.hexdigest(branch_name).to_i(16))\n+\n reviewers = team.select { |member| member.reviewer?(project, category) }\n traintainers = team.select { |member| member.traintainer?(project, category) }\n maintainers = team.select { |member| member.maintainer?(project, category) }\n \n # TODO: filter out people who are currently not in the office\n+ # https://gitlab.com/gitlab-org/gitlab-ce/issues/57652\n+ #\n # TODO: take CODEOWNERS into account?\n+ # https://gitlab.com/gitlab-org/gitlab-ce/issues/57653\n \n # Make traintainers have triple the chance to be picked as a reviewer\n- reviewer = (reviewers + traintainers + traintainers).sample\n- maintainer = maintainers.sample\n+ reviewer = (reviewers + traintainers + traintainers).sample(random: rng)\n+ maintainer = maintainers.sample(random: rng)\n \n \"| #{helper.label_for_category(category)} | #{reviewer&.markdown_name} | #{maintainer&.markdown_name} |\"\n end\n@@ -65,6 +72,12 @@ categories = changes.keys - [:unknown]\n # CSS Clean up MRs are reviewed using a slightly different process, so we\n # disable the review roulette for such MRs.\n if changes.any? && !gitlab.mr_labels.include?('single codebase') && !gitlab.mr_labels.include?('CSS cleanup')\n+ # Strip leading and trailing CE/EE markers\n+ canonical_branch_name = gitlab\n+ .mr_json['source_branch']\n+ .gsub(/^[ce]e-/, '')\n+ .gsub(/-[ce]e$/, '')\n+\n team =\n begin\n helper.project_team\n@@ -79,7 +92,7 @@ if changes.any? && !gitlab.mr_labels.include?('single codebase') && !gitlab.mr_l\n project = helper.project_name\n unknown = changes.fetch(:unknown, [])\n \n- rows = categories.map { |category| spin(team, project, category) }\n+ rows = categories.map { |category| spin(team, project, category, canonical_branch_name) }\n \n markdown(MESSAGE)\n markdown(CATEGORY_TABLE_HEADER + rows.join(\"\\n\")) unless rows.empty?\n"
}
],
"compare_timeout": false,
"compare_same_ref": false
},
"rawHeaders": [
"Server",
"nginx",
"Date",
"Tue, 25 Feb 2020 17:43:41 GMT",
"Content-Type",
"application/json",
"Content-Length",
"5859",
"Connection",
"close",
"Cache-Control",
"max-age=0, private, must-revalidate",
"Etag",
"W/\"ab8052cb9a009582d0fe4e988f5d668d\"",
"Vary",
"Origin",
"X-Content-Type-Options",
"nosniff",
"X-Frame-Options",
"SAMEORIGIN",
"X-Request-Id",
"FM1OBYEIHt9",
"X-Runtime",
"0.137387",
"Strict-Transport-Security",
"max-age=31536000",
"Referrer-Policy",
"strict-origin-when-cross-origin",
"RateLimit-Limit",
"600",
"RateLimit-Observed",
"1",
"RateLimit-Remaining",
"599",
"RateLimit-Reset",
"1582652681",
"RateLimit-ResetTime",
"Tue, 25 Feb 2020 17:44:41 GMT",
"GitLab-LB",
"fe-13-lb-gprd",
"GitLab-SV",
"localhost"
]
}
]
Loading

0 comments on commit a9eff81

Please sign in to comment.