Skip to content

Commit

Permalink
feat(gitlab): use forked_from_project for Merge Requests target project
Browse files Browse the repository at this point in the history
Appian specific patch to always use the upstream (forked_from_project) when
dealing with Merge Requests or Issues.

This ensures that Renovate follows the standard development workflow where
branches are created against the "dev" fork and then Merge Requests
are opened against the forked (or "prod") fork.

In addition, we want the Dependency Dashboard created against the "prod"
fork, so any calls to the Issues API goes to the "prod" fork.
  • Loading branch information
valdisrigdon authored and rohithrrao committed May 10, 2024
1 parent 376a2a5 commit cdc0a11
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 63 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ jobs:
merge-multiple: true

- name: Codecov
uses: codecov/codecov-action@84508663e988701840491b86de86b666e8a86bed # v4.3.0
uses: codecov/codecov-action@5ecb98a3c6b747ed38dc09f787459979aebb39be # v4.3.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: coverage/lcov
Expand Down Expand Up @@ -558,6 +558,9 @@ jobs:
- name: Build
run: pnpm build:docs

- name: Test docs
run: pnpm test:docs

- name: Upload
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
with:
Expand Down Expand Up @@ -629,7 +632,7 @@ jobs:
show-progress: false

- name: docker-config
uses: containerbase/internal-tools@75d68da3c8cd3d622cf6d4b8d2dfe7e4dc33dfed # v3.0.78
uses: containerbase/internal-tools@dbc381e7a824b3777dceafc226804644311ab9a1 # v3.0.82
with:
command: docker-config

Expand Down
2 changes: 2 additions & 0 deletions lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ exports[`modules/platform/gitlab/index initRepo should fall back respecting when
"defaultBranch": "master",
"ignorePrAuthor": undefined,
"mergeMethod": "merge",
"mergeRequestRepository": "some%2Frepo%2Fproject",
"repository": "some%2Frepo%2Fproject",
"url": "http://oauth2:123test@mycompany.com/gitlab/some/repo/project.git",
},
Expand All @@ -247,6 +248,7 @@ exports[`modules/platform/gitlab/index initRepo should use ssh_url_to_repo if gi
"defaultBranch": "master",
"ignorePrAuthor": undefined,
"mergeMethod": "merge",
"mergeRequestRepository": "some%2Frepo%2Fproject",
"repository": "some%2Frepo%2Fproject",
"url": "ssh://git@gitlab.com/some%2Frepo%2Fproject.git",
},
Expand Down
66 changes: 38 additions & 28 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ let config: {
cloneSubmodules: boolean | undefined;
ignorePrAuthor: boolean | undefined;
squash: boolean;
mergeRequestRepository: string;
} = {} as any;

