screwdriver-coverage-sonar 5.0.1 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +51 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -101,14 +101,61 @@ class CoverageSonar extends CoverageBase {
101
101
  });
102
102
  }
103
103
 
104
+ /**
105
+ * Returns the configured Git App name for the given SCM context
106
+ * @param {String} scmContext SCM context in format "scm:host" (e.g., "github:github.com")
107
+ * @returns {String} Git App name
108
+ */
109
+ async _getGitApp(scmContext) {
110
+ const scmHost = scmContext && scmContext.includes(':') ? scmContext.split(':')[1] : scmContext;
111
+
112
+ if (!scmHost) {
113
+ logger.error('Invalid scmContext provided; using default Git App name');
114
+
115
+ return this.sonarGitAppName;
116
+ }
117
+
118
+ const { body } = await request({
119
+ method: 'GET',
120
+ url: `${this.sonarHost}/api/alm_settings/list`,
121
+ username: this.adminToken
122
+ });
123
+
124
+ const almSettings = (body && body.almSettings) || [];
125
+
126
+ if (almSettings.length === 0) {
127
+ logger.error('No ALM settings found in SonarQube; using default Git App name');
128
+
129
+ return this.sonarGitAppName;
130
+ }
131
+ if (almSettings.length === 1) {
132
+ return almSettings[0].key;
133
+ }
134
+
135
+ const gitAppSetting = almSettings.find(app => app.key.endsWith(`[${scmHost}]`));
136
+
137
+ if (!gitAppSetting) {
138
+ const availableKeys = almSettings.map(app => app.key).join(', ');
139
+
140
+ logger.error(
141
+ `Git App for scm '[${scmHost}]' not found in SonarQube. Available keys: ${availableKeys}. Using default Git App name`
142
+ );
143
+
144
+ return this.sonarGitAppName;
145
+ }
146
+
147
+ return gitAppSetting.key;
148
+ }
149
+
104
150
  /**
105
151
  * Configure Git App in SonarQube
106
152
  * @param {String} projectKey Sonar project key
107
153
  * @param {String} projectName Sonar project name
154
+ * @param {String} scmContext SCM context (github:github.com or github:github.example.com)
108
155
  * @return {Promise} Nothing if Git App configured successfully
109
156
  */
110
- configureGitApp(projectKey, projectName) {
111
- const gitApp = this.sonarGitAppName;
157
+ async configureGitApp(projectKey, projectName, scmContext) {
158
+ const gitApp = await this._getGitApp(scmContext);
112
159
  const gitAppEncoded = encodeURIComponent(gitApp);
113
160
  const componentId = encodeURIComponent(projectKey);
114
161
 
@@ -363,7 +410,7 @@ class CoverageSonar extends CoverageBase {
363
410
  * to talk to coverage server
364
411
  */
365
412
  _getAccessToken({ scope, username, projectKey, projectName, jobName, pipelineName, buildCredentials }) {
366
- const { jobId, pipelineId, prParentJobId } = buildCredentials;
413
+ const { jobId, pipelineId, prParentJobId, scmContext } = buildCredentials;
367
414
  let projectData = { username, projectKey, projectName };
368
415
 
369
416
  if (!username || !projectKey || !projectName || projectName.includes('undefined')) {
@@ -382,7 +429,7 @@ class CoverageSonar extends CoverageBase {
382
429
  const password = uuidv4();
383
430
 
384
431
  return this.createProject(projectData.projectKey)
385
- .then(() => this.configureGitApp(projectData.projectKey, projectData.projectName))
432
+ .then(() => this.configureGitApp(projectData.projectKey, projectData.projectName, scmContext))
386
433
  .then(() => this.createUser(projectData.username, password))
387
434
  .then(() => this.grantUserPermission(projectData.username, projectData.projectKey))
388
435
  .then(() => this.generateToken(projectData.username))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-coverage-sonar",
3
- "version": "5.0.1",
3
+ "version": "5.1.1",
4
4
  "description": "SonarQube implementation of the coverage-base class",
5
5
  "main": "index.js",
6
6
  "scripts": {