-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add recent cipe view & notifications (#2322)
- Loading branch information
Showing
31 changed files
with
1,935 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
libs/language-server/workspace/src/lib/get-recent-cipe-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { lspLogger } from '@nx-console/language-server/utils'; | ||
import { | ||
getNxAccessToken, | ||
getNxCloudId, | ||
getNxCloudUrl, | ||
} from '@nx-console/shared/npm'; | ||
import { CIPEInfo, CIPEInfoError } from '@nx-console/shared/types'; | ||
import { execSync } from 'child_process'; | ||
import { xhr } from 'request-light'; | ||
import { getNxCloudConfigIni } from './get-cloud-onboarding-info'; | ||
|
||
export async function getRecentCIPEData(workspacePath: string): Promise<{ | ||
info?: CIPEInfo[]; | ||
error?: CIPEInfoError; | ||
workspaceUrl?: string; | ||
}> { | ||
const branches = getRecentlyCommittedGitBranches(workspacePath); | ||
|
||
const nxCloudUrl = await getNxCloudUrl(workspacePath); | ||
const nxCloudId = await getNxCloudId(workspacePath); | ||
const nxCloudConfigIni = getNxCloudConfigIni(); | ||
|
||
const personalAccessToken = | ||
nxCloudConfigIni?.[nxCloudUrl]?.personalAccessToken; | ||
|
||
const data = JSON.stringify({ | ||
branches: branches.map((branch) => branch.name), | ||
}); | ||
|
||
const url = `${nxCloudUrl}/nx-cloud/nx-console/ci-pipeline-executions`; | ||
const headers: any = { | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
if (nxCloudId) { | ||
headers['Nx-Cloud-Id'] = nxCloudId; | ||
} | ||
if (personalAccessToken) { | ||
headers['Nx-Cloud-Personal-Access-Token'] = personalAccessToken; | ||
} | ||
|
||
const accessToken = await getNxAccessToken(workspacePath); | ||
if (accessToken) { | ||
headers['Authorization'] = accessToken; | ||
} | ||
|
||
try { | ||
const response = await xhr({ | ||
type: 'POST', | ||
url, | ||
headers, | ||
data, | ||
timeout: 5000, | ||
}); | ||
const responseData = JSON.parse(response.responseText) as { | ||
ciPipelineExecutions: CIPEInfo[]; | ||
workspaceUrl: string; | ||
}; | ||
return { | ||
info: responseData.ciPipelineExecutions, | ||
workspaceUrl: responseData.workspaceUrl, | ||
}; | ||
} catch (e) { | ||
if (e.status === 401) { | ||
lspLogger.log(`Authentication error: ${e.responseText}`); | ||
return { | ||
error: { | ||
type: 'authentication', | ||
message: e.responseText, | ||
}, | ||
}; | ||
} | ||
lspLogger.log(`Error: ${JSON.stringify(e)}`); | ||
return { | ||
error: { | ||
type: 'other', | ||
message: e.responseText ?? e.message, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
function getRecentlyCommittedGitBranches( | ||
workspacePath: string | ||
): { name: string; time: string }[] { | ||
try { | ||
const localUserEmail = execSync('git config user.email').toString().trim(); | ||
const oneHourAgo = new Date(Date.now() - 60 * 60 * 24 * 1000).toISOString(); | ||
|
||
const res = execSync( | ||
'git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname) - %(committerdate:iso-strict) - %(authoremail)"', | ||
{ | ||
cwd: workspacePath, | ||
} | ||
).toString(); | ||
|
||
const branches = res | ||
.split('\n') | ||
.filter((line) => line.trim() !== '') | ||
.map((line) => { | ||
const [refname, time, email] = line | ||
.split(' - ') | ||
.map((item) => item.trim()); | ||
return { | ||
name: refname.replace('refs/heads/', ''), | ||
time, | ||
email: email, | ||
}; | ||
}) | ||
.filter((item) => { | ||
return item.email.includes(localUserEmail) && item.time >= oneHourAgo; | ||
}); | ||
|
||
return branches; | ||
} catch (e) { | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './lib/nx-workspace'; | ||
export * from './lib/project-folder-tree'; | ||
export * from './lib/cloud-onboarding-info'; | ||
export * from './lib/cloud-info'; | ||
export * from './lib/pdv-data'; |
Oops, something went wrong.