const defaults = {
Expand Down Expand Up @@ -323,7 +324,6 @@ export async function initRepo({
);
throw new Error(REPOSITORY_ARCHIVED);
}

if (res.body.mirror && includeMirrors !== true) {
logger.debug(
'Repository is a mirror - throwing error to abort renovation',
Expand Down Expand Up @@ -359,6 +359,8 @@ export async function initRepo({
}
logger.debug(`${repository} default branch = ${config.defaultBranch}`);
delete config.prList;
config.mergeRequestRepository =
res.body.forked_from_project?.id ?? config.repository;
logger.debug('Enabling Git FS');
const url = getRepoUrl(repository, gitUrl, res);
await git.initRepo({
Expand Down Expand Up @@ -555,7 +557,7 @@ async function fetchPrList(): Promise<Pr[]> {
searchParams.scope = 'created_by_me';
}
const query = getQueryString(searchParams);
const urlString = `projects/${config.repository}/merge_requests?${query}`;
const urlString = `projects/${config.mergeRequestRepository}/merge_requests?${query}`;
try {
const res = await gitlabApi.getJson<
{
Expand Down Expand Up @@ -593,7 +595,7 @@ export async function getPrList(): Promise<Pr[]> {

async function ignoreApprovals(pr: number): Promise<void> {
try {
const url = `projects/${config.repository}/merge_requests/${pr}/approval_rules`;
const url = `projects/${config.mergeRequestRepository}/merge_requests/${pr}/approval_rules`;
const { body: rules } = await gitlabApi.getJson<
{
name: string;
Expand Down Expand Up @@ -684,7 +686,7 @@ async function tryPrAutomerge(
pipeline: {
status: string;
};
}>(`projects/${config.repository}/merge_requests/${pr}`, {
}>(`projects/${config.mergeRequestRepository}/merge_requests/${pr}`, {
memCache: false,
});
// detailed_merge_status is available with Gitlab >=15.6.0
Expand Down Expand Up @@ -713,7 +715,7 @@ async function tryPrAutomerge(
for (let attempt = 1; attempt <= retryTimes; attempt += 1) {
try {
await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${pr}/merge`,
`projects/${config.mergeRequestRepository}/merge_requests/${pr}/merge`,
{
body: {
should_remove_source_branch: true,
Expand All @@ -739,7 +741,7 @@ async function tryPrAutomerge(
async function approvePr(pr: number): Promise<void> {
try {
await gitlabApi.postJson(
`projects/${config.repository}/merge_requests/${pr}/approve`,
`projects/${config.mergeRequestRepository}/merge_requests/${pr}/approve`,
);
} catch (err) {
logger.warn({ err }, 'GitLab: Error approving merge request');
Expand All @@ -761,6 +763,9 @@ export async function createPr({
}
const description = sanitize(rawDescription);
logger.debug(`Creating Merge Request: ${title}`);
// the creation is always against the repository with the branch
// but if the target_project_id is forked_from_project, then the Merge
// Request will end up in that repository
const res = await gitlabApi.postJson<Pr & { iid: number }>(
`projects/${config.repository}/merge_requests`,
{
Expand All @@ -772,6 +777,7 @@ export async function createPr({
description,
labels: (labels ?? []).join(','),
squash: config.squash,
target_project_id: config.mergeRequestRepository,
},
},
);
Expand All @@ -794,7 +800,7 @@ export async function createPr({

export async function getPr(iid: number): Promise<GitlabPr> {
logger.debug(`getPr(${iid})`);
const mr = await getMR(config.repository, iid);
const mr = await getMR(config.mergeRequestRepository, iid);

// Harmonize fields with GitHub
const pr: GitlabPr = {
Expand Down Expand Up @@ -852,7 +858,7 @@ export async function updatePr({
}

await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${iid}`,
`projects/${config.mergeRequestRepository}/merge_requests/${iid}`,
{ body },
);

Expand All @@ -873,7 +879,7 @@ export async function reattemptPlatformAutomerge({
export async function mergePr({ id }: MergePRConfig): Promise<boolean> {
try {
await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${id}/merge`,
`projects/${config.mergeRequestRepository}/merge_requests/${id}/merge`,
{
body: {
should_remove_source_branch: true,
Expand Down Expand Up @@ -1010,11 +1016,12 @@ export async function setBranchStatus({
state: renovateState,
url: targetUrl,
}: BranchStatusConfig): Promise<void> {
const branchSourceProject = config.repository;
// First, get the branch commit SHA
const branchSha = git.getBranchCommit(branchName);
// Now, check the statuses for that commit
// TODO: types (#22198)
const url = `projects/${config.repository}/statuses/${branchSha!}`;
const url = `projects/${branchSourceProject}/statuses/${branchSha!}`;
let state = 'success';
if (renovateState === 'yellow') {
state = 'pending';
Expand Down Expand Up @@ -1076,7 +1083,7 @@ export async function getIssueList(): Promise<GitlabIssue[]> {
});
const res = await gitlabApi.getJson<
{ iid: number; title: string; labels: string[] }[]
>(`projects/${config.repository}/issues?${query}`, {
>(`projects/${config.mergeRequestRepository}/issues?${query}`, {
memCache: false,
paginate: true,
});
Expand All @@ -1101,7 +1108,7 @@ export async function getIssue(
try {
const issueBody = (
await gitlabApi.getJson<{ description: string }>(
`projects/${config.repository}/issues/${number}`,
`projects/${config.mergeRequestRepository}/issues/${number}`,
{ memCache: useCache },
)
).body.description;
Expand Down Expand Up @@ -1148,13 +1155,13 @@ export async function ensureIssue({
if (issue) {
const existingDescription = (
await gitlabApi.getJson<{ description: string }>(
`projects/${config.repository}/issues/${issue.iid}`,
`projects/${config.mergeRequestRepository}/issues/${issue.iid}`,
)
).body.description;
if (issue.title !== title || existingDescription !== description) {
logger.debug('Updating issue');
await gitlabApi.putJson(
`projects/${config.repository}/issues/${issue.iid}`,
`projects/${config.mergeRequestRepository}/issues/${issue.iid}`,
{
body: {
title,
Expand All @@ -1167,14 +1174,17 @@ export async function ensureIssue({
return 'updated';
}
} else {
await gitlabApi.postJson(`projects/${config.repository}/issues`, {
body: {
title,
description,
labels: (labels ?? []).join(','),
confidential: confidential ?? false,
await gitlabApi.postJson(
`projects/${config.mergeRequestRepository}/issues`,
{
body: {
title,
description,
labels: (labels ?? []).join(','),
confidential: confidential ?? false,
},
},
});
);
logger.info('Issue created');
// delete issueList so that it will be refetched as necessary
delete config.issueList;
Expand All @@ -1197,7 +1207,7 @@ export async function ensureIssueClosing(title: string): Promise<void> {
if (issue.title === title) {
logger.debug({ issue }, 'Closing issue');
await gitlabApi.putJson(
`projects/${config.repository}/issues/${issue.iid}`,
`projects/${config.mergeRequestRepository}/issues/${issue.iid}`,
{
body: { state_event: 'close' },
},
Expand Down Expand Up @@ -1282,7 +1292,7 @@ export async function addReviewers(
newReviewerIDs = [...new Set(newReviewerIDs)];

try {
await updateMR(config.repository, iid, {
await updateMR(config.mergeRequestRepository, iid, {
reviewer_ids: [...existingReviewerIDs, ...newReviewerIDs],
});
} catch (err) {
Expand All @@ -1301,7 +1311,7 @@ export async function deleteLabel(
.filter((l: string) => l !== label)
.join(',');
await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${issueNo}`,
`projects/${config.mergeRequestRepository}/merge_requests/${issueNo}`,
{
body: { labels },
},
Expand All @@ -1314,7 +1324,7 @@ export async function deleteLabel(
async function getComments(issueNo: number): Promise<GitlabComment[]> {
// GET projects/:owner/:repo/merge_requests/:number/notes
logger.debug(`Getting comments for #${issueNo}`);
const url = `projects/${config.repository}/merge_requests/${issueNo}/notes`;
const url = `projects/${config.mergeRequestRepository}/merge_requests/${issueNo}/notes`;
const comments = (
await gitlabApi.getJson<GitlabComment[]>(url, { paginate: true })
).body;
Expand All @@ -1325,7 +1335,7 @@ async function getComments(issueNo: number): Promise<GitlabComment[]> {
async function addComment(issueNo: number, body: string): Promise<void> {
// POST projects/:owner/:repo/merge_requests/:number/notes
await gitlabApi.postJson(
`projects/${config.repository}/merge_requests/${issueNo}/notes`,
`projects/${config.mergeRequestRepository}/merge_requests/${issueNo}/notes`,
{
body: { body },
},
Expand All @@ -1339,7 +1349,7 @@ async function editComment(
): Promise<void> {
// PUT projects/:owner/:repo/merge_requests/:number/notes/:id
await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${issueNo}/notes/${commentId}`,
`projects/${config.mergeRequestRepository}/merge_requests/${issueNo}/notes/${commentId}`,
{
body: { body },
},
Expand All @@ -1352,7 +1362,7 @@ async function deleteComment(
): Promise<void> {
// DELETE projects/:owner/:repo/merge_requests/:number/notes/:id
await gitlabApi.deleteJson(
`projects/${config.repository}/merge_requests/${issueNo}/notes/${commentId}`,
`projects/${config.mergeRequestRepository}/merge_requests/${issueNo}/notes/${commentId}`,
);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/modules/platform/gitlab/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ export interface RepoResponse {
empty_repo: boolean;
ssh_url_to_repo: string;
http_url_to_repo: string;
forked_from_project: boolean;
repository_access_level: 'disabled' | 'private' | 'enabled';
merge_requests_access_level: 'disabled' | 'private' | 'enabled';
merge_method: MergeMethod;
path_with_namespace: string;
squash_option?: 'never' | 'always' | 'default_on' | 'default_off';
forked_from_project?: {
id: string;
};
}

// See https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/graphql/types/user_status_type.rb
Expand Down
Loading

0 comments on commit cdc0a11

Please sign in to comment